using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Pretest : MonoBehaviour, IDataPresistence
{
    [Header("Daftar Pertanyaan")]
    public List<Questions> daftarPertanyaan;

    [Header("UI Komponen")]
    public List<TextMeshProUGUI> textPertanyaanList;
    public List<QuestionToggle> toggleJawabanList;

    public GameObject questionBox;

    public ToggleGroup toggleGroup;

    public List<int> collectedJawaban = new List<int>();

    public DataPresistenceManager presistenceManager;

    public TextMeshProUGUI pertanyaanKe;
    public int nomorPertanyaan = 1;

    private List<Questions> ambilUrutanPertanyaan;

    private int indexPertanyaan = 0;
    private int indexPertanyaanIncreament = 0;

    private bool hasShownQuestion = false;

    // Start is called before the first frame update
    void Start()
    {
        ambilUrutanPertanyaan = new List<Questions>(daftarPertanyaan);

        // Tampilkan pertanyaan dan jawaban (jawaban tidak diacak)
        for (int i = 0; i < textPertanyaanList.Count && i < ambilUrutanPertanyaan.Count; i++)
        {
            var pertanyaan = ambilUrutanPertanyaan[i];

            // Set teks pertanyaan
            textPertanyaanList[i].text = pertanyaan.pertanyaan;

            // Tidak mengacak jawaban
            var jawaban = pertanyaan.jawaban;

            // Set teks jawaban ke Toggle
            var toggleGroup = toggleJawabanList[i].toggles;
            for (int j = 0; j < toggleGroup.Count && j < jawaban.Count; j++)
            {
                toggleGroup[j].GetComponentInChildren<TextMeshProUGUI>().text = jawaban[j];
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (indexPertanyaan == indexPertanyaanIncreament && !hasShownQuestion)
        {
            pertanyaanKe.text = nomorPertanyaan+"/25";
            indexPertanyaanIncreament++;
            if (nomorPertanyaan < 25)
            {
                nomorPertanyaan++;
            }
            ShowQuestion(indexPertanyaan);
            hasShownQuestion = true;
        }
    }

    public void QuestionsBox()
    {
        questionBox.SetActive(true);
    }

    public void SubmitButton()
    {
        Toggle toggle = toggleGroup.ActiveToggles().FirstOrDefault();

        if (toggle != null)
        {
            string jawabanUser = toggle.GetComponentInChildren<TextMeshProUGUI>().text;

            // Cari pertanyaan aktif berdasarkan text pertanyaan
            string teksPertanyaanAktif = textPertanyaanList[0].text;
            Questions pertanyaanAktif = ambilUrutanPertanyaan
                .FirstOrDefault(q => q.pertanyaan == teksPertanyaanAktif);

            if (pertanyaanAktif != null)
            {
                // Contoh perbandingan jawaban
                if (jawabanUser == pertanyaanAktif.jawabanBenar)
                {
                    Debug.Log("Jawaban Benar: " + pertanyaanAktif.jawabanBenar + ", true");
                    collectedJawaban.Add(1);
                }
                else
                {
                    Debug.Log("Jawaban Benar: " + pertanyaanAktif.jawabanBenar + ", false");
                    collectedJawaban.Add(2);
                }
            }
            else
            {
                Debug.LogWarning("Pertanyaan aktif tidak ditemukan!");
            }

            indexPertanyaan++;
            hasShownQuestion = false;
            questionBox.SetActive(false);
        }
        else
        {
            hasShownQuestion = true;
            questionBox.SetActive(true);
        }
    }

    public void ShowQuestion(int index)
    {
        if (index < ambilUrutanPertanyaan.Count)
        {
            var pertanyaan = ambilUrutanPertanyaan[index];
            questionBox.SetActive(true);

            // Set text pertanyaan
            textPertanyaanList[0].text = pertanyaan.pertanyaan;

            // Set jawaban ke Toggle
            var jawaban = pertanyaan.jawaban;
            var toggleGroup = toggleJawabanList[0].toggles;

            foreach (var toggle in toggleGroup)
            {
                toggle.isOn = false;
            }

            for (int j = 0; j < toggleGroup.Count && j < jawaban.Count; j++)
            {
                toggleGroup[j].GetComponentInChildren<TextMeshProUGUI>().text = jawaban[j];
            }
        }
        else
        {
            presistenceManager.FinishQuitGame();
            SceneManager.LoadScene("PretestMenu");
        }
    }

    public void QuitPretest()
    {
        SceneManager.LoadScene("MainMenu");
    }

    public void LoadData(GameData data) {}

    public void SaveData(ref GameData data)
    {
        for (int i = 0; i < collectedJawaban.Count && i < data.jawabanPretest.Length; i++)
        {
            data.jawabanPretest[i] = collectedJawaban[i];
        }
    }
}
