using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;
using UnityEngine.UI;

public class TabListManager : MonoBehaviour
{
    public GameObject Texiao;
    public List<GameObject> Tablist;
    public InfoWindow infoWindow;
    public static TabListManager Instance;
    public GameObject Fgo;
    public GameObject Sgo;
    public GameObject Tgo;
    public DeMaConfig dc;

   // public Text windowTop;

    public List<GameObject> golist;

    public Animator animator;
    public void Awake()
    {
        Instance=this;
        Fgo.SetActive(false);
        Sgo.SetActive(false);
        Tgo.SetActive(false);
        
        // 设置首选语言为英语
        SetLanguage("en");
    }


 StringTable stringTable ;
    // 通过 Key 获取对应的语言文本
    public string GetTextByKey(string key)
    {
        if(stringTable==null)
            stringTable=  LocalizationSettings.StringDatabase.GetTable("DeMa");

        // 获取 TableEntry
        var tableEntry = stringTable.GetEntry(key);
        if (tableEntry != null)
        {
            // 返回翻译后的文本
            return tableEntry.GetLocalizedString();
        }
        else
        {
            return key;
        }
    }
    public void SetLanguage(string languageCode)
    {
        // 遍历所有可用的语言
        foreach (var locale in LocalizationSettings.AvailableLocales.Locales)
        {
            if (locale.Identifier.Code == languageCode)
            {
                LocalizationSettings.SelectedLocale = locale;
                Debug.Log("Language set to: " + languageCode);
                return;
            }
        }

        Debug.LogError("Language not found: " + languageCode);
    }
    public void Start()
    {
        TableReference tableReference = "DeMa";
        for (int i = 0;i<dc.TaskList.Count;i++)
        {
            GameObject fgo = GameObject.Instantiate(Fgo,Fgo.transform.parent);
            fgo.name = GetTextByKey( dc.TaskList[i].key);
            fgo.SetActive(true);
            FItemGo fg= new FItemGo();
            fg.info = dc.TaskList[i].info;
            fg.url = dc.TaskList[i].imgandmp4;

            fg.showList=new List<GameObject>();
            //fgo.GetComponentInChildren<Text>().text =  GetTextByKey( dc.TaskList[i].name);
            LocalizedText tt= fgo.GetComponentInChildren<Text>().gameObject.AddComponent<LocalizedText>();
            tt.localizedString= new LocalizedString();
            tt.localizedString.TableReference = tableReference;
            tt.changeKey(dc.TaskList[i].key); // 设置你的 Key
            for (int p=0;p<dc.TaskList[i].goidx.Count;p++)
            {
                fg.showList.Add(golist[dc.TaskList[i].goidx[p]]);
            }
            
            fg.Idx = i;
            fg.go = fgo;
            fg.init();
            fgoList.Add(fg);
            for (int j = 0;j<dc.TaskList[i].slist.Count;j++)
            {

                GameObject sgo = GameObject.Instantiate(Sgo,Sgo.transform.parent);
                sgo.name =GetTextByKey(  dc.TaskList[i].slist[j].key);;
                sgo.SetActive(false);
                SItemGo sg= new SItemGo();
                sg.tab =Tablist[ dc.TaskList[i].slist[j].tabidx];
                sg.info =  dc.TaskList[i].slist[j].info;
                sg.url =  dc.TaskList[i].slist[j].imgandmp4;

                if(sg.tab)
                {
                    Debug.Log(" sg.tab===>"+ sg.tab.name);
                    //fgo.GetComponentInChildren<Text>().text =  GetTextByKey( dc.TaskList[i].name);
                    LocalizedText ttt= sg.tab.GetComponentInChildren<Text>().gameObject.AddComponent<LocalizedText>();
                    ttt.localizedString= new LocalizedString();
                    ttt.localizedString.TableReference = tableReference;
                    ttt.changeKey(dc.TaskList[i].slist[j].key); // 设置你的 Key

                }
                sg.showList=new List<GameObject>();
                for (int p=0;p<dc.TaskList[i].slist[j].goidx.Count;p++)
                {
                    sg.showList.Add(golist[dc.TaskList[i].slist[j].goidx[p]]);
                }
                sg.Idx = j;
                sg.go = sgo;
                sg.init();
               // sgo.GetComponentInChildren<Text>().text =GetTextByKey(  dc.TaskList[i].slist[j].name);
                LocalizedText tt2= sgo.GetComponentInChildren<Text>().gameObject.AddComponent<LocalizedText>();
                tt2.localizedString= new LocalizedString();
                tt2.localizedString.TableReference = tableReference;
                tt2.changeKey( dc.TaskList[i].slist[j].key); // 设置你的 Key
                fgoList[i].slist.Add(sg);
                for (int z = 0;z<dc.TaskList[i].slist[j].tlist.Count;z++)
                {

                    GameObject tgo = GameObject.Instantiate(Tgo,Tgo.transform.parent);
                    tgo.name = GetTextByKey(dc.TaskList[i].slist[j].tlist[z].key);
                    tgo.SetActive(false);
                    TItemGo tg= new TItemGo();
                    tg.application = dc.TaskList[i].slist[j].tlist[z].application;
                    tg.title = dc.TaskList[i].slist[j].tlist[z].key;
                    tg.Idx = dc.TaskList[i].slist[j].tlist[z].index;
                    tg.info =  dc.TaskList[i].slist[j].tlist[z].info;
                    tg.info2 =  dc.TaskList[i].slist[j].tlist[z].info2;
                    tg.url =  dc.TaskList[i].slist[j].tlist[z].imgandmp4;
                    tg.showList=golist[dc.TaskList[i].slist[j].tlist[z].goidx];

                    try
                    {
                    if(tg.showList.GetComponent<ModelXuHua>().TabGo!=null)
                    {
                        LocalizedText tt4 = tg.showList.GetComponent<ModelXuHua>().TabGo.GetComponentInChildren<Text>().gameObject.AddComponent<LocalizedText>();

                        tt4.localizedString= new LocalizedString();
                        tt4.localizedString.TableReference = tableReference;
                        tt4.changeKey(dc.TaskList[i].slist[j].tlist[z].key); // 设置你的 Key

                    }

                    }catch
                    {

                    }
                    tg.go = tgo;
                    tg.init();
                   // tgo.GetComponentInChildren<Text>().text =GetTextByKey(dc.TaskList[i].slist[j].tlist[z].name);
                    
                    LocalizedText tt3= tgo.GetComponentInChildren<Text>().gameObject.AddComponent<LocalizedText>();
                    tt3.localizedString= new LocalizedString();
                    tt3.localizedString.TableReference = tableReference;
                    tt3.changeKey(dc.TaskList[i].slist[j].tlist[z].key); // 设置你的 Key
                    fgoList[i].slist[j].tlist.Add(tg);
                }

            }

        }
        //StartCoroutine(Dh());

        TabListManager.Instance.infoWindow.gameObject.SetActive(true);
        TabListManager.Instance.infoWindow.init("300001","","",0,"301001","320001");
    }   
    bool isstart=true;
    private IEnumerator Dh()
    {
        int idx=0;
        while (isstart)
        {
            yield return new WaitForSeconds(60f);
            if(idx==0)
                golist[1].GetComponent<ModelXuHua>().hide();
            else
            {
                golist[0].GetComponent<ModelXuHua>().hide();
            }
            idx++;
            yield return new WaitForSeconds(2f);
            if(idx>1)
            {
                yield return new WaitForSeconds(30f);
                idx=0;
                golist[0].GetComponent<ModelXuHua>().show();
                yield return new WaitForSeconds(0.5f);
                golist[0].GetComponent<ModelXuHua>().changemode();
                yield return new WaitForSeconds(30f);
                golist[1].GetComponent<ModelXuHua>().show();
                yield return new WaitForSeconds(0.5f);
                golist[1].GetComponent<ModelXuHua>().changemode();
            }
        }
    }
    int nowFindx=-1;
    public void showF(int index,bool isF=true)
    {
        for (int i=0;i<Tablist.Count;i++)
        {
            Tablist[i].SetActive(false);
        }
        TabListManager.Instance.infoWindow.gameObject.SetActive(true);
        TabListManager.Instance.infoWindow.init("300001","","",0,"301001","320001");
        if(nowFindx==index&&isF)
        {
            nowFindx=-1;
            isstart=true;
            //StartCoroutine(Dh());
            showAllModel();
        }
        else
        {
            isstart=false;
            StopAllCoroutines();
            nowFindx=index;
            for (int i=0;i<fgoList[nowFindx].slist.Count;i++)
            {
                fgoList[nowFindx].slist[i].go.SetActive(true);
            }
            closeAllModel();
        
            for (int j=0;j< fgoList[nowFindx].showList.Count;j++)
            {
                fgoList[nowFindx].showList[j].GetComponent<ModelXuHua>().showModel();
            }

        }
    }

