using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
namespace ChillConnect
{
    public class SaveLocalData
    {
        public static SaveLoacalobject saveobject;
        public static SaveLoacalobject GetSaveLocalObject()
        {
            if (saveobject == null)
            {
                if (File.Exists(jsonFilePath))
                {
                    string jsonstr = File.ReadAllText(jsonFilePath);
                    saveobject = JsonConvert.DeserializeObject<SaveLoacalobject>(jsonstr);
                    return saveobject;
                }
                else
                {
                    saveobject = new SaveLoacalobject();
                    return saveobject;
                }
            }
            else return saveobject;
        }

        private static string jsonFilePath = Path.Combine(Application.persistentDataPath, "SaveLocalData1.json");

        public static void saveLocalDataFunc()
        {
            if (saveobject == null) return;
            string save = JsonConvert.SerializeObject(saveobject);
            if (save == null || save == string.Empty) return;
            if (File.Exists(jsonFilePath)) File.Delete(jsonFilePath);
            File.WriteAllText(jsonFilePath, save);

        }
        public static void clearRankData()
        {
            if (File.Exists(jsonFilePath)) File.Delete(jsonFilePath);
            File.WriteAllText(jsonFilePath, null);
            saveobject = new SaveLoacalobject();
            saveLocalDataFunc();
        }
    }

    public class SaveLoacalobject
    {
        public List<UserData> users = new List<UserData>(); 
        // public UserData selfUser = new UserData();
        public List<RankRewardData> rankRewardData = new List<RankRewardData>();
    }
}
