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


public class PlayerPrefsMgr : BaseMgr<PlayerPrefs>
{
    public void SaveData(string Keyname, object data)
    {
        //拿到data的Type信息
        Type dataType = data.GetType();

        //拿到data里面的字段信息
        FieldInfo[] infos = dataType.GetFields();

        #region 存储字段信息

        string saveKeyName = string.Empty;

        FieldInfo savedatainfo = null;

        for (int i = 0; i < infos.Length; i++)
        {
            //拿到单一字段信息
            savedatainfo = infos[i];

            //PlayerPrefs里面的Key的内容
            saveKeyName = Keyname + "_" + dataType.Name + "_" + savedatainfo.FieldType + "_" + savedatainfo.Name;

            SaveValue(saveKeyName, savedatainfo.GetValue(data));
        }

        #endregion
    }

    private void SaveValue(string Keyname, object Value) 
    {
        Type type = Value.GetType();

        if (type == typeof(int))
        {
            PlayerPrefs.SetInt(Keyname, (int)Value);
        }
        else if (type == typeof(float))
        {
            PlayerPrefs.SetFloat(Keyname, (float)Value);
        }
        else if (type == typeof(string))
        {
            PlayerPrefs.SetString(Keyname, Value.ToString());
        }
        else if (type == typeof(bool))
        {
            PlayerPrefs.SetInt(Keyname, (bool)Value ? 1 : 0);
        }
        else if (typeof(IList).IsAssignableFrom(type))
        {
            IList listdata = Value as IList;

            PlayerPrefs.SetInt(Keyname, listdata.Count);

            for (int i = 0; i < listdata.Count; i++)
            {
                SaveValue(Keyname + "_" + i, listdata[i]);
            }
        }
        else if (typeof(IDictionary).IsAssignableFrom(type))
        {
            IDictionary dicdata = Value as IDictionary;
            PlayerPrefs.SetInt(Keyname, dicdata.Count);

            int index = 0;
            foreach (var item in dicdata.Keys)
            {
                SaveValue(Keyname + "_Key_" + index, item);
                SaveValue(Keyname + "_Value_" + index, dicdata[item]);
                index++;
            }
        }
        else
        {
            SaveData(Keyname, Value);
        }
    }

    public object LoadData(string Keyname, Type type)
    {
        object data = Activator.CreateInstance(type);

        FieldInfo[] infos = type.GetFields();

        #region 读取字段信息

        string getKeyName = string.Empty;

        FieldInfo getdatainfo = null;

        for (int i = 0; i < infos.Length; i++)
        {
            //拿到单一字段信息
            getdatainfo = infos[i];

            //PlayerPrefs里面的Key的内容
            getKeyName = Keyname + "_" + type.Name + "_" + getdatainfo.FieldType + "_" + getdatainfo.Name;

            //给字段赋值
            getdatainfo.SetValue(data, LoadValue(getKeyName, getdatainfo.FieldType));
        }

        #endregion

        return data;
    }

    private object LoadValue(string Keyname, Type type) 
    {
        if (type == typeof(int))
        {
            return PlayerPrefs.GetInt(Keyname, 0);
        }
        else if (type == typeof(float))
        {
            return PlayerPrefs.GetFloat(Keyname, 0);
        }
        else if (type == typeof(string))
        {
            return PlayerPrefs.GetString(Keyname, string.Empty);
        }
        else if (type == typeof(bool))
        {
            return PlayerPrefs.GetInt(Keyname) == 1;
        }
        else if (typeof(IList).IsAssignableFrom(type))
        {
            int count = PlayerPrefs.GetInt(Keyname,0);

            IList datalist = Activator.CreateInstance(type) as IList;

            for (int i = 0; i < count; i++)
            {
                datalist.Add(LoadValue(Keyname + "_" + i, type.GetGenericArguments()[0]));
            }

            return datalist;
        }
        else if (typeof(IDictionary).IsAssignableFrom(type))
        {
            int count = PlayerPrefs.GetInt(Keyname, 0);

            IDictionary datadic = Activator.CreateInstance(type) as IDictionary;

            for (int i = 0; i < count; i++)
            {
                object dicKey = LoadValue(Keyname + "_Key_" + i, type.GetGenericArguments()[0]);
                object dicValue = LoadValue(Keyname + "_Value_" + i, type.GetGenericArguments()[1]);
                datadic[dicKey] = dicValue;
            }

            return datadic;
        }
        else
        {
            return LoadData(Keyname, type);
        }
    }
}
