using System;
using System.Collections.Generic;
using Cute;
using LitJson;
using UnityEngine;

namespace Wizard;

public class PlayerStaticData : MonoBehaviour
{
	public class UserTex
	{
		public enum Type
		{
			Emblem,
			RankIcon,
			Country
		}

		public Texture[] m_loadedList;

		public List<UITexture>[] m_requiredAttachList;

		public List<string> m_unloadList = new List<string>();

		private readonly Type m_type;

		private readonly int m_sizeDiffNum;

		public int m_loadingCnt;

		public Format Format;

		private List<string> _rankIconPathList = new List<string>();

		public UserTex(Type type)
		{
			m_type = type;
			m_sizeDiffNum = GetSizeDiffNum(m_type);
			m_loadedList = new Texture[m_sizeDiffNum];
			m_requiredAttachList = new List<UITexture>[m_sizeDiffNum];
			for (int i = 0; i < m_requiredAttachList.Length; i++)
			{
				m_requiredAttachList[i] = new List<UITexture>();
			}
		}

		public void LoadTexture()
		{
			ResourcesManager resourcesManager = Toolbox.ResourcesManager;
			List<string> pathList = null;
			if (m_type == Type.RankIcon)
			{
				pathList = GetRankTexturePath();
				for (int i = 0; i < m_sizeDiffNum; i++)
				{
					m_loadedList[i] = null;
				}
			}
			else
			{
				for (int j = 0; j < m_sizeDiffNum; j++)
				{
					m_loadedList[j] = null;
					m_loadingCnt++;
					string texturePath = GetTexturePath(GetTextureName(), j);
					if (pathList == null)
					{
						pathList = new List<string>();
					}
					pathList.Add(texturePath);
				}
			}
			resourcesManager.StartCoroutine_LoadAssetGroupSync(pathList, delegate
			{
				m_loadingCnt = 0;
				UpdateTexture();
				UnloadTexture();
				if (m_type == Type.RankIcon)
				{
					_rankIconPathList.AddRange(pathList);
				}
			});
		}

		private List<string> GetRankTexturePath()
		{
			List<string> list = new List<string>();
			GetRankTexturePathSub(Format.Unlimited, list);
			GetRankTexturePathSub(Format.Rotation, list);
			m_loadingCnt += list.Count;
			return list;
		}

		private static void GetRankTexturePathSub(Format format, List<string> list)
		{
			list.Add(Toolbox.ResourcesManager.GetAssetTypePath(UserRank(format).ToString("00"), ResourcesManager.AssetLoadPathType.RankIcon_S));
			list.Add(Toolbox.ResourcesManager.GetAssetTypePath(UserRank(format).ToString("00"), ResourcesManager.AssetLoadPathType.RankIcon_L));
		}

		public void ReLoadTexture(string oldId, string newId)
		{
			if (m_type != Type.RankIcon && !string.IsNullOrEmpty(oldId) && !m_unloadList.Contains(oldId))
			{
				m_unloadList.Add(oldId);
			}
			if (!string.IsNullOrEmpty(newId))
			{
				m_unloadList.Remove(newId);
				LoadTexture();
				return;
			}
			for (int i = 0; i < m_sizeDiffNum; i++)
			{
				m_loadedList[i] = null;
			}
		}

		public void AttachTexture(UITexture uiTexture, int size)
		{
			if (m_loadingCnt == 0)
			{
				uiTexture.mainTexture = m_loadedList[size];
			}
			else
			{
				m_requiredAttachList[size].Add(uiTexture);
			}
		}

		private void UpdateTexture()
		{
			ResourcesManager resourcesManager = Toolbox.ResourcesManager;
			for (int i = 0; i < m_sizeDiffNum; i++)
			{
				Texture mainTexture = (m_loadedList[i] = resourcesManager.LoadObject(GetTexturePath(GetTextureName(), i, isfetch: true)) as Texture);
				List<UITexture> list = m_requiredAttachList[i];
				foreach (UITexture item in list)
				{
					item.mainTexture = mainTexture;
				}
				list.Clear();
			}
		}

