

using LitJson;
using SC.XR.Unity;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoginDataManager:Singleton<LoginDataManager>
{

    string[] testAclist =new string[] { "戴广军","曹宇","刘晟","王玉洁", "戴广军", "曹宇", "刘晟", "王玉洁", "戴广军", "曹宇", "刘晟", "王玉洁", "戴广军", "曹宇", "刘晟", "王玉洁", "戴广军", "曹宇", "刘晟", "王玉洁" };
    List<AccountData> ldata;

    //获取 账号登录的列表
    public void GetLoginData(Action<List<AccountData>> callback)
    {
        ldata = new List<AccountData>();
        AccountData adata;
        for (int i = 0; i < testAclist.Length; i++)
        {
            adata = new AccountData();
            adata.account = testAclist[i];
            adata.name = testAclist[i];
            adata.iconUrl = "/icon/"+i+".png";
            ldata.Add(adata);
        }
        WindowsManager.Instance.StartCoroutine(testBack(callback));
    }


    IEnumerator testBack(Action<List<AccountData>> callback)
    {
        yield return null;
        callback.Invoke(ldata);
    }


    List<AccountData> OftenUseldata;
    //获取常用账号列表

    public void GetOftenUseLoginData(Action<List<AccountData>> callback)
    {
        OftenUseldata = new List<AccountData>();
        JsonData data = getOftenUseAcc();
        if(data.IsArray)
        {

            AccountData adata;
            for (int i = data.Count-1; i >=0 ; i--)
            {
                adata = new AccountData();
                adata.account = data[i]["account"].ToString();
                adata.name = data[i]["name"].ToString();
                adata.iconUrl = data[i]["iconUrl"].ToString();
                adata.password = data[i]["password"].ToString();
                OftenUseldata.Add(adata);
            }
        }
        WindowsManager.Instance.StartCoroutine(testOftenUseBack(callback));
    }


    int checkData(JsonData adata)
    {
        JsonData data = getOftenUseAcc();

        if (data.IsArray)
        {

            for (int i = data.Count - 1; i >= 0; i--)
            {
                if(adata["account"].ToString() == data[i]["account"].ToString())
                {
                    return i;
                }
            }
        }
        return -1;
    }
    IEnumerator testOftenUseBack(Action<List<AccountData>> callback)
    {
        yield return null;
        callback.Invoke(OftenUseldata);
    }

    //添加常驻账号
    public void setOftenUseAcc(AccountData adata)
    {
        JsonData data = getOftenUseAcc();
        JsonData accData = new JsonData();
        accData["name"] = adata.name;
        accData["password"] = adata.password;
        accData["account"] = adata.account;
        accData["iconUrl"] = adata.iconUrl;
        int index = checkData(accData);
        
            JsonData accDataNew = new JsonData();
            accDataNew.Add(accData);
            for (int i = 0; i < data.Count; i++)
            {
                if(index!=i)
                    accDataNew.Add(data[i]);
                if (accDataNew.Count >= 4)
                    break;
            }
            PlayerPrefs.SetString("OftenUse", accDataNew.ToJson());
            PlayerPrefs.Save();
     
    }

    //获取常驻账号
    public JsonData getOftenUseAcc()
    {
        string OftenUse = PlayerPrefs.GetString("OftenUse");
        JsonData data = new JsonData();
        if (OftenUse != "" && OftenUse != null)
        {
            try
            {
                data = JsonMapper.ToObject(OftenUse);

            }
            catch
            {

            }
        }
        return data;
    }
}
public class AccountData
{
    public string name = "";
    public string password = "";
    public string account = "";
    public string iconUrl = "";
}
