using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoginManager : MonoSingleton<LoginManager>
{
    //账号列表
    [HideInInspector]
    public GameObject bigLoginItem;
    [HideInInspector]
    public GameObject smallLoginItem;

    public GameObject itemRoot;
    [HideInInspector]
    public List<GameObject> biglistItem = new List<GameObject>();
    [HideInInspector]
    public List<GameObject> smalllistItem = new List<GameObject>();

    //常驻列表
    [HideInInspector]
    public GameObject bigOftenUseLoginItem;
    [HideInInspector]
    public List<GameObject> OftenUsesmalllistItem = new List<GameObject>();
    [HideInInspector]
    public List<GameObject> OftenUsebiglistItem = new List<GameObject>();

    public GameObject OftenUseitemRoot;


    private void Start()
    {
        bigLoginItem = WindowsManager.Instance.GetPrefab(WindowConfig.windowType.Login,"bigLoginItem");
        smallLoginItem = WindowsManager.Instance.GetPrefab(WindowConfig.windowType.Login, "smallLoginItem");
        bigOftenUseLoginItem = WindowsManager.Instance.GetPrefab(WindowConfig.windowType.Login, "bigOftenUseLoginItem");

        Instance.removeAll();
        Instance.InitPlayer();
        Instance.initOftenUse();
    }

    public void ExitUser()
    {
        removeAll();
        InitPlayer();
        initOftenUse();
    }


    public void GotoLogin()
    {

        if (UserManager.Instance.NowChoose != null)
        {
            if (LoginPassWordManager.Instance.tmpField.text.Length > 4)
            {
                WindowsManager.Instance.show(WindowConfig.windowType.ProjectMain, true);
                WindowsManager.Instance.show(WindowConfig.windowType.Top, false);
                TopManager.Instance.root.SetActive(false);
                UserManager.Instance.MoveIcon(Vector3.zero, TopManager.Instance.icon.transform, () => { TopManager.Instance.root.SetActive(true); });
            }
            else
            {
                LoginPassWordManager.Instance.toError();
            }
        }
        else
        {
            WindowsManager.Instance.show(WindowConfig.windowType.Tip2, false, "请选择账号");
        }
    }

    public void initOftenUse()
    {
        
        LoginDataManager.Instance.GetOftenUseLoginData((List<AccountData> adata) => {
            GameObject smallitem;
            GameObject bigitem;
            //创建bigLoginItem
            bigitem = GameObject.Instantiate(bigOftenUseLoginItem, OftenUseitemRoot.transform);
            OftenUsebiglistItem.Add(bigitem);
            //创建smallLoginItem
            for (int j = 3; j >=0; j--)
            {
                if(adata.Count>j)
                {
                    smallitem = GameObject.Instantiate(smallLoginItem, bigitem.transform);
                    SmallLoginItem slitem =  smallitem.GetComponent<SmallLoginItem>();
                    slitem.setData(adata[j],false);
                    OftenUsebiglistItem.Add(smallitem);
                }
                else
                {
                    break;
                }
            }

        });
    }

    public void InitPlayer()
    {
        LoginDataManager.Instance.GetLoginData((List<AccountData> adata) => {
            GameObject smallitem;
            GameObject bigitem;
            //创建bigLoginItem
            for (int i = 0; i < (adata.Count / 10 + 1); i++)
            {
                bigitem = GameObject.Instantiate(bigLoginItem, itemRoot.transform);
                biglistItem.Add(bigitem);
                //创建smallLoginItem
                for (int j = i; j < i + 10; j++)
                {
                    if (adata.Count > j)
                    {
                        smallitem = GameObject.Instantiate(smallLoginItem, bigitem.transform);
                        SmallLoginItem slitem = smallitem.GetComponent<SmallLoginItem>();
                        slitem.setData(adata[j],true);
                        smalllistItem.Add(smallitem);
                    }
                    else
                    {
                        break;
                    }
                }
            }

        });

    }

    public void removeAll()
    {

        /*
        for (int i = 0; i < smalllistItem.Count; i++)
        {

        }*/
        for (int i = 0; i < biglistItem.Count; i++)
        {
            Destroy(biglistItem[i]);
        }
        for (int i = 0; i < OftenUsebiglistItem.Count; i++)
        {
            Destroy(OftenUsebiglistItem[i]);
        }
        smalllistItem.Clear();
        biglistItem.Clear();
        OftenUsebiglistItem.Clear();
        OftenUsesmalllistItem.Clear();
    }
}