    public void showT(int index)
    {
        TabListManager.Instance.infoWindow.gameObject.SetActive(false);
        showF(nowFindx,false);
        for (int i=0;i<fgoList[nowFindx].slist[index].tlist.Count;i++)
        {
            fgoList[nowFindx].slist[index].tlist[i].go.SetActive(true);
        }
        for (int i=0;i<Tablist.Count;i++)
        {
            Tablist[i].SetActive(false);
        }
        fgoList[nowFindx].slist[index].tab.SetActive(true);
    }

    public void closeAllModel()
    {
        for (int i=0;i<golist.Count;i++)
        {
            if(golist[i]&&golist[i].GetComponent<ModelXuHua>())
            golist[i].GetComponent<ModelXuHua>().showBaiMo();
        }
    }
    public void showAllModel()
    {
        for (int i=0;i<golist.Count;i++)
        {
            golist[i].GetComponent<ModelXuHua>().showModel(false);
            
        }
        for (int i=0;i<Tablist.Count;i++)
        {
            Tablist[i].SetActive(false);
        }
        Texiao.SetActive(false);
        animator.enabled=true;
    }

    public void showInfo(GameObject go)
    {
       // windowTop.transform.parent.parent.gameObject.SetActive(true);
      //  windowTop.text = go.GetComponentInChildren<Text>().text;
    }

