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

namespace Kasizuki
{
	public class KasizukiSaveAndLoadCtrl : NGUIWindow
	{
		private void Start()
		{
			if (this.m_IsStarted)
			{
				return;
			}
			this.SetUpSavePanel();
			this.SetUpLoadPanel();
			this.SetUpOtherButtons();
			this.SetUpDataUnits();
			this.m_NowOpenPage = SaveAndLoadMgr.PageNo.Page_1;
			this.m_IsStarted = true;
		}

		private void SetUpSavePanel()
		{
			this.m_CacheSaveObjects = new ObjectCacheDic();
			this.m_CacheSaveObjects.CacheChildObject<Component>(base.gameObject, "TitleGroup/TitleSave", "SavePanel");
			UIButton childObject = this.m_CacheSaveObjects.GetChildObject<UIButton>(base.gameObject, "DisplayButtonGroup/DisplayLoad");
			this.m_CacheSaveObjects.AddCache<UIButton>("LoadButton", childObject);
			EventDelegate.Add(childObject.onClick, delegate()
			{
				this.Open(SaveAndLoadMgr.ViewType.Load, true);
			});
		}

		private void SetUpLoadPanel()
		{
			this.m_CacheLoadObjects = new ObjectCacheDic();
			this.m_CacheLoadObjects.CacheChildObject<Component>(base.gameObject, "TitleGroup/TitleLoad", "LoadPanel");
			UIButton childObject = this.m_CacheLoadObjects.GetChildObject<UIButton>(base.gameObject, "DisplayButtonGroup/DisplaySave");
			this.m_CacheLoadObjects.AddCache<UIButton>("SaveButton", childObject);
			EventDelegate.Add(childObject.onClick, delegate()
			{
				this.Open(SaveAndLoadMgr.ViewType.Save, true);
			});
		}

		private void SetUpOtherButtons()
		{
			this.m_CacheButtons = new ObjectCacheDic();
			UIButton uibutton = this.m_CacheButtons.CacheChildObject<UIButton>(base.gameObject, "Cancel", "Cancel");
			EventDelegate.Add(uibutton.onClick, new EventDelegate.Callback(this.OnClickCancel));
			this.m_PageButtonDic = new Dictionary<SaveAndLoadMgr.PageNo, SaveAndLoadCtrl.PageButton>();
			this.m_InactiveColor = (this.m_ActiveColor = this.m_ListPageButtons.tempItem.GetComponent<UIWidget>().color);
			this.m_ActiveColor.a = 1f;
			this.m_ListPageButtons.Show<UIButton>(5, delegate(int index, UIButton button)
			{
				int num = index + 1;
				UISprite component = button.GetComponent<UISprite>();
				if (component != null)
				{
					component.spriteName = string.Format("{0}{1:00}", "cm3d2_save_load_page", num);
				}
				SaveAndLoadCtrl.PageButton buttonPage = new SaveAndLoadCtrl.PageButton();
				buttonPage.pageNo = (SaveAndLoadMgr.PageNo)Enum.Parse(typeof(SaveAndLoadMgr.PageNo), num.ToString());
				buttonPage.btnPageNo = button;
				buttonPage.selectCursor = button.transform.Find("SelectCursor").gameObject;
				this.m_PageButtonDic.Add(buttonPage.pageNo, buttonPage);
				EventDelegate.Add(button.onClick, delegate()
				{
					this.OnClickPageButton(buttonPage.pageNo);
				});
			});
		}

		private void SetUpDataUnits()
		{
			this.m_ListDataUnits.tempItem.AddComponent<KasizukiSaveAndLoadCtrl.DataUnitComp>();
			this.m_ListDataUnits.Show<KasizukiSaveAndLoadCtrl.DataUnitComp>(16, delegate(int index, KasizukiSaveAndLoadCtrl.DataUnitComp dataUnit)
			{
				dataUnit.Init(index);
				UIButton component = dataUnit.GetComponent<UIButton>();
				if (index == 0)
				{
					EventDelegate.Add(component.onClick, delegate()
					{
						this.OnClickNewGameUnit(dataUnit);
					});
				}
				else
				{
					EventDelegate.Add(component.onClick, delegate()
					{
						this.OnClickDataUnit(dataUnit);
					});
				}
				UIInput inputComment = dataUnit.inputComment;
				EventDelegate.Add(inputComment.onChange, delegate()
				{
					this.OnChangeDataUnitComment(dataUnit);
				});
				EventDelegate.Add(inputComment.onSubmit, new EventDelegate.Callback(inputComment.RemoveFocus));
			});
		}

