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

[RequireComponent(typeof(UIPanel))]
public class MaidTransferLoadMain : WfFadeBehaviour
{
	public void Awake()
	{
		this.pageTabPanel = UTY.GetChildObject(base.gameObject, "SaveAndLoadPanel/PageGroup/PageButtonParent", false).GetComponent<UIWFTabPanel>();
		UIWFTabButton[] componentsInChildren = this.pageTabPanel.GetComponentsInChildren<UIWFTabButton>(true);
		foreach (UIWFTabButton uiwftabButton in componentsInChildren)
		{
			EventDelegate.Add(uiwftabButton.onSelect, new EventDelegate.Callback(this.OnSelectPage));
		}
		GameObject childObject = UTY.GetChildObject(base.gameObject, "SaveAndLoadPanel/DataViewer/DataUnitParent", false);
		UIButton[] componentsInChildren2 = childObject.GetComponentsInChildren<UIButton>(true);
		foreach (UIButton uibutton in componentsInChildren2)
		{
			EventDelegate.Add(uibutton.onClick, new EventDelegate.Callback(this.OnSelectDataUnit));
		}
		this.saveDataPath = GameMain.Instance.CMSystem.CM3D2Path;
		if (!string.IsNullOrEmpty(this.saveDataPath))
		{
			this.saveDataPath += "\\SaveData";
			if (!Directory.Exists(this.saveDataPath))
			{
				this.saveDataPath = string.Empty;
			}
		}
		long num = 0L;
		if (!string.IsNullOrEmpty(this.saveDataPath))
		{
			string[] files = Directory.GetFiles(this.saveDataPath, "*.save");
			foreach (string text in files)
			{
				int saveFileNameToSaveNo = this.GetSaveFileNameToSaveNo(text);
				if (saveFileNameToSaveNo >= 0)
				{
					GameMain.SerializeHeader saveDataHeader = this.GetSaveDataHeader(text);
					this.saveDataHeaderDic.Add(saveFileNameToSaveNo, new KeyValuePair<string, GameMain.SerializeHeader>(text, saveDataHeader));
					long num2 = 0L;
					if (long.TryParse(saveDataHeader.strSaveTime, out num2) && num < num2)
					{
						num = num2;
						this.newSaveDataNo = saveFileNameToSaveNo;
					}
				}
			}
		}
	}

	public void Start()
	{
		this.panel = base.GetComponent<UIPanel>();
		this.panel.alpha = 0f;
	}

	public void Call(Action onEventFinishCall)
	{
		if (this.newSaveDataNo < 0 || 100 <= this.newSaveDataNo)
		{
			this.pageTabPanel.Select(this.pageTabPanel.transform.GetChild(0).GetComponentInChildren<UIWFTabButton>());
		}
		else
		{
			int index = this.newSaveDataNo / 10;
			this.pageTabPanel.Select(this.pageTabPanel.transform.GetChild(index).GetComponentInChildren<UIWFTabButton>());
		}
		this.onEventFinishCall = onEventFinishCall;
		WfFadeJob.Create(this, this.mainUi, this.fadeTime, iTween.EaseType.easeOutSine);
	}

	public void Close()
	{
		WfFadeJob.Create(this.mainUi, this, this.fadeTime, iTween.EaseType.easeOutSine);
	}

	public override void OnUpdateFadeIn(float val)
	{
		this.panel.alpha = val;
	}

	public override void OnUpdateFadeOut(float val)
	{
		this.panel.alpha = val;
	}

	public override void OnCompleteFadeIn()
	{
		base.OnCompleteFadeIn();
		if (this.onEventFinishCall != null)
		{
			this.onEventFinishCall();
		}
	}

	private void OnSelectPage()
	{
		if (!UIWFSelectButton.current.isSelected)
		{
			return;
		}
		string[] array = UIWFSelectButton.current.transform.parent.name.Split(new char[]
		{
			'_'
		});
		NDebug.Assert(array.Length == 2, "ページボタンの名前が不正です");
		int num = 0;
		if (!int.TryParse(array[1], out num))
		{
			NDebug.Assert("ページボタンのint.Parseに失敗しました", false);
		}
		this.UpdateDataUnit(num - 1);
	}