    public GameObject TabGoList;
    public void showTab(GameObject go)
    {
        TabGoList.SetActive(false);
        for (int i=0;i<TabGoList.GetAllChildren().Count;i++)
        {
            TabGoList.transform.GetChild(i).gameObject.SetActive(false);
        }
        if(go.GetComponent<ModelXuHua>().TabGo)
        go.GetComponent<ModelXuHua>().TabGo.SetActive(true);
       // windowTop.transform.parent.parent.gameObject.SetActive(true);
      //  windowTop.text = go.GetComponentInChildren<Text>().text;
        for (int i=0;i<Tablist.Count;i++)
        {
            Tablist[i].SetActive(false);
        }
    }
    public void closeTab()
    {

        TabGoList.SetActive(false);
    }

    public void clossAll()
    {

      //  windowTop.transform.parent.parent.gameObject.SetActive(false);
        for (int i = 0;i<fgoList.Count;i++)
        {
            for (int j = 0;j<fgoList[i].slist.Count;j++)
            {
                fgoList[i].slist[j].go.SetActive(false);
                for (int z = 0;z<fgoList[i].slist[j].tlist.Count;z++)
                {
                   fgoList[i].slist[j].tlist[z].go.SetActive(false);
                }

            }

        }
    }

    List<FItemGo> fgoList  = new List<FItemGo>();

    [System.Serializable]
    public class FItemGo
    {
        public string info;
        public string url;
        public int Idx;
        public GameObject go;
        public List<SItemGo> slist= new List<SItemGo>();
        public List<GameObject> showList =new List<GameObject>();

        public void init()
        {
            go.GetComponent<Button>().onClick.AddListener(()=>{
                TabListManager.Instance.clossAll();
                TabListManager.Instance.showF(Idx);
                    Debug.Log("TabListManager.Instance.nowFindx"+TabListManager.Instance.nowFindx);
                if( TabListManager.Instance.nowFindx!=-1)
                {
                    TabListManager.Instance.animator.Play("Take 001", 0, 0f);
                    TabListManager.Instance.animator.speed =0;
                    TabListManager.Instance.showInfo(go);
                    TabListManager.Instance.closeAllModel();
                
                    for (int j=0;j< showList.Count;j++)
                    {
                    showList[j].GetComponent<ModelXuHua>().showModel();
                    }

                }
                else
                {
                    TabListManager.Instance.animator.Play("Take 001", 0, 0f);
                    TabListManager.Instance.animator.speed =1;

                }
                    TabListManager.Instance.closeTab();
                
                TabListManager.Instance.Texiao.SetActive(false);

            });
        }
    }
    public class SItemGo
    {
        public GameObject tab;
        public string info;
        public string url;
        public int Idx;
        public GameObject go;
        public List<TItemGo> tlist= new List<TItemGo>();

        public List<GameObject> showList =new List<GameObject>();
        public void init()
        {
            go.GetComponent<Button>().onClick.AddListener(()=>{
                TabListManager.Instance.clossAll();
                TabListManager.Instance.showT(Idx);
                TabListManager.Instance.showInfo(go);

                TabListManager.Instance.closeAllModel();
            
                for (int j=0;j< showList.Count;j++)
                {
                   showList[j].GetComponent<ModelXuHua>().showModel();
                }
                TabListManager.Instance.closeTab();
                TabListManager.Instance.infoWindow.gameObject.SetActive(false);
                TabListManager.Instance.Texiao.SetActive(false);
                    TabListManager.Instance.animator.Play("Take 001", 0, 0f);
                    TabListManager.Instance.animator.speed =0;
            });
            
        }
    }
    public class TItemGo
    {
        public string info;
        public string info2;
        public string url;
        public string title;
        public string application;
        public int Idx;
        public GameObject go;

        public GameObject showList =new GameObject();
        public void init()
        {
            
            go.GetComponent<Button>().onClick.AddListener(()=>{
                TabListManager.Instance.showInfo(go);
                TabListManager.Instance.closeAllModel();
                showList.GetComponent<ModelXuHua>().showModel();
                TabListManager.Instance.showTab(showList);
                TabListManager.Instance.infoWindow.gameObject.SetActive(true);
                TabListManager.Instance.infoWindow.init(info,url,info2,Idx,title,application);
                TabListManager.Instance.Texiao.SetActive(true);
                    TabListManager.Instance.animator.Play("Take 001", 0, 0f);
                    TabListManager.Instance.animator.speed =0;

            });
        }
    }
}