		public void Open(SaveAndLoadMgr.ViewType viewType, bool isLatestPageOpen = false)
		{
			if (!this.m_IsStarted)
			{
				this.Start();
			}
			if (isLatestPageOpen)
			{
				int pageNumberLatestData = this.GetPageNumberLatestData();
				if (pageNumberLatestData > 0)
				{
					this.m_NowOpenPage = (SaveAndLoadMgr.PageNo)pageNumberLatestData;
				}
				else
				{
					this.m_NowOpenPage = SaveAndLoadMgr.PageNo.Page_1;
				}
			}
			if (viewType == SaveAndLoadMgr.ViewType.Save)
			{
				this.OpenSave();
			}
			else if (viewType == SaveAndLoadMgr.ViewType.Load)
			{
				this.OpenLoad();
			}
			else
			{
				NDebug.Assert("セーブロードのUIを開くときの種類に不正な値が入りました", false);
				Debug.LogError("セーブロードのUIを開くときの種類に不正な値が入りました");
			}
			this.m_NowViewType = viewType;
			this.OnClickPageButton(this.m_NowOpenPage);
		}

		private void OpenSave()
		{
			foreach (KeyValuePair<string, UnityEngine.Object> keyValuePair in this.m_CacheLoadObjects.Dic)
			{
				Component component = (Component)keyValuePair.Value;
				if (component && component.gameObject)
				{
					component.gameObject.SetActive(false);
				}
				else
				{
					Debug.LogWarningFormat("m_CacheLoadObjects の中に GameObject 以外のものが入っている\nキャッシュ名：{0}", new object[]
					{
						keyValuePair.Key
					});
				}
			}
			foreach (KeyValuePair<string, UnityEngine.Object> keyValuePair2 in this.m_CacheSaveObjects.Dic)
			{
				Component component2 = (Component)keyValuePair2.Value;
				if (component2 && component2.gameObject)
				{
					component2.gameObject.SetActive(true);
				}
				else
				{
					Debug.LogWarningFormat("m_CacheSaveObjects の中に GameObject 以外のものが入っている\nキャッシュ名：{0}", new object[]
					{
						keyValuePair2.Key
					});
				}
			}
		}

		private void OpenLoad()
		{
			foreach (KeyValuePair<string, UnityEngine.Object> keyValuePair in this.m_CacheSaveObjects.Dic)
			{
				Component component = (Component)keyValuePair.Value;
				if (component && component.gameObject)
				{
					component.gameObject.SetActive(false);
				}
				else
				{
					Debug.LogWarningFormat("m_CacheSaveObjects の中に GameObject 以外のものが入っている\nキャッシュ名：{0}", new object[]
					{
						keyValuePair.Key
					});
				}
			}
			foreach (KeyValuePair<string, UnityEngine.Object> keyValuePair2 in this.m_CacheLoadObjects.Dic)
			{
				Component component2 = (Component)keyValuePair2.Value;
				if (component2 && component2.gameObject)
				{
					component2.gameObject.SetActive(true);
				}
				else
				{
					Debug.LogWarningFormat("m_CacheLoadObjects の中に GameObject 以外のものが入っている\nキャッシュ名：{0}", new object[]
					{
						keyValuePair2.Key
					});
				}
			}
		}

