using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Cute;
using UnityEngine;

namespace Wizard;

public static class CardMasterLocalFileUtility
{
	private static readonly string CARDMASTER_DIRECTORY_NAME = "cardmaster";

	private static string CardMasterDirPath => Path.Combine(Application.persistentDataPath, CARDMASTER_DIRECTORY_NAME);

	private static string GetCardMasterFilePath(CardMaster.CardMasterId cardMasterId)
	{
		return Path.Combine(CardMasterDirPath, $"card_master_{(int)cardMasterId}");
	}

	public static string GetCardMasterHash()
	{
		string cardMasterFilePath = GetCardMasterFilePath(CardMaster.CardMasterId.Default);
		string result = string.Empty;
		try
		{
			if (!File.Exists(cardMasterFilePath))
			{
				return result;
			}
			using StreamReader streamReader = new StreamReader(cardMasterFilePath);
			result = CryptAES.decryptForNode(streamReader.ReadLine());
		}
		catch (Exception ex)
		{
			WriteClientInfoLog("CardMasater: \"" + cardMasterFilePath + "\" の読み込みに失敗\n" + ex);
			result = string.Empty;
		}
		return result;
	}

	public static bool ReadLocalCardMasterCSV(out Dictionary<CardMaster.CardMasterId, string> dictCardMasterCsv)
	{
		dictCardMasterCsv = new Dictionary<CardMaster.CardMasterId, string>();
		foreach (CardMaster.CardMasterId value in Enum.GetValues(typeof(CardMaster.CardMasterId)))
		{
			string cardMasterFilePath = GetCardMasterFilePath(value);
			if (!File.Exists(cardMasterFilePath))
			{
				continue;
			}
			try
			{
				using StreamReader streamReader = new StreamReader(cardMasterFilePath);
				if (value == CardMaster.CardMasterId.Default)
				{
					streamReader.ReadLine();
				}
				dictCardMasterCsv[value] = CryptAES.decryptForNode(streamReader.ReadToEnd());
			}
			catch (Exception ex)
			{
				WriteClientInfoLog("CardMasater: \"" + cardMasterFilePath + "\" の読み込みに失敗\n" + ex);
				CreateMasterReadErrorDialog();
				return false;
			}
		}
		return true;
	}

	private static void CreateMasterReadErrorDialog()
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(Data.SystemText.Get("System_0055"));
		dialogBase.SetText(Data.SystemText.Get("System_0056"));
		DeleteAllCardMasterLocalFile();
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BackToTitleBtn);
		dialogBase.SetFadeButtonEnabled(flag: false);
	}

	public static void WriteAllCardMasterLocalFile(Dictionary<CardMaster.CardMasterId, string> dictCardMasterCSV, string cardMasterHash)
	{
		DeleteAllCardMasterLocalFile();
		Directory.CreateDirectory(CardMasterDirPath);
		foreach (KeyValuePair<CardMaster.CardMasterId, string> item in dictCardMasterCSV)
		{
			CardMaster.CardMasterId key = item.Key;
			string value = item.Value;
			if (!(value == ""))
			{
				string text = null;
				if (key == CardMaster.CardMasterId.Default)
				{
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.AppendLine(CryptAES.encryptForNode(cardMasterHash));
					stringBuilder.AppendLine(CryptAES.encryptForNode(value));
					text = stringBuilder.ToString();
				}
				else
				{
					text = CryptAES.encryptForNode(value);
				}
				WriteCardMasterLocalFile(key, text);
			}
		}
	}

	private static void WriteCardMasterLocalFile(CardMaster.CardMasterId cardMasterId, string writeStr)
	{
		string cardMasterFilePath = GetCardMasterFilePath(cardMasterId);
		try
		{
			File.WriteAllText(cardMasterFilePath, writeStr);
		}
		catch (Exception ex)
		{
			WriteClientInfoLog("CardMasater: \"" + cardMasterFilePath + "\" の書き込みに失敗\n" + ex);
			DeleteAllCardMasterLocalFile();
		}
	}

	public static void DeleteAllCardMasterLocalFile()
	{
		try
		{
			if (Directory.Exists(CardMasterDirPath))
			{
				Directory.Delete(CardMasterDirPath, recursive: true);
			}
		}
		catch (Exception ex)
		{
			WriteClientInfoLog("CardMasaterディレクトリ: \"" + CardMasterDirPath + "\" の削除に失敗\n" + ex);
		}
	}

	private static void WriteClientInfoLog(string log)
	{
		LocalLog.AccumulateTraceLog(log);
	}
}
