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

public class SaveAndLoadMgr : BaseCreatePanel
{
	public SaveAndLoadMgr.PageNo GetCurrentPageNo()
	{
		return this.currentPageNo;
	}

	public Action CallbackAfterFadeOut { get; set; }

	public void SetCurrentActiveData(string activeData)
	{
		this.currentActiveData = activeData;
	}

	public Action OnOpenLoadPanelKasizukiLocal { get; set; }

	public override void Init()
	{
		this.m_goPanel = base.GetPanel("SaveAndLoadPanel");
		this.m_ctrl = base.GetCtrl<SaveAndLoadCtrl>();
		this.m_ctrl.Init(this, this.m_goPanel);
		this.m_goPanel.SetActive(false);
		this.currentView = SaveAndLoadMgr.ViewType.None;
		this.currentPageNo = SaveAndLoadMgr.PageNo.Page_1;
	}

	public void OpenSavePanel()
	{
		Debug.Log("セーブをオープンしました。");
		base.BaseOpenPanel(SaveAndLoadMgr.ViewType.Save.ToString());
	}

	public void OpenLoadPanel()
	{
		this.m_ctrl.Kasiduki = false;
		Debug.Log("ロードをオープンしました。");
		base.BaseOpenPanel(SaveAndLoadMgr.ViewType.Load.ToString());
	}

	public void OpenLoadPanelKasiduki()
	{
		this.m_ctrl.Kasiduki = true;
		Debug.Log("ロード(カシヅキ)をオープンしました。");
		base.BaseOpenPanel(SaveAndLoadMgr.ViewType.Load.ToString());
	}

	public void OpenLoadPanelKasizukiLocal()
	{
		base.BaseOpenPanel("傅き");
	}

	protected override void OpenPanel(string openType)
	{
		if (openType == "傅き")
		{
			if (this.OnOpenLoadPanelKasizukiLocal != null)
			{
				this.OnOpenLoadPanelKasizukiLocal();
			}
			return;
		}
		SaveAndLoadMgr.ViewType viewType = (SaveAndLoadMgr.ViewType)Enum.Parse(typeof(SaveAndLoadMgr.ViewType), openType);
		if (viewType != SaveAndLoadMgr.ViewType.Save)
		{
			if (viewType == SaveAndLoadMgr.ViewType.Load)
			{
				this.DisplayLoadPanel();
			}
		}
		else
		{
			this.DisplaySavePanel();
		}
	}

	private void DisplaySavePanel()
	{
		this.CreateSaveOrLoadViewer(SaveAndLoadMgr.ViewType.Save);
	}

	private void DisplayLoadPanel()
	{
		this.CreateSaveOrLoadViewer(SaveAndLoadMgr.ViewType.Load);
	}

	private void CreateSaveOrLoadViewer(SaveAndLoadMgr.ViewType selectView)
	{
		if (this.currentView == selectView)
		{
			return;
		}
		int pageNoLatestData = this.GetPageNoLatestData();
		if (pageNoLatestData == -1)
		{
			this.currentPageNo = SaveAndLoadMgr.PageNo.Page_1;
		}
		else
		{
			this.currentPageNo = (SaveAndLoadMgr.PageNo)pageNoLatestData;
		}
		Dictionary<string, SaveAndLoadCtrl.LoadDataUnit> dicData = this.LoadDataByPage((int)this.currentPageNo);
		this.m_goPanel.SetActive(true);
		this.m_ctrl.CreateSaveAndLoadDataViewer(dicData, selectView);
		this.currentView = selectView;
		this.currentActiveData = string.Empty;
	}

	private int GetPageNoLatestData()
	{
		long num = GameMain.Instance.NewSaveDataNo();
		return (int)num / 10 + 1;
	}

	public void CloseSaveAndLoadPanel()
	{
		base.BaseClosePanel();
		this.m_ctrl.CloseReset();
	}

