var tdv_sdk = {};

tdv_sdk.list = [
    { 
        "id": "1", 
        "name": "dog", 
        "image": "https://images.senaai.vn/images/dog4.jpg",
        "audio": "https://audio.senaai.vn/audio/en_female_1_dog.mp3"
    },
    { 
        "id": "2", 
        "name": "cat", 
        "image": "https://images.senaai.vn/images/cat_1.jpg",
        "audio": "https://audio.senaai.vn/audio/en_female_1_cat.mp3"
    },
    { 
        "id": "3", 
        "name": "cow", 
        "image": "https://images.senaai.vn/images/cow1.jpg",
        "audio": "https://audio.senaai.vn/audio/en_female_1_cow.mp3"
    },
    { 
        "id": "4", 
        "name": "fish", 
        "image": "https://images.senaai.vn/images/blue_fish1.jpg",
        "audio": "https://audio.senaai.vn/audio/en_female_1_fish.mp3"
    },
    { 
        "id": "5", 
        "name": "bird", 
        "image": "https://images.senaai.vn/images/brown_bird1.jpg",
        "audio": "https://audio.senaai.vn/audio/en_female_1_bird.mp3"
    },
    { 
        "id": "6", 
        "name": "duck", 
        "image": "https://images.senaai.vn/images/duck2.jpg",
        "audio": "https://audio.senaai.vn/audio/en_female_1_duck.mp3"
    }
];

tdv_sdk.max_question = 5; // CẤU HÌNH SỐ LƯỢNG CÂU HỎI TẠI ĐÂY

tdv_sdk.gameList = [];
tdv_sdk.currentLevel = 0;
tdv_sdk.score = 0;

tdv_sdk.shuffleArray = function(array) {
    var newArray = array.slice();
    for (let i = newArray.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [newArray[i], newArray[j]] = [newArray[j], newArray[i]];
    }
    return newArray;
};

tdv_sdk.start = function() {
    tdv_sdk.currentLevel = 0;
    tdv_sdk.score = 0;
    tdv_sdk.gameList = [];

    var shuffledList = tdv_sdk.shuffleArray(tdv_sdk.list);
    var selectedItems = shuffledList.slice(0, tdv_sdk.max_question);

    selectedItems.forEach(function(item) {
        var isPair = Math.random() < 0.5;
        var questionObj = {
            image: item.image,
            audio_image: item.audio,
            is_pair: isPair
        };

        if (isPair) {
            questionObj.text = item.name;
            questionObj.audio_text = item.audio;
        } else {
            var distractor;
            do {
                distractor = tdv_sdk.list[Math.floor(Math.random() * tdv_sdk.list.length)];
            } while (distractor.id === item.id);

            questionObj.text = distractor.name;
            questionObj.audio_text = distractor.audio;
        }
        tdv_sdk.gameList.push(questionObj);
    });
};

tdv_sdk.getCurrentLevel = function() {
    return tdv_sdk.currentLevel + 1;
};

tdv_sdk.getTotalQuestion = function() {
    return tdv_sdk.max_question;
};

tdv_sdk.getCurImage = function() {
    if (tdv_sdk.gameList[tdv_sdk.currentLevel]) {
        return tdv_sdk.gameList[tdv_sdk.currentLevel].image;
    }
    return "";
};

tdv_sdk.getCurText = function() {
    if (tdv_sdk.gameList[tdv_sdk.currentLevel]) {
        return tdv_sdk.gameList[tdv_sdk.currentLevel].text;
    }
    return "";
};

tdv_sdk.isPair = function() {
    if (tdv_sdk.gameList[tdv_sdk.currentLevel]) {
        return tdv_sdk.gameList[tdv_sdk.currentLevel].is_pair ? 1 : 0;
    }
    return 0;
};

tdv_sdk.playAudio = function(url) {
    if (window.audio && !window.audio.paused) {
        window.audio.pause();
    }
    window.audio = new Audio(url);
    window.audio.play();
};

tdv_sdk.playAudioImage = function() {
    if (tdv_sdk.gameList[tdv_sdk.currentLevel]) {
        tdv_sdk.playAudio(tdv_sdk.gameList[tdv_sdk.currentLevel].audio_image);
    }
};

tdv_sdk.playAudioText = function() {
    if (tdv_sdk.gameList[tdv_sdk.currentLevel]) {
        tdv_sdk.playAudio(tdv_sdk.gameList[tdv_sdk.currentLevel].audio_text);
    }
};

tdv_sdk.nextLevel = function() {
    if (tdv_sdk.currentLevel < tdv_sdk.max_question) {
        tdv_sdk.currentLevel++;
    }
};

tdv_sdk.addScore = function() {
    tdv_sdk.score++;
};

tdv_sdk.getScore = function() {
    return tdv_sdk.score;
};