var tdv_sdk = {};
tdv_sdk.list = [
  // --- PART 1: MULTIPLE CHOICE (Q1 - Q10) ---
  {
    id: "q31",
    question:
      "We see many apricot blossoms at Tet in the South of Vietnam. What color are apricot blossoms usually?",
    options: ["Blue", "Yellow", "Purple", "Black"],
    answer: "Yellow",
    audio: "",
  },
  {
    id: "q33",
    question:
      "Square glutinous rice cake is a traditional food at Tet. What is its Vietnamese name?",
    options: ["Bánh mì", "Bánh tét", "Bánh chưng", "Bánh bao"],
    answer: "Bánh chưng",
    audio: "",
  },
  {
    id: "q1",
    question: "Which subject do students learn about numbers?",
    options: ["English", "Art", "Math", "Vietnamese"],
    answer: "Math",
    audio: "",
  },
  {
    id: "q2",
    question: "Which subject is about countries and maps?",
    options: ["Biology", "Geography", "Chemistry", "Physics"],
    answer: "Geography",
    audio: "",
  },
  {
    id: "q3",
    question: "Which sentence is correct?",
    options: [
      "Today is Monday.",
      "Today Monday is.",
      "Today are Monday.",
      "Monday today.",
    ],
    answer: "Today is Monday.",
    audio: "",
  },
  {
    id: "q4",
    question: "Which activity do you do when you wake up?",
    options: ["have a snack", "brush my teeth", "do my homework", "watch TV"],
    answer: "brush my teeth",
    audio: "",
  },
  {
    id: "q5",
    question: "Which activity means 'go to school by bus'?",
    options: ["walk to school", "catch the bus", "ride my bike", "get dressed"],
    answer: "catch the bus",
    audio: "",
  },
  {
    id: "q6",
    question: "Which word shows order?",
    options: ["always", "then", "never", "often"],
    answer: "then",
    audio: "",
  },
  {
    id: "q7",
    question: "Which sentence is correct?",
    options: [
      "I do my homework every day.",
      "I do every day my homework.",
      "I every day do my homework.",
      "I homework do every day.",
    ],
    answer: "I do my homework every day.",
    audio: "",
  },
  {
    id: "q8",
    question: "Which place can you watch a movie?",
    options: ["swimming pool", "playground", "movie theater", "sports center"],
    answer: "movie theater",
    audio: "",
  },
  {
    id: "q9",
    question: "Which adverb means '0% of the time?'",
    options: ["always", "usually", "sometimes", "never"],
    answer: "never",
    audio: "",
  },
  {
    id: "q10",
    question: "Which activity uses a ball?",
    options: ["watch TV", "walk the dog", "play soccer", "listen to music"],
    answer: "play soccer",
    audio: "",
  },

  // --- PART 2: FILL IN THE BLANKS (Q11 - Q20) ---
  {
    id: "q11",
    question: "First, I take a shower. ___, I get dressed.",
    options: ["Never", "Then", "Always", "Yesterday"],
    answer: "Then",
    audio: "",
  },
  {
    id: "q12",
    question: "He ___ to school by bus every day.",
    options: ["walk", "walks", "catch", "catches"],
    answer: "catches",
    audio: "",
  },
  {
    id: "q13",
    question: "Which sentence is correct?",
    options: [
      "She brush her teeth every day.",
      "She brushes her teeth every day.",
      "She brushing her teeth every day.",
      "She brush teeth every day.",
    ],
    answer: "She brushes her teeth every day.",
    audio: "",
  },
  {
    id: "q14",
    question: "I usually ___ TV in the evening.",
    options: ["watch", "watches", "watched", "watching"],
    answer: "watch",
    audio: "",
  },
  {
    id: "q15",
    question: "Which place can you buy food and clothes?",
    options: ["café", "theatre", "shopping mall", "playground"],
    answer: "shopping mall",
    audio: "",
  },
  {
    id: "q16",
    question: "I ___ go to the skatepark. (only on Sunday)",
    options: ["alway", "usuallys", "sometimes", "nevers"],
    answer: "sometimes",
    audio: "",
  },
  {
    id: "q17",
    question: "Which food is a drink?",
    options: ["mushrooms", "cucumber", "lemonade", "garlic"],
    answer: "lemonade",
    audio: "",
  },
  {
    id: "q18",
    question: "Choose the correct sentence.",
    options: [
      "I like noodles and rice.",
      "I like noodle and rice.",
      "I likes noodles and rice.",
      "I liking noodles and rice.",
    ],
    answer: "I like noodles and rice.",
    audio: "",
  },
  {
    id: "q19",
    question: "Which vehicle travels in the sky?",
    options: ["bus", "taxi", "train", "plane"],
    answer: "plane",
    audio: "",
  },
  {
    id: "q20",
    question: "Which country is in Europe?",
    options: ["Viet Nam", "Thailand", "France", "Singapore"],
    answer: "France",
    audio: "",
  },

  // --- PART 3: LISTENING – TRICKY VERSION (Q21 - Q30) ---
  // Có kèm Text context và Audio link giả định
  {
    id: "q21",
    question: "What subject does the speaker have today?",
    options: ["English", "Art", "Math", "Vietnamese"],
    answer: "Math",
    text: "Today is Wednesday. I have Math, not English. English is on Thursday.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_today_is_wednesday_i_have_math_not_english_english_fc9add41.mp3",
  },
  {
    id: "q22",
    question: "What does the speaker do last?",
    options: ["brush teeth", "get dressed", "walk to school", "catch the bus"],
    answer: "catch the bus",
    text: "Every morning, I brush my teeth. Then I get dressed. I catch the bus to school.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_every_morning_i_brush_my_teeth_then_i_get_dressed__909e2d52.mp3",
  },
  {
    id: "q23",
    question: "When does the speaker do homework?",
    options: ["after school", "in the morning", "in the evening", "at lunch"],
    answer: "in the evening",
    text: "After school, I have a snack. I don't do my homework then. I do it in the evening.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_after_school_i_have_a_snack_i_dont_do_my_homework__5ec48cc2.mp3",
  },
  {
    id: "q24",
    question: "What does the speaker NEVER do?",
    options: ["play soccer", "watch TV", "walk the dog", "listen to music"],
    answer: "walk the dog",
    text: "I usually play soccer. I sometimes watch TV. I never walk the dog.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_i_usually_play_soccer_i_sometimes_watch_tv_i_never_57a91e92.mp3",
  },
  {
    id: "q25",
    question: "Where do they go?",
    options: ["market", "café", "shopping mall", "playground"],
    answer: "shopping mall",
    text: "On Sunday, we go to the shopping mall. We don't go to the market. We buy clothes.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_on_sunday_we_go_to_the_shopping_mall_we_dont_go_to_5d9dc1fa.mp3",
  },
  {
    id: "q26",
    question: "What does the speaker drink?",
    options: ["soda", "ice tea", "lemonade", "water"],
    answer: "lemonade",
    text: "For lunch, I eat noodles and meat. I drink lemonade. I don't drink soda.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_for_lunch_i_eat_noodles_and_meat_i_drink_lemonade__48a1174b.mp3",
  },
  {
    id: "q27",
    question: "How does the speaker go to school?",
    options: ["by bike", "by bus", "on foot", "by taxi"],
    answer: "by bus",
    text: "I go to school by bus. My sister rides her bike. We don't walk to school.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_i_go_to_school_by_bus_my_sister_rides_her_bike_we__9491b621.mp3",
  },
  {
    id: "q28",
    question: "Where does the speaker live?",
    options: ["Australia", "the U.S.A", "Viet Nam", "Singapore"],
    answer: "Viet Nam",
    text: "I live in Viet Nam. I want to visit Australia. I don't live there.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_i_live_in_viet_nam_i_want_to_visit_australia_i_don_083cbfae.mp3",
  },
  {
    id: "q29",
    question: "What does the speaker do in free time?",
    options: ["watch TV", "cook", "play the guitar", "play soccer"],
    answer: "play the guitar",
    text: "In my free time, I play the guitar. I don't watch TV. I listen to music.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_in_my_free_time_i_play_the_guitar_i_dont_watch_tv__f91b06b6.mp3",
  },
  {
    id: "q30",
    question: "When does the game finish?",
    options: ["at 2 o'clock", "at 3 o'clock", "at 4 o'clock", "at 5 o'clock"],
    answer: "at 4 o'clock",
    text: "The game starts at 3 o'clock. It finishes at 4 o'clock. We go home after that.",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_the_game_starts_at_3_oclock_it_finishes_at_4_ocloc_3e321c1b.mp3",
  },

  // --- PART 4: TET BACK-UP (Q31 - Q40) ---
  {
    id: "q31",
    question:
      "We see many apricot blossoms at Tet in the South of Vietnam. What color are apricot blossoms usually?",
    options: ["Blue", "Yellow", "Purple", "Black"],
    answer: "Yellow",
    audio: "",
  },
  {
    id: "q32",
    question: "At Tet, children get lucky money in a ____.",
    options: ["white box", "school bag", "red envelope", "paper bag"],
    answer: "red envelope",
    audio: "",
  },
  {
    id: "q33",
    question:
      "Square glutinous rice cake is a traditional food at Tet. What is its Vietnamese name?",
    options: ["Bánh mì", "Bánh tét", "Bánh chưng", "Bánh bao"],
    answer: "Bánh chưng",
    audio: "",
  },
  {
    id: "q34",
    question: "People go to the flower market at Tet to ____.",
    options: ["buy books", "buy flowers", "buy clothes", "buy toys"],
    answer: "buy flowers",
    audio: "",
  },
  {
    id: "q35",
    question: "At Tet, families often visit ____.",
    options: [
      "teachers",
      "classmates",
      "relatives and friends",
      "neighbors only",
    ],
    answer: "relatives and friends",
    audio: "",
  },
  {
    id: "q36",
    question: "What does fireworks mean?",
    options: ["flowers", "lights in the sky", "food", "clothes"],
    answer: "lights in the sky",
    audio: "",
  },
  {
    id: "q37",
    question: "People go to the pagoda at Tet to ____.",
    options: ["play games", "pray for good luck", "buy food", "watch TV"],
    answer: "pray for good luck",
    audio: "",
  },
  {
    id: "q38",
    question: "Which sentence is correct?",
    options: [
      "I get lucky money in red envelope.",
      "I get lucky money on red envelope.",
      "I get lucky money from red envelope.",
      "I get lucky money in a red envelope.",
    ],
    answer: "I get lucky money in a red envelope.",
    audio: "",
  },
  {
    id: "q39",
    question: "Which activity is NOT for Tet?",
    options: [
      "Getting lucky money",
      "Watching fireworks",
      "Visiting relatives",
      "Taking an English exam",
    ],
    answer: "Taking an English exam",
    audio: "",
  },
  {
    id: "q40",
    question: "What do they do at night?",
    options: ["Eat cake", "Visit friends", "Watch fireworks", "Go to school"],
    answer: "Watch fireworks",
    text: "What do they do at night?",
    audio:
      "https://audio.senaai.tech/audio/Sena_Voice1_at_tet_my_family_eats_square_glutinous_rice_cake_w_b8193d8c.mp3",
  },
];
tdv_sdk.currentIndex = 0;
tdv_sdk.shuffleArray = function (Array) {
  for (let i = Array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [Array[i], Array[j]] = [Array[j], Array[i]];
  }
};
tdv_sdk.start = function () {
  tdv_sdk.currentIndex = 0;
  //tdv_sdk.shuffleArray(tdv_sdk.list);
  tdv_sdk.listquestion = [];
  tdv_sdk.totalquestion = 40;
  for (let i = 0; i < tdv_sdk.list.length; i++) {
    tdv_sdk.listquestion.push(tdv_sdk.list[i]);
  }
};
tdv_sdk.loadquestion = function () {
  tdv_sdk.curQuestion = tdv_sdk.listquestion[tdv_sdk.currentIndex];
  tdv_sdk.correctAnswer = tdv_sdk.curQuestion.answer;
  //tdv_sdk.shuffleArray(tdv_sdk.curQuestion.options);
};
tdv_sdk.getCurQuestion = function () {
  return tdv_sdk.curQuestion.question;
};
tdv_sdk.getCurOptions = function (index) {
  return tdv_sdk.curQuestion.options[index];
};
tdv_sdk.getCurCorrectOption = function () {
  return tdv_sdk.correctAnswer;
};
tdv_sdk.getCorrectCount = function () {
  return tdv_sdk.answers;
};
tdv_sdk.nextQuestion = function () {
  tdv_sdk.currentIndex++;
};
tdv_sdk.getCurIndex = function () {
  return tdv_sdk.currentIndex;
};
tdv_sdk.getTotalQuestion = function () {
  return tdv_sdk.list.length;
};
tdv_sdk.getId = function (index) {
  return tdv_sdk.list[index].id;
};
tdv_sdk.getAnswers = function (index) {
  return tdv_sdk.answers[index];
};
tdv_sdk.goHome = function () {
  window.location.href = "../";
};