		private void UpdateDataUnits()
		{
			if (this.m_NowViewType == SaveAndLoadMgr.ViewType.Save)
			{
				GameObject[] itemArray = this.m_ListDataUnits.ItemArray;
				for (int i = 0; i < itemArray.Length; i++)
				{
					int saveDataIndex = i + (this.m_NowOpenPage - SaveAndLoadMgr.PageNo.Page_1) * itemArray.Length;
					KasizukiManager.SaveDataHeader saveDataHeader = GameMain.Instance.KasizukiMgr.GetSaveDataHeader(saveDataIndex);
					KasizukiSaveAndLoadCtrl.DataUnitComp component = itemArray[i].GetComponent<KasizukiSaveAndLoadCtrl.DataUnitComp>();
					this.SetDataUnitAtHeader(component, saveDataHeader);
					UIButton component2 = component.GetComponent<UIButton>();
					if (i != 0)
					{
						component2.isEnabled = true;
					}
					else
					{
						component2.isEnabled = false;
					}
				}
			}
			else if (this.m_NowViewType == SaveAndLoadMgr.ViewType.Load)
			{
				GameObject[] itemArray2 = this.m_ListDataUnits.ItemArray;
				for (int j = 0; j < itemArray2.Length; j++)
				{
					int saveDataIndex2 = j + (this.m_NowOpenPage - SaveAndLoadMgr.PageNo.Page_1) * itemArray2.Length;
					KasizukiManager.SaveDataHeader saveDataHeader2 = GameMain.Instance.KasizukiMgr.GetSaveDataHeader(saveDataIndex2);
					KasizukiSaveAndLoadCtrl.DataUnitComp component3 = itemArray2[j].GetComponent<KasizukiSaveAndLoadCtrl.DataUnitComp>();
					this.SetDataUnitAtHeader(component3, saveDataHeader2);
					UIButton component4 = component3.GetComponent<UIButton>();
					if (j != 0)
					{
						component4.isEnabled = (saveDataHeader2 != null);
					}
					else
					{
						component4.isEnabled = true;
					}
				}
			}
			else
			{
				NDebug.Assert("セーブロードのUIを更新するときの種類に不正な値が入っていました", false);
			}
		}

		private void SetDataUnitAtHeader(KasizukiSaveAndLoadCtrl.DataUnitComp dataUnit, KasizukiManager.SaveDataHeader header)
		{
			NDebug.Assert(dataUnit != null, "セーブデータの項目にnullが指定されました");
			if (header != null)
			{
				dataUnit.content.SetActive(true);
				dataUnit.newLabel.SetActive(GameMain.Instance.KasizukiMgr.LatestSaveDataNumber == header.saveDataNumber);
				int lastManType = header.lastManType;
				ManData.Data data = ManData.GetData(lastManType);
				dataUnit.labelDate.text = header.GetDateTime().ToString("yyyy.MM.dd HH:mm");
				dataUnit.labelMaidCount.text = KasizukiManager.GetEnableMaidList().Count.ToString();
				dataUnit.labelManName.text = data.drawName;
				dataUnit.labelUsedCount.text = header.playCount.ToString();
				dataUnit.inputComment.value = header.strComment;
			}
			else
			{
				dataUnit.content.SetActive(false);
				dataUnit.newLabel.SetActive(false);
			}
			if (dataUnit.index == 0)
			{
				dataUnit.newGameLabel.SetActive(true);
			}
			else
			{
				dataUnit.newGameLabel.SetActive(false);
			}
		}

		private int GetPageNumberLatestData()
		{
			int latestSaveDataNumber = GameMain.Instance.KasizukiMgr.LatestSaveDataNumber;
			return latestSaveDataNumber / 16 + 1;
		}

		private void OnClickPageButton(SaveAndLoadMgr.PageNo pageNo)
		{
			this.m_NowOpenPage = pageNo;
			this.UpdatePageButtons(pageNo);
			this.UpdateDataUnits();
		}

		private void UpdatePageButtons(SaveAndLoadMgr.PageNo pageNo)
		{
			foreach (KeyValuePair<SaveAndLoadMgr.PageNo, SaveAndLoadCtrl.PageButton> keyValuePair in this.m_PageButtonDic)
			{
				if (keyValuePair.Key == pageNo)
				{
					keyValuePair.Value.btnPageNo.defaultColor = this.m_ActiveColor;
					keyValuePair.Value.selectCursor.SetActive(true);
				}
				else
				{
					keyValuePair.Value.btnPageNo.defaultColor = this.m_InactiveColor;
					keyValuePair.Value.selectCursor.SetActive(false);
				}
			}
		}