	private void UpdateDataUnit(int selectedPage)
	{
		this.selectedPage = selectedPage;
		Transform transform = UTY.GetChildObject(base.gameObject, "SaveAndLoadPanel/DataViewer/DataUnitParent", false).transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			int num = selectedPage * 10 + i;
			GameObject gameObject = transform.GetChild(i).gameObject;
			if (!this.saveDataHeaderDic.ContainsKey(num))
			{
				UTY.GetChildObject(gameObject, "NewLabel", false).SetActive(false);
				UTY.GetChildObject(gameObject, "Content", false).SetActive(false);
				gameObject.GetComponent<UIButton>().isEnabled = false;
			}
			else
			{
				GameMain.SerializeHeader value = this.saveDataHeaderDic[num].Value;
				UTY.GetChildObject(gameObject, "NewLabel", false).SetActive(this.newSaveDataNo == num);
				GameObject childObject = UTY.GetChildObject(gameObject, "Content", false);
				childObject.SetActive(true);
				gameObject.GetComponent<UIButton>().isEnabled = true;
				UILabel component = UTY.GetChildObject(childObject, "DataInfo/Date", false).GetComponent<UILabel>();
				component.text = value.strSaveTime;
				UILabel component2 = UTY.GetChildObject(childObject, "DataInfo/LapsedDays/Number", false).GetComponent<UILabel>();
				component2.text = value.nGameDay.ToString();
				UILabel component3 = UTY.GetChildObject(childObject, "DataInfo/Manager/Name", false).GetComponent<UILabel>();
				component3.text = value.strPlayerName;
				UILabel component4 = UTY.GetChildObject(childObject, "DataInfo/NumberOfEmployees/Number", false).GetComponent<UILabel>();
				component4.text = value.nMaidNum.ToString();
				GameObject childObject2 = UTY.GetChildObject(childObject, "Comment/InputField/Label", false);
				UILabel component5 = childObject2.GetComponent<UILabel>();
				component5.text = value.strComment;
			}
		}
	}

	private void OnSelectDataUnit()
	{
		if (this.selectedPage < 0)
		{
			return;
		}
		string[] array = UIButton.current.transform.name.Split(new char[]
		{
			'_'
		});
		NDebug.Assert(array.Length == 2, "データユニットの名前が不正です");
		int num = 0;
		if (!int.TryParse(array[1], out num))
		{
			NDebug.Assert("データユニットのint.Parseに失敗しました", false);
		}
		num--;
		if (!this.saveDataHeaderDic.ContainsKey(this.selectedPage * 10 + num))
		{
			return;
		}
		UIButton.current.SetState(UIButtonColor.State.Normal, true);
		string key = this.saveDataHeaderDic[this.selectedPage * 10 + num].Key;
		this.transferMain.Call(key);
	}

	private int GetSaveFileNameToSaveNo(string filePath)
	{
		string text = Path.GetFileName(filePath);
		if (Path.GetExtension(text) != Path.GetExtension(".save"))
		{
			return -1;
		}
		text = Path.GetFileNameWithoutExtension(text);
		if (text.Length != 11)
		{
			return -1;
		}
		int num = text.IndexOf("SaveData");
		if (num != 0)
		{
			return -1;
		}
		int result = -1;
		if (!int.TryParse(text.Substring(8), out result))
		{
			return -1;
		}
		return result;
	}

	private GameMain.SerializeHeader GetSaveDataHeader(string filePath)
	{
		GameMain.SerializeHeader serializeHeader = null;
		if (File.Exists(filePath))
		{
			using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					string a = binaryReader.ReadString();
					NDebug.Assert(a == "CM3D2_SAVE", "セーブデータファイルのヘッダーが不正です。");
					int nVer = binaryReader.ReadInt32();
					serializeHeader = new GameMain.SerializeHeader();
					serializeHeader.strSaveTime = binaryReader.ReadString();
					serializeHeader.nGameDay = binaryReader.ReadInt32();
					serializeHeader.strPlayerName = binaryReader.ReadString();
					serializeHeader.nMaidNum = binaryReader.ReadInt32();
					serializeHeader.strComment = binaryReader.ReadString();
					serializeHeader.nVer = nVer;
					serializeHeader.lHeaderSize = binaryReader.BaseStream.Position;
				}
			}
		}
		return serializeHeader;
	}

	[SerializeField]
	public float fadeTime;

	[SerializeField]
	private WfFadeBehaviour mainUi;

	[SerializeField]
	private MaidTransferMain transferMain;

	private UIPanel panel;

	private string saveDataPath = string.Empty;

	private UIWFTabPanel pageTabPanel;

	private Action onEventFinishCall;

	private Dictionary<int, KeyValuePair<string, GameMain.SerializeHeader>> saveDataHeaderDic = new Dictionary<int, KeyValuePair<string, GameMain.SerializeHeader>>();

	private int newSaveDataNo = -1;

	private int selectedPage = -1;
}