// Biến lưu trữ đối tượng âm thanh hiện tại để có thể dừng/phát lại
tdv_sdk.currentAudioObj = null;

// Hàm phát audio của câu hỏi hiện tại
tdv_sdk.playCurrentAudio = function (speed) {
  // 0. Xử lý tham số tốc độ: Nếu không truyền hoặc truyền null/undefined thì mặc định là 1.0
  var playbackSpeed = speed !== undefined && speed !== null ? speed : 1.0;

  // 1. Lấy thông tin câu hỏi hiện tại
  var curQ = tdv_sdk.listquestion[tdv_sdk.currentIndex];

  // Kiểm tra xem có link audio không
  if (curQ && curQ.audio) {
    var audioUrl = curQ.audio;

    // 2. Nếu đang có audio nào chạy thì dừng lại trước khi phát cái mới
    tdv_sdk.stopAudio();

    // 3. Khởi tạo đối tượng Audio HTML5 mới
    tdv_sdk.currentAudioObj = new Audio(audioUrl);

    // ===> CẬP NHẬT: Thiết lập tốc độ đọc <===
    // Giá trị: 1.0 là bình thường, < 1.0 là chậm, > 1.0 là nhanh
    tdv_sdk.currentAudioObj.playbackRate = playbackSpeed;

    // 4. Bắt lỗi nếu trình duyệt chặn Autoplay hoặc link hỏng
    var playPromise = tdv_sdk.currentAudioObj.play();
    if (playPromise !== undefined) {
      playPromise
        .then(function () {
          // Audio bắt đầu phát thành công
          console.log(
            "Audio playing: " + audioUrl + " | Speed: " + playbackSpeed,
          );
        })
        .catch(function (error) {
          // Lỗi (thường do user chưa tương tác với game)
          console.error("Audio playback failed:", error);
        });
    }

    // (Tùy chọn) Lắng nghe sự kiện khi audio kết thúc
    tdv_sdk.currentAudioObj.onended = function () {
      console.log("Audio finished.");
      // Nếu muốn gọi lại Construct 2 khi hết audio, bạn có thể dùng:
      // c2_callFunction("OnAudioEnded");
    };
  } else {
    console.log("No audio found for this question.");
  }
};