		private void OnClickDataUnit(KasizukiSaveAndLoadCtrl.DataUnitComp dataUnit)
		{
			int saveSlot = dataUnit.index + (this.m_NowOpenPage - SaveAndLoadMgr.PageNo.Page_1) * 16;
			if (UICamera.currentTouchID != -2)
			{
				SaveAndLoadMgr.ViewType nowViewType = this.m_NowViewType;
				if (nowViewType != SaveAndLoadMgr.ViewType.Load)
				{
					if (nowViewType != SaveAndLoadMgr.ViewType.Save)
					{
						NDebug.Assert("セーブデータを開く種類に不正な値が入っていました", false);
					}
					else
					{
						string msg;
						if (GameMain.Instance.KasizukiMgr.IsExistSaveData(saveSlot))
						{
							msg = string.Format("{0}番に上書きして保存します。\n宜しいですか？", saveSlot);
						}
						else
						{
							msg = string.Format("{0}番にデータを保存しますか？", saveSlot);
						}
						this.OpenDialog(SystemDialog.TYPE.OK_CANCEL, msg, delegate
						{
							this.CloseDialog();
							this.Save(saveSlot);
						});
					}
				}
				else
				{
					string msg = string.Format("{0}番のデータをロードしますか？", saveSlot);
					this.OpenDialog(SystemDialog.TYPE.OK_CANCEL, msg, delegate
					{
						this.CloseDialog();
						this.Load(saveSlot);
					});
				}
				return;
			}
			if (!GameMain.Instance.KasizukiMgr.IsExistSaveData(saveSlot))
			{
				return;
			}
			string msg2 = string.Format("データを削除します。\n本当に宜しいですか。", new object[0]);
			this.OpenDialog(SystemDialog.TYPE.OK_CANCEL, msg2, delegate
			{
				this.CloseDialog();
				this.Delete(saveSlot);
			});
		}

		private void OnClickNewGameUnit(KasizukiSaveAndLoadCtrl.DataUnitComp dataUnit)
		{
			if (dataUnit.index != 0)
			{
				return;
			}
			string msg = "ここに新規顧客情報を登録します。";
			this.OpenDialog(SystemDialog.TYPE.OK_CANCEL, msg, delegate
			{
				this.CloseDialog();
				this.NewGame();
			});
		}

		private void OnChangeDataUnitComment(KasizukiSaveAndLoadCtrl.DataUnitComp dataUnit)
		{
			int saveDataIndex = dataUnit.index + (this.m_NowOpenPage - SaveAndLoadMgr.PageNo.Page_1) * 16;
			KasizukiManager.SaveDataHeader saveDataHeader = GameMain.Instance.KasizukiMgr.GetSaveDataHeader(saveDataIndex);
			saveDataHeader.strComment = dataUnit.inputComment.value;
		}

		private void OnClickCancel()
		{
			if (this.onClickCancel != null)
			{
				this.onClickCancel();
			}
		}

		private void OpenDialog(SystemDialog.TYPE openType, string msg, Action onOK)
		{
			GameMain.Instance.SysDlg.Show(msg, openType, delegate
			{
				if (onOK != null)
				{
					onOK();
				}
			}, new SystemDialog.OnClick(this.CloseDialog));
		}

		private void CloseDialog()
		{
			GameMain.Instance.SysDlg.Close();
		}

		private void Save(int saveSlot)
		{
			GameMain.Instance.KasizukiMgr.SerializeLocal(saveSlot);
			this.Open(SaveAndLoadMgr.ViewType.Save, false);
			if (this.onSave != null)
			{
				this.onSave();
			}
			this.OpenDialog(SystemDialog.TYPE.OK, "データを保存しました。", new Action(this.CloseDialog));
		}

		private void Load(int saveSlot)
		{
			GameMain.Instance.KasizukiMgr.DeserializeLocal(saveSlot);
			if (this.onLoad != null)
			{
				this.onLoad();
			}
		}

		private void Delete(int saveSlot)
		{
			GameMain.Instance.KasizukiMgr.DeleteSaveDataLocal(saveSlot);
			this.Open(this.m_NowViewType, false);
			if (this.onDelete != null)
			{
				this.onDelete();
			}
			this.OpenDialog(SystemDialog.TYPE.OK, "データを削除しました。", new Action(this.CloseDialog));
		}

