using System.Collections.Generic;
using System.IO;
using IgnoreOPS;
using SGModule.DataStorage;
using UnityEngine;

namespace ChillConnect
{
    public class SaveData
    {
        private static Saveobject _saveObject;
        public static Saveobject GetSaveObject()
        {
            if (_saveObject == null)
            {
                // if (File.Exists(jsonFilePath))
                // {
                //     string jsonstr = File.ReadAllText(jsonFilePath);
                //     _saveObject = JsonConvert.DeserializeObject<Saveobject>(jsonstr);
                //     return _saveObject;
                // }
                // else
                // {
                //     _saveObject = new Saveobject();
                //     return _saveObject;
                // }

                _saveObject = DataManager.Instance.LoadData(DataKeys.SaveObject, new Saveobject());
            }


            return _saveObject;
        }

        private static RankData _rankdata;
        public static RankData GetRankData()
        {
            if (_rankdata == null)
            {
                _rankdata = DataManager.Instance.LoadData(DataKeys.RankData, new RankData());
            }
            return _rankdata;
        }
        public static void SaveRankFunc(bool mustSave = false)
        {
            DataManager.Instance.SaveData(DataKeys.RankData, _rankdata);
        }
        private static string jsonFilePath = Path.Combine(Application.persistentDataPath, "SaveData1.json");