	public void ClosePanelAfterAction<T>(Func<T, bool> action, T param)
	{
		TweenAlpha tweenAlpha = TweenAlpha.Begin(this.fadeTargetPanel, 0.5f, 0f);
		EventDelegate.Set(tweenAlpha.onFinished, delegate()
		{
			if (action != null)
			{
				if (action(param))
				{
					this.fadeTargetPanel.SetActive(false);
				}
				else
				{
					this.CloseSaveAndLoadPanel();
				}
				action = null;
			}
		});
	}

	protected override void ClosePanel()
	{
		this.currentView = SaveAndLoadMgr.ViewType.None;
		this.currentPageNo = SaveAndLoadMgr.PageNo.Page_1;
	}

	public void ClickPageNo()
	{
		string name = UIButton.current.name;
		if (!base.IsValidateButton<SaveAndLoadMgr.PageNo>(name))
		{
			return;
		}
		SaveAndLoadMgr.PageNo pageNo = (SaveAndLoadMgr.PageNo)Enum.Parse(typeof(SaveAndLoadMgr.PageNo), name);
		if (pageNo == this.currentPageNo)
		{
			return;
		}
		this.currentPageNo = pageNo;
		int pageNo2 = (int)pageNo;
		Dictionary<string, SaveAndLoadCtrl.LoadDataUnit> dicData = this.LoadDataByPage(pageNo2);
		this.m_ctrl.CreateSaveAndLoadDataViewer(dicData, this.currentView);
	}

	public void PageUpper()
	{
		Dictionary<string, SaveAndLoadCtrl.LoadDataUnit> dicData = this.LoadDataByPage((int)this.currentPageNo);
		this.m_ctrl.CreateSaveAndLoadDataViewer(dicData, this.currentView);
	}

	public void ClickSaveOrLoadData()
	{
		string name = UIButton.current.name;
		if (this.currentActiveData == name)
		{
			Debug.Log("同じデータがクリックされたので処理を抜けます。");
			return;
		}
		if (UICamera.currentTouchID == -1)
		{
			this.m_ctrl.SetSaveOrLoadDataActive(name);
			if (UICamera.currentTouchID == -1)
			{
				this.currentActiveData = name;
				this.m_ctrl.SaveAndLoad(this.currentView, name);
			}
		}
		else if (UICamera.currentTouchID == -2 && !GameMain.Instance.VRMode)
		{
			Debug.Log(string.Format("{0}ボタンが右クリックされました。", name));
			if (this.m_ctrl.ExistData(name))
			{
				this.m_ctrl.DeleteSaveOrLoadData(name);
			}
		}
	}

	public void ClickComment()
	{
		Collider collider = UICamera.lastHit.collider;
		if (collider != null)
		{
			UIButton componentInParent = collider.GetComponentInParent<UIButton>();
			this.m_ctrl.SetSaveOrLoadDataActive(componentInParent.name);
		}
	}

	public void OnChangeComment()
	{
		string name = UIInput.current.name;
		string value = UIInput.current.value;
		this.m_ctrl.SaveComment(name, value);
	}

	protected override void SetFadeTargetPanel()
	{
		this.fadeTargetPanel = this.m_goPanel;
	}

	protected override void AfterFadeOut()
	{
		if (this.m_ctrl.IsTitleScene)
		{
			if (this.CallbackAfterFadeOut != null)
			{
				this.CallbackAfterFadeOut();
			}
		}
		else if (!this.m_ctrl.GetIsLoading())
		{
			DailyMgr manager = base.GetManager<DailyMgr>();
			manager.BaseOpenPanel(manager.GetCurrentDaily().ToString());
		}
		base.AfterFadeOut();
	}

	protected override void BeforeClose()
	{
		UICamera.InputEnable = false;
	}

	protected override void AfterClose()
	{
		if (!this.m_ctrl.GetIsLoading())
		{
			UICamera.InputEnable = true;
		}
	}