		private void UnloadTexture()
		{
			List<string> list = new List<string>();
			foreach (string unload in m_unloadList)
			{
				for (int i = 0; i < m_sizeDiffNum; i++)
				{
					list.Add(GetTexturePath(unload, i));
				}
			}
			m_unloadList.Clear();
			list.AddRange(_rankIconPathList);
			_rankIconPathList.Clear();
			Toolbox.ResourcesManager.RemoveAssetGroup(list);
		}

		private static int GetSizeDiffNum(Type type)
		{
			return type switch
			{
				Type.Emblem => 2, 
				Type.RankIcon => 2, 
				Type.Country => 2, 
				_ => 0, 
			};
		}

		private string GetTexturePath(string name, int sizeId, bool isfetch = false)
		{
			int num = 0;
			switch (m_type)
			{
			case Type.Emblem:
				num = 42;
				break;
			case Type.RankIcon:
				num = 49;
				break;
			case Type.Country:
				num = 51;
				break;
			}
			return Toolbox.ResourcesManager.GetAssetTypePath(name, (ResourcesManager.AssetLoadPathType)(num + sizeId), isfetch);
		}

		private string GetTextureName()
		{
			return m_type switch
			{
				Type.Emblem => UserEmblemID.ToString(), 
				Type.RankIcon => (GameMgr.GetIns().GetDataMgr().IsDipslayHighRankFormat() ? UserRankHighAllFormat() : UserRank(Format)).ToString("00"), 
				Type.Country => UserCountryCode, 
				_ => "", 
			};
		}
	}

	public enum EmblemTexSize
	{
		S,
		M,
		MAX
	}

	public enum RankTexSize
	{
		S,
		L,
		MAX
	}

	public enum CountryTexSize
	{
		S,
		M,
		MAX
	}

	public enum AgreementState
	{
		Agreed,
		NotAgree,
		Reset
	}

	public enum LootBoxType
	{
		GACHA,
		TWOPICK,
		SEALED,
		COLOSSEUM,
		COMPETITION,
		SPECIAL_CRYSTAL,
		MAX
	}

	public static AgreementState _tosAgreementState = AgreementState.NotAgree;

	public static AgreementState _privacyPolicyAgreementState = AgreementState.NotAgree;

	private static UserTex UserEmblemTexture = new UserTex(UserTex.Type.Emblem);

	private static UserTex UserCountryTexture = new UserTex(UserTex.Type.Country);

	private static UserTex UserRankTexture = new UserTex(UserTex.Type.RankIcon);

	public static TimeData UserTime = new TimeData();

	public static int UserViewerID => Certification.ViewerId;

	public static string UserName
	{
		get
		{
			if (Data.Load.data == null)
			{
				return "";
			}
			return Data.Load.data._userInfo.name;
		}
		set
		{
			Data.Load.data._userInfo.name = value;
		}
	}