        public static void saveDataFunc()
        {

            // string save = JsonConvert.SerializeObject(saveobject);
            //
            // if (File.Exists(jsonFilePath)) File.Delete(jsonFilePath);
            // File.WriteAllText(jsonFilePath, save);
            DataManager.Instance.SaveData(DataKeys.SaveObject, _saveObject);

        }
        public static bool battlepassred()
        {

            int pass_level = GameHelper.GetBattleLv();
            List<int> freelist = GetSaveObject().battle_pass_freelist;
            for (int i = 1; i <= ConfigSystem.GetConfig<PassportrewardsModel>().DataList.Count; i++)
            {
                if (pass_level >= i)
                {
                    if (!freelist.Contains(i))
                    {
                        return true;
                    }
                }

            }
            if (GetSaveObject().is_get_battlepass)
            {
                List<int> paylist = GetSaveObject().battle_pass_paylist;
                for (int i = 1; i <= ConfigSystem.GetConfig<PassportrewardsModel>().DataList.Count; i++)
                {
                    if (pass_level >= i)
                    {
                        if (!paylist.Contains(i))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public static void AddAllLevelRewardsToLevelReward()
        {
            int maxLevel = GameHelper.GetLevel() / 10 * 10;
            var passingTaskModel = ConfigSystem.GetConfig<PassingTaskModel>();
            var saveObject = GetSaveObject();

            foreach (var task in passingTaskModel.DataList)
            {
                if (task.tol_num < maxLevel && !saveObject.level_reward.Contains(task.tol_num))
                {
                    saveObject.level_reward.Add(task.tol_num);
                }
            }
        }

        public static int getAllLevelAward()
        {
            int all_reward = 0;
            int maxLevel = GameHelper.GetLevel() / 10 * 10;
            foreach (var task in ConfigSystem.GetConfig<PassingTaskModel>().DataList)
            {
                // 检查当前任务的等级是否小于 maxLevel
                if (task.tol_num < maxLevel && GameHelper.GetLevel() % 10 != 0)
                {
                    if (!GetSaveObject().level_reward.Contains(task.tol_num))
                    {
                        all_reward += task.reward_num;
                    }
                }
            }

            return all_reward;
        }


        public static int getLevelreward(int level)
        {
            int levelreward = 0;
            foreach (var task in ConfigSystem.GetConfig<PassingTaskModel>().DataList)
            {
                if (GameHelper.GetLevel() > task.tol_num)
                {
                    if (level == task.tol_num && !GetSaveObject().level_reward.Contains(level))
                    {
                        levelreward = task.reward_num;
                        break;
                    }
                }
            }

            return levelreward;
        }

        public static bool passtaskred()
        {
            List<int> list = GetSaveObject().pass_task_record;
            for (int i = 0; i < ConfigSystem.GetConfig<PassingTaskModel>().DataList.Count; i++)
            {
                if (GameHelper.GetLevel() > ConfigSystem.GetConfig<PassingTaskModel>().DataList[i].tol_num)
                {
                    if (!list.Contains(ConfigSystem.GetConfig<PassingTaskModel>().DataList[i].id)) return true;
                }
            }
            return false;
        }
        public static bool timetaskred()
        {
            List<int> list = GetSaveObject().time_task_record;
            for (int i = 0; i < ConfigSystem.GetConfig<DurationtasksModel>().DataList.Count; i++)
            {
                if (GameHelper.GetGameTime() >= ConfigSystem.GetConfig<DurationtasksModel>().DataList[i].tol_num * 60)
                {
                    if (!list.Contains(ConfigSystem.GetConfig<DurationtasksModel>().DataList[i].id)) return true;
                }
            }
            return false;
        }

        public static bool AdTaskred()
        {
            List<int> list = GetSaveObject().ad_task_record;

            for (int i = 0; i < ConfigSystem.GetConfig<ADTaskModel>().DataList.Count; i++)
            {
                if (DataMgr.VideoWatchCount.Value >= ConfigSystem.GetConfig<ADTaskModel>().DataList[i].tol_num)
                {
                    if (!list.Contains(ConfigSystem.GetConfig<ADTaskModel>().DataList[i].id)) return true;
                }
            }
            return false;
        }
        public static bool getRed()
        {

            return battlepassred();
        }
        public static float pay_time = 0;
        public static float redeem_time = 0;



    }

    public class Saveobject
    {

        public bool is_get_battlepass = false;
        public List<int> battle_pass_freelist = new List<int>();
        public List<int> battle_pass_paylist = new List<int>();

        public int[] _goldtime;
        public List<int> gold_shop_time = new List<int>();
        public bool have_slot;
        public int battle_pass_time;
        public int _watch_ad_cd;

        public List<int> pass_task_record = new List<int>();
        public List<int> time_task_record = new List<int>();
        public List<int> ad_task_record = new List<int>();
        public List<int> level_reward = new List<int>();

        /// <summary>
        /// 每一局使用道具数量（[0]:out [1]:back [2]:refresh）
        /// </summary>
        public int[] usePropsNum = new int[3];
        /// <summary>
        /// 卡牌层数
        /// </summary>
        public int card_layer;
        /// <summary>
        /// 卡牌总数量
        /// </summary>
        public int all_card_numbers;


        public int login_day;
        public int login_hour;
        public int ch_people;
        public float ch_people_1;
        public float ch_people_2;

        public bool is_get_packreward;

        public bool is_get_removead;
        public bool is_autopack_show;
        public int is_autopack_show_day;

        public long start_time;
        public int clear_number;
        public int crush_egg_time;

        public int this_time_cardtype;

        public int[] dark_Dayref;

        public ApplePayClass max_pay_object;
        public List<SaveingPotClass> saveingpot_history = new List<SaveingPotClass>();
        public float saveingpot_ch;
        public float last_saveingpot_ch;

        public long failed_pack_time;
        public int three_gift_got_index = 1;
        public long last_got_three_gift_time = 0;
        public bool is_get_ThreeDaysGift;
        public int addview_off_time;
        public int game_fail_number;
        public int game_fail_off_number;

        public long lastLoginTime = 0;
        public int remove_ad_time;

        public bool IsGetFirstReward;

        #region Game data
        public int OutPropNum;
        public int BackPropNum;
        public int RefreshPropNum;
        public int LevelNum = 1;
        public int LevelState = 3;
        public int GameDay = 0;
        public int GameExp = 0;
        public int ChLv = 0;
        public int ExchangeModeToggle = 1;
        public int chout_lv;
        public int TurnOffNumbers;
        public int TurnOffDay;
        public long TurnOffTime;
        public int LargeRewardNum;
        public int InterstitialPLayNum;
        public int GameTime;

        public long first_login_time = 0;
        public int[] TodayOpenNum = new int[2] { 0, 0 };

        public int ExchangeProcessMode = -1;
        public int CHProcessMode = -1;
        #endregion
    }
    public class RankData
    {
        public List<UserData> users = new List<UserData>();
        public UserData selfUser = new UserData();
        public List<RankRewardData> rankRewardData = new List<RankRewardData>();
    }
}