		private void NewGame()
		{
			GameMain.Instance.KasizukiMgr.CreateNewData();
			if (this.onClickNewGame != null)
			{
				this.onClickNewGame();
			}
		}

		private void OnDestroy()
		{
			this.onClickCancel = null;
			this.onClickNewGame = null;
			this.onLoad = null;
			this.onSave = null;
			this.onDelete = null;
		}

		public Action onClickCancel { get; set; }

		public Action onClickNewGame { get; set; }

		public Action onLoad { get; set; }

		public Action onSave { get; set; }

		public Action onDelete { get; set; }

		private const int PAGE_BUTTON_COUNT = 5;

		private const int PAGE_DATA_COUNT = 16;

		private const string STR_PAGE_BUTTON_NAME = "cm3d2_save_load_page";

		private const string DIALOG_MESSAGE_TO_SAVE_NORMAL = "{0}番にデータを保存しますか？";

		private const string DIALOG_MESSAGE_TO_SAVE_OVERWRITE = "{0}番に上書きして保存します。\n宜しいですか？";

		private const string DIALOG_MESSAGE_TO_LOAD = "{0}番のデータをロードしますか？";

		private const string DIALOG_MESSAGE_TO_DELETE = "データを削除します。\n本当に宜しいですか。";

		private const string DIALOG_MESSAGE_RESULT_SAVE = "データを保存しました。";

		private const string DIALOG_MESSAGE_RESULT_DELETE = "データを削除しました。";

		private const string DIALOG_MESSAGE_TO_NEW_GAME = "ここに新規顧客情報を登録します。";

		private bool m_IsStarted;

		[SerializeField]
		private uGUIListViewer m_ListPageButtons;

		[SerializeField]
		private uGUIListViewer m_ListDataUnits;

		private SaveAndLoadMgr.PageNo m_NowOpenPage;

		private SaveAndLoadMgr.ViewType m_NowViewType;

		private ObjectCacheDic m_CacheSaveObjects;

		private ObjectCacheDic m_CacheLoadObjects;

		private ObjectCacheDic m_CacheButtons;

		private Color m_ActiveColor;

		private Color m_InactiveColor;

		private Dictionary<SaveAndLoadMgr.PageNo, SaveAndLoadCtrl.PageButton> m_PageButtonDic;

		public class DataUnitComp : MonoBehaviour
		{
			public int index { get; private set; }

			public void Awake()
			{
				this.SetUpCache();
			}

			protected virtual void SetUpCache()
			{
				this.content = UTY.GetChildObject(base.gameObject, "Content", false);
				this.labelDate = UTY.GetChildObject(this.content, "DataInfo/Date", false).GetComponent<UILabel>();
				this.labelUsedCount = UTY.GetChildObject(this.content, "DataInfo/UsedCount/Number", false).GetComponent<UILabel>();
				this.labelManName = UTY.GetChildObject(this.content, "DataInfo/Man/Name", false).GetComponent<UILabel>();
				this.labelMaidCount = UTY.GetChildObject(this.content, "DataInfo/NumberOfEmployees/Number", false).GetComponent<UILabel>();
				this.inputComment = UTY.GetChildObject(this.content, "Comment/InputField", false).GetComponent<UIInput>();
				this.selectCursor = UTY.GetChildObject(this.content, "SelectCursor", false);
				this.newLabel = UTY.GetChildObject(base.gameObject, "NewLabel", false);
				this.newGameLabel = UTY.GetChildObject(base.gameObject, "NewGameLabel", false);
			}

			public void Init(int index)
			{
				if (this.isInit)
				{
					Debug.Log("セーブデータ項目{0}番目は既に Init() 関数を呼ばれています。\n処理を行いません。");
					return;
				}
				this.index = index;
				this.isInit = true;
			}

			public UILabel labelDate;

			public UILabel labelUsedCount;

			public UILabel labelManName;

			public UILabel labelMaidCount;

			public UIInput inputComment;

			public GameObject selectCursor;

			public GameObject newLabel;

			public GameObject newGameLabel;

			public GameObject content;

			private bool isInit;
		}
	}
}