	private Dictionary<string, SaveAndLoadCtrl.LoadDataUnit> LoadDataByPage(int pageNo)
	{
		Dictionary<string, SaveAndLoadCtrl.LoadDataUnit> dictionary = new Dictionary<string, SaveAndLoadCtrl.LoadDataUnit>();
		int num = (pageNo - 1) * 10;
		int num2 = num + 10 - 1;
		for (int i = num; i <= num2; i++)
		{
			SaveAndLoadCtrl.LoadDataUnit loadDataUnit = new SaveAndLoadCtrl.LoadDataUnit();
			GameMain.SerializeHeader saveDataHeader = GameMain.Instance.GetSaveDataHeader(i);
			if (saveDataHeader != null)
			{
				loadDataUnit.pageNo = pageNo;
				loadDataUnit.serialNo = i;
				loadDataUnit.isLatest = GameMain.Instance.IsSaveDataNew(i);
				loadDataUnit.nVer = saveDataHeader.nVer;
				loadDataUnit.createAt = this.DateTimeParse(saveDataHeader.strSaveTime);
				loadDataUnit.lapsedDays = saveDataHeader.nGameDay;
				loadDataUnit.managerName = saveDataHeader.strPlayerName;
				loadDataUnit.numberOfEmployees = saveDataHeader.nMaidNum;
				loadDataUnit.comment = saveDataHeader.strComment;
				loadDataUnit.isEmpty = false;
			}
			else
			{
				loadDataUnit.pageNo = pageNo;
				loadDataUnit.serialNo = i;
				loadDataUnit.isEmpty = true;
			}
			dictionary.Add(string.Format("serialNo_{0}", i), loadDataUnit);
		}
		return dictionary;
	}

	private Dictionary<string, SaveAndLoadCtrl.LoadDataUnit> MakeDummyData()
	{
		Dictionary<string, SaveAndLoadCtrl.LoadDataUnit> dictionary = new Dictionary<string, SaveAndLoadCtrl.LoadDataUnit>();
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		int num4 = 47;
		for (int i = 0; i < num4; i++)
		{
			if (i % 10 == 0)
			{
				num++;
				num2 = 1;
			}
			int num5 = UnityEngine.Random.Range(0, 10);
			if (num5 % 3 == 0)
			{
				num2++;
			}
			else
			{
				SaveAndLoadCtrl.LoadDataUnit loadDataUnit = new SaveAndLoadCtrl.LoadDataUnit();
				loadDataUnit.pageNo = num;
				loadDataUnit.serialNo = num3;
				loadDataUnit.isLatest = false;
				loadDataUnit.createAt = DateTime.Now.ToString();
				loadDataUnit.lapsedDays = i;
				loadDataUnit.managerName = "経営者_" + i;
				loadDataUnit.numberOfEmployees = UnityEngine.Random.Range(0, 30);
				loadDataUnit.comment = string.Format("コメント_{0}、ページ={1}、シリアルNo={2}", i, loadDataUnit.pageNo, loadDataUnit.serialNo);
				dictionary.Add(string.Format("serialNo_{0}", num3), loadDataUnit);
				num2++;
				num3++;
			}
		}
		return dictionary;
	}

	private string DateTimeParse(string strDate)
	{
		string format = "yyyyMMddHHmmss";
		return DateTime.ParseExact(strDate, format, null).ToString("yyyy.MM.dd HH:mm");
	}

	private string currentActiveData;

	private SaveAndLoadMgr.PageNo currentPageNo;

	private SaveAndLoadCtrl m_ctrl;

	private SaveAndLoadMgr.ViewType currentView;

	private const SaveAndLoadMgr.PageNo PAGE_NO_WHEN_OPEN = SaveAndLoadMgr.PageNo.Page_1;

	private const string BUTTON_NAME_TEMPLATE = "serialNo_{0}";

	private const float DURATION_TO_FADE = 0.5f;

	private const int MAX_SLOT_COUNT_PER_PAGE = 10;

	private const int NOTHING_SAVE_DATA = -1;

	private const int LeftClick = -1;

	private const int RightClick = -2;

	public enum ViewType
	{
		None,
		Save,
		Load
	}

	public enum PageNo
	{
		Page_1 = 1,
		Page_2,
		Page_3,
		Page_4,
		Page_5,
		Page_6,
		Page_7,
		Page_8,
		Page_9,
		Page_10
	}
}