// Hàm dừng audio (Dùng khi chuyển câu hỏi hoặc tắt tiếng)
tdv_sdk.stopAudio = function () {
  if (tdv_sdk.currentAudioObj) {
    tdv_sdk.currentAudioObj.pause();
    tdv_sdk.currentAudioObj.currentTime = 0; // Tua về đầu
    tdv_sdk.currentAudioObj = null;
  }
};

// Thêm vào cuối file tdv_sdk.js
tdv_sdk.isEssay = function () {
  // Nếu mảng options rỗng hoặc không tồn tại -> Là câu tự luận
  if (
    !tdv_sdk.curQuestion.options ||
    tdv_sdk.curQuestion.options.length === 0
  ) {
    return 1; // True (Trong C2 số 1 là true)
  }
  return 0; // False
};

// Kiểm tra xem câu hỏi hiện tại có audio hay không
tdv_sdk.hasAudio = function () {
  var q = tdv_sdk.curQuestion;
  // Kiểm tra nếu thuộc tính audio tồn tại và có độ dài > 0
  if (q && q.audio && q.audio.length > 0) {
    return 1; // Có audio -> Là câu nghe
  }
  return 0; // Không có -> Trắc nghiệm thường hoặc Tự luận
};
// Hàm tính toán scale đồng bộ cho cả 4 đáp án
tdv_sdk.getUniformAnswerScale = function () {
  var opts = tdv_sdk.curQuestion.options;

  // Safety check: Nếu không có options (câu tự luận) trả về 1
  if (!opts || opts.length === 0) return 1.0;

  // 1. Tìm độ dài của đáp án dài nhất (Max Length)
  var maxLen = 0;
  for (var i = 0; i < opts.length; i++) {
    if (opts[i].length > maxLen) {
      maxLen = opts[i].length;
    }
  }

  // 2. Tính toán Scale dựa trên Max Length
  // Công thức: Constant / maxLen
  // Giải thích: Với Scale chuẩn 1.1, giả sử hiển thị đẹp cho khoảng 11-12 ký tự.
  // Ta chọn hằng số khoảng 13.5.

  var calculatedScale = 11 / maxLen;

  // 3. Clamp (Kẹp) giá trị trong khoảng cho phép
  // Min: 0.55 (Bé quá không đọc được)
  // Max: 1.1 (Scale mặc định bạn mong muốn)

  if (calculatedScale > 1.2) calculatedScale = 1.2;
  if (calculatedScale < 0.7) calculatedScale = 0.7;

  return calculatedScale;
};
