<template>
  <div id="Chapter1_1" class="content-wrap">
    <div style="margin: 30px 0px 50px; width: 20%">
      <router-link to="/MyPlan.page">
        <div class="logo mb25"><img src="../../../../resources/img/logo2.png" alt="" /></div>
      </router-link>
    </div>
    <div class="title-box mb25 flex align-center mt40">
      <span class="title mr40">1. Hello WORLD</span>
      <span class="subtitle">my name is dd</span>
    </div>
    <div class="flex justify-between align-center">
      <div class="pre-btn" @click="goToPage('Chapter3_11')"><img src="../../../../resources/img/left.png" alt="" /></div>
      <div class="content title-box">
        <p class="title mt25 title-bg">step3</p>
        <div class="flex align-center mb30">
          <p class="subtitle2 mr20">단어를 바르게 배열해 문장을 완성하세요</p>
          <!-- <button><img src="../../../../resources/img/btn10_s.png" alt="">
               </button> -->
        </div>

        <div class="text-ct">
          <div class="time-hint">
            <button class="hint-btn">HINT</button>
            <div class="time-bg mt20">
              <div>
                <div class="time">
                  <p class="second">{{ timer }}</p>
                  <p class="text">sec</p>
                </div>
              </div>
            </div>
          </div>
          <div class="dragGroup mt40">
            <div class="flex justify-center" style="gap: 20px">
              <div class="dropContainer" id="sourceContainer">
                <button v-for="(word, index) in question" :key="index" draggable="true" @dragstart="onDragStart($event, word, index)">
                  <p>{{ word }}</p>
                </button>
              </div>
            </div>
          </div>
          <div class="dropContainer flex align-center justify-center mt30" id="targetContainer">
            <div class="dropSlot" v-for="(slot, index) in questionLength" :key="index" @dragover.prevent @drop="onDrop($event, index)">
              <div class="dropSlot-inner" draggable="true" @dragstart="onDragStart($event, userAnswer[index], index)">
                <div class="dropSlot-inner">
                  <div v-if="userAnswer[index]" class="dropped-char">
                    {{ userAnswer[index] }}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <button class="submit-button" @click="handleSubmit()">제출하기</button>
      </div>
      <div class="next-btn" @click="goToPage('Chapter3_13')"><img src="../../../../resources/img/right.png" alt="" /></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timer: "00",
      question: [`don't`, "banana", "i", "like"],
      questionLength: null,
      answer: null,

      userAnswer: [], // 초기화 시 빈 배열로 설정
      draggedChar: null, // 드래그한 문자를 임시로 저장
      draggedCharIndex: null, // 드래그한 문자의 인덱스 저장
    };
  },
  methods: {
    goToPage(page) {
      this.$router.push({ name: page });
    },
    startTimer() {
      if (this.intervalId) {
        clearInterval(this.intervalId);
      }
      this.timer = 5;
      this.intervalId = setInterval(() => {
        if (this.timer > 0) {
          this.timer--;
        } else {
          clearInterval(this.intervalId);
        }
      }, 1000);
    },
    initializeAnswer() {
      this.questionLength = this.question.length;
      this.userAnswer = new Array(this.questionLength).fill(null); // userAnswer 배열을 question 길이만큼 초기화
    },
    // 드래그 시작 시 호출
    onDragStart(event, char, index) {
      this.draggedChar = char;
      this.draggedCharIndex = index;
    },
    // 드롭 시 호출
    onDrop(event, index) {
      if (!this.userAnswer[index]) {
        // 해당 슬롯이 비어있는 경우에만 드롭
        this.userAnswer.splice(index, 1, this.draggedChar); // userAnswer에 드래그한 문자 추가
        this.draggedChar = null;
        this.draggedCharIndex = null;
      }
    },
    handleSubmit() {
      // 정답을 체크하거나 제출 시 로직 처리
      let answer = this.userAnswer.join(" ");
      console.log("정답 : ", answer);
    },
  },
  watch: {},
  computed: {},
  components: {},
  mounted() {
    this.initializeAnswer();
  },
};
</script>
<style scoped>
.textbox {
  height: 60px;
}
.textbox p {
  font-size: 28px;
  font-weight: bold;
  line-height: 65px;
}
.dropGroup button {
  position: relative;
}

.dropGroup button p {
  font-size: 48px;
}

.dragGroup button p {
  font-size: 48px;
}

#sourceContainer {
  margin-top: 100px;
  display: flex;
  flex-direction: row;
  gap: 230px;
}

#targetContainer {
  margin-top: 15rem;
  height: 100px;
  display: flex;
  flex-direction: row;
  gap: 80px;
}

#targetContainer .dropSlot {
  width: 250px;
  height: 70px;
  padding: 10px;
  border-bottom: 8px solid #ffba08;
}

#targetContainer .dropSlot .dropped-char {
  font-size: 48px;
  font-family: "ONEMobilePOPOTF";
}

.submit-button {
  position: absolute;
  background-color: #ffba08;
  padding: 10px 30px;
  border-radius: 10px;
  font-size: 28px;
  font-family: "ONEMobilePOPOTF";
  right: 5rem;
  bottom: 3rem;
}
</style>