	public static string UserRegionCode => PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.CURRENT_REGION_CODE);

	public static bool IsOfficialUser { get; set; }

	public static bool IsOfficialUserDisplay { get; set; }

	public static AgreementState KorAuthorityAgreementState { get; set; } = AgreementState.NotAgree;

	public static int UserCrystalCount
	{
		get
		{
			if (Data.Load.data == null)
			{
				return 0;
			}
			return Data.Load.data._userCrystalCount.total_crystal;
		}
		set
		{
			Data.Load.data._userCrystalCount.total_crystal = value;
		}
	}

	public static int UserRupyCount
	{
		get
		{
			if (Data.Load.data == null)
			{
				return 0;
			}
			return Data.Load.data._userCrystalCount.rupy;
		}
		set
		{
			Data.Load.data._userCrystalCount.rupy = value;
		}
	}

	public static int UserRedEtherCount
	{
		get
		{
			if (Data.Load.data == null)
			{
				return 0;
			}
			return Data.Load.data._userCrystalCount.red_ether;
		}
		set
		{
			Data.Load.data._userCrystalCount.red_ether = value;
		}
	}

	public static int UserSpotCardPointCount
	{
		get
		{
			if (Data.Load.data == null)
			{
				return 0;
			}
			return Data.Load.data._userCrystalCount._spotcardPoint;
		}
		set
		{
			Data.Load.data._userCrystalCount._spotcardPoint = value;
		}
	}

	public static int UserOrbNum
	{
		get
		{
			return GetItemNum(1000);
		}
		set
		{
			UpdateItemNum(1000, value);
		}
	}

	public static int UserChallengeTicketNum
	{
		get
		{
			return GetItemNum(1);
		}
		set
		{
			UpdateItemNum(1, value);
		}
	}

	public static int UserColosseumTicketNum
	{
		get
		{
			return GetItemNum(2);
		}
		set
		{
			UpdateItemNum(2, value);
		}
	}

	public static long UserEmblemID
	{
		get
		{
			return Data.Load.data._userInfo.selected_emblem_id;
		}
		set
		{
			UserInfo userInfo = Data.Load.data._userInfo;
			long selected_emblem_id = userInfo.selected_emblem_id;
			if (selected_emblem_id != value)
			{
				userInfo.selected_emblem_id = value;
				UserEmblemTexture.ReLoadTexture(selected_emblem_id.ToString(), value.ToString());
			}
		}
	}

	public static int UserDegreeID
	{
		get
		{
			return Data.Load.data._userInfo.selected_degree_id;
		}
		set
		{
			Data.Load.data._userInfo.selected_degree_id = value;
		}
	}

	public static string UserCountryCode
	{
		get
		{
			return Data.Load.data._userInfo.country_code;
		}
		set
		{
			UserInfo userInfo = Data.Load.data._userInfo;
			string country_code = userInfo.country_code;
			if (country_code != value)
			{
				userInfo.country_code = value;
				UserCountryTexture.ReLoadTexture(country_code, value);
			}
		}
	}

	public static bool UserPromotionFlag
	{
		get
		{
			return Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.is_promotion;
		}
		set
		{
			Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.is_promotion = value;
		}
	}

	public static int UserPromotionMatchCount
	{
		get
		{
			return Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.match_count;
		}
		set
		{
			Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.match_count = value;
		}
	}

	public static int UserPromotionWinCount
	{
		get
		{
			return Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.win;
		}
		set
		{
			Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.win = value;
		}
	}

	public static int UserPromotionLoseCount
	{
		get
		{
			return Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.lose;
		}
		set
		{
			Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.lose = value;
		}
	}

	public static string UserBirthDay
	{
		get
		{
			return Data.Load.data._userInfo.birth_day;
		}
		set
		{
			Data.Load.data._userInfo.birth_day = value;
		}
	}

	public static bool IsLootBoxRegulation(LootBoxType type)
	{
		return Data.Load.data.LootBoxReguration[(int)type];
	}

	public static bool IsPurchaseNotificationOfLootBox()
	{
		for (int i = 0; i < 6; i++)
		{
			if (IsLootBoxRegulation((LootBoxType)i))
			{
				return true;
			}
		}
		return false;
	}

	public static int GetItemNum(int itemId)
	{
		if (Data.Load.data._userItemDict == null)
		{
			return 0;
		}
		if (Data.Load.data._userItemDict.ContainsKey(itemId))
		{
			return Data.Load.data._userItemDict[itemId];
		}
		return 0;
	}

	public static void InitializeItemNum(int itemId, int num)
	{
		UpdateItemNum(itemId, num);
	}

	public static void UpdateItemNum(int itemId, int num)
	{
		if (Data.Load.data._userItemDict == null)
		{
			Data.Load.data._userItemDict = new Dictionary<int, int>();
		}
		if (Data.Load.data._userItemDict.ContainsKey(itemId))
		{
			Data.Load.data._userItemDict[itemId] = num;
		}
		else
		{
			Data.Load.data._userItemDict.Add(itemId, num);
		}
	}

	public static void Clear()
	{
		UserEmblemTexture = new UserTex(UserTex.Type.Emblem);
		UserCountryTexture = new UserTex(UserTex.Type.Country);
		UserRankTexture = new UserTex(UserTex.Type.RankIcon);
	}

	public static void LoadUserEmblemTexture()
	{
		UserEmblemTexture.LoadTexture();
	}

	public static void AttachUserEmblemTexture(UITexture uiTexture, EmblemTexSize size)
	{
		UserEmblemTexture.AttachTexture(uiTexture, (int)size);
	}

	public static void LoadUserCountryTexture()
	{
		if (!string.IsNullOrEmpty(UserCountryCode))
		{
			UserCountryTexture.LoadTexture();
		}
	}

	public static void AttachUserCountryTexture(UITexture uiTexture, CountryTexSize size)
	{
		UserCountryTexture.AttachTexture(uiTexture, (int)size);
	}

	public static int UserBattlePointCurrentFormat()
	{
		return UserBattlePoint(Data.CurrentFormat);
	}

	public static int UserBattlePoint(Format inFormat)
	{
		return Data.Load.data._userRank[(int)inFormat].battle_point;
	}

	public static int UserBattlePointHighFormat()
	{
		int val = UserBattlePoint(Format.Rotation);
		int val2 = UserBattlePoint(Format.Unlimited);
		return Math.Max(val, val2);
	}

	public static int UserMasterPointCurrentFormat()
	{
		return UserMasterPoint(Data.CurrentFormat);
	}

	public static int UserMasterPoint(Format inFormat)
	{
		return Data.Load.data._userRank[(int)inFormat].master_point;
	}

	public static int UserMasterPointHighAllFormat()
	{
		return UserMasterPoint(HighRankFormat());
	}

	public static Format HighRankFormat()
	{
		int num = UserRank(Format.Rotation);
		int num2 = UserRank(Format.Unlimited);
		if (num == num2)
		{
			int num3 = UserBattlePoint(Format.Rotation);
			int num4 = UserBattlePoint(Format.Unlimited);
			if (IsMasterRank(Format.Rotation))
			{
				num3 = UserMasterPoint(Format.Rotation);
				num4 = UserMasterPoint(Format.Unlimited);
			}
			if (num3 <= num4)
			{
				return Format.Unlimited;
			}
			return Format.Rotation;
		}
		if (num <= num2)
		{
			return Format.Unlimited;
		}
		return Format.Rotation;
	}

	public static int UserBattlePointEachRankCurrentFormat()
	{
		return UserBattlePointEachRank(Data.CurrentFormat);
	}

	public static int UserBattlePointEachRank(Format inFormat)
	{
		if (IsMasterRank(inFormat))
		{
			return Data.Load.data._userRank[(int)inFormat].grandMasterData.currentMasterPoint;
		}
		int num = ((!Data.Load.data.IsStartRank(inFormat, UserRank(inFormat))) ? Data.Load.data.GetRankInfo(inFormat, UserRank(inFormat) - 1).accumulate_point : 0);
		return UserBattlePoint(inFormat) - num;
	}

	public static int UserRankCurrentFormat()
	{
		return UserRank(Data.CurrentFormat);
	}

	public static int UserRank(Format inFormat)
	{
		if (inFormat == Format.PreRotation)
		{
			inFormat = Format.Rotation;
		}
		if (Data.Load.data == null)
		{
			return 0;
		}
		return Data.Load.data._userRank[(int)inFormat].rank;
	}

	public static int UserRankHighAllFormat()
	{
		int val = UserRank(Format.Rotation);
		int val2 = UserRank(Format.Unlimited);
		return Math.Max(val, val2);
	}

	public static void LoadUserRankTexture(Format inFormat)
	{
		UserRankTexture.Format = inFormat;
		UserRankTexture.LoadTexture();
	}

	public static void ReLoadUserRankTexture(string inOldId, string inNewId, Format inFormat)
	{
		UserRankTexture.Format = inFormat;
		UserRankTexture.ReLoadTexture(inOldId, inNewId);
	}

	public static void AttachUserRankTexture(UITexture uiTexture, RankTexSize size)
	{
		UserRankTexture.AttachTexture(uiTexture, (int)size);
	}

	public static bool IsMasterRankCurrentFormat()
	{
		return IsMasterRank(Data.CurrentFormat);
	}

	public static bool IsMasterRank(Format inFormat)
	{
		return Data.Load.data._userRank[(int)inFormat].is_master_rank;
	}

	public static bool IsGrandMasterRankCurrentFormat()
	{
		return IsGrandMasterRank(Data.CurrentFormat);
	}

	public static bool IsGrandMasterRank(Format inFormat)
	{
		return Data.Load.data._userRank[(int)inFormat].is_grand_master_rank;
	}

	public static bool IsMaxRank(Format inFormat)
	{
		List<RankInfo> rankInfoRawList = Data.Load.data.GetRankInfoRawList(inFormat);
		return UserRank(inFormat) >= rankInfoRawList[rankInfoRawList.Count - 1].RankId;
	}

	public static int UserNextRankExp(Format inFormat)
	{
		if (IsMasterRank(inFormat))
		{
			return Data.Load.data._userRank[(int)inFormat].grandMasterData.targetMasterPoint;
		}
		return Mathf.Max(0, Data.Load.data.GetRankInfo(inFormat, UserRank(inFormat)).necessary_point - UserBattlePointEachRank(inFormat));
	}

	public static bool UserPromotionIsWin(int matchNum)
	{
		return (Data.Load.data._userRank[(int)Data.CurrentFormat].user_promotion_match.battle_result >> matchNum) % 2 != 0;
	}

	public static void UpdateHaveUserGoodsNumByJsonData(JsonData userGoodsList)
	{
		for (int i = 0; i < userGoodsList.Count; i++)
		{
			JsonData jsonData = userGoodsList[i];
			UserGoods.Type type = (UserGoods.Type)jsonData["reward_type"].ToInt();
			long userGoodsId = jsonData["reward_id"].ToLong();
			int num = jsonData["reward_num"].ToInt();
			UpdateHaveUserGoodsNum(type, userGoodsId, num);
		}
	}

	public static void UpdateHaveUserGoodsNum(UserGoods.Type type, long userGoodsId, int num)
	{
		switch (type)
		{
		case UserGoods.Type.RedEther:
			UserRedEtherCount = num;
			break;
		case UserGoods.Type.Crystal:
			UserCrystalCount = num;
			if (MyPageMenu.Instance != null)
			{
				MyPageMenu.Instance.UpdateCrystalCount();
			}
			break;
		case UserGoods.Type.Item:
			UpdateItemNum((int)userGoodsId, num);
			break;
		case UserGoods.Type.Card:
		{
			int cardId = (int)userGoodsId;
			GameMgr.GetIns().GetDataMgr().UpdateUserOwnCardData(cardId, num);
			break;
		}
		case UserGoods.Type.SpotCard:
		case UserGoods.Type.SpotCardOnlyLatestCardPack:
		{
			int cardId = (int)userGoodsId;
			GameMgr.GetIns().GetDataMgr().SpotCardData.SetSpotCardNum(cardId, num);
			break;
		}
		case UserGoods.Type.Rupy:
			UserRupyCount = num;
			if (MyPageMenu.Instance != null)
			{
				MyPageMenu.Instance.UpdateRupyCount();
			}
			break;
		case UserGoods.Type.Skin:
			GameMgr.GetIns().GetDataMgr().GetCharaPrmBySkinId((int)userGoodsId)?.Acquired();
			break;
		case UserGoods.Type.Sleeve:
			Data.Master.SleeveMgr.Acquired(userGoodsId);
			break;
		case UserGoods.Type.Emblem:
			Data.Master.EmblemMgr.Acquired(userGoodsId);
			break;
		case UserGoods.Type.Degree:
			Data.Master.DegreeMgr.Acquired((int)userGoodsId);
			break;
		case UserGoods.Type.SpotCardPoint:
			UserSpotCardPointCount = num;
			break;
		case UserGoods.Type.MyPageBG:
			Data.Load.data.AcquiredMyPageBGList.Add(userGoodsId.ToString());
			break;
		case (UserGoods.Type)3:
		case UserGoods.Type.FreeGachaCount:
			break;
		}
	}

	public static int GetHaveUserGoods(UserGoods.Type type, long userGoodsId)
	{
		int result = 0;
		switch (type)
		{
		case UserGoods.Type.RedEther:
			result = UserRedEtherCount;
			break;
		case UserGoods.Type.Crystal:
			result = UserCrystalCount;
			break;
		case UserGoods.Type.Item:
			result = GetItemNum((int)userGoodsId);
			break;
		case UserGoods.Type.Rupy:
			result = UserRupyCount;
			break;
		case UserGoods.Type.SpotCardPoint:
			result = UserSpotCardPointCount;
			break;
		}
		return result;
	}
}
