using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using MaidStatus;
using MaidStatus.Old;
using UnityEngine;

[RequireComponent(typeof(UIPanel))]
public class MaidTransferMain : WfFadeBehaviour
{
	public void Awake()
	{
		this.statusMgr.Init();
		this.statusMgr.statusCtrl.gameObject.SetActive(true);
		this.loadBlackBG.SetActive(false);
		this.maidList = new List<Maid>(this.maidStorageObject.GetComponents<Maid>());
		this.charaSelectMgr.SetCallBackCallBackOnSelect(new CharacterSelectManager.CallBackOnSelect(this.OnSelectChara));
		this.deserializeCount = -1;
		this.initCount = 0;
		this.initCoroutine = this.InitializeMaid();
		base.StartCoroutine(this.initCoroutine);
	}

	private IEnumerator InitializeMaid()
	{
		this.initCount = 0;
		while (0 <= this.initCount)
		{
			this.maidList[this.initCount].InitializeStatus();
			this.initCount++;
			if (this.maidList.Count <= this.initCount)
			{
				this.initCount = -1;
			}
			yield return null;
		}
		yield break;
	}

	public void Call(string loadSaveFilePath)
	{
		if (this.initCount != -1)
		{
			base.StopCoroutine(this.initCoroutine);
			while (0 <= this.initCount)
			{
				this.maidList[this.initCount].InitializeStatus();
				this.initCount++;
				if (this.maidList.Count <= this.initCount)
				{
					this.initCount = -1;
				}
			}
		}
		if (this.deserializeCount != -1)
		{
			return;
		}
		this.loadSaveFilePath = loadSaveFilePath;
		OldDataConverter oldDataConverter = new OldDataConverter();
		this.oldDataList = oldDataConverter.OpenFile(loadSaveFilePath);
		base.StartCoroutine(this.InitializeDeserialize());
	}

	private IEnumerator InitializeDeserialize()
	{
		UICamera.InputEnable = false;
		this.deserializeCount = 0;
		GameMain.Instance.LoadIcon.NextLoadIcontImmediatelyDisplay();
		GameMain.Instance.LoadIcon.force_draw_new = true;
		GameMain.Instance.LoadIcon.SetForceDraw(true);
		GameMain.Instance.LoadIcon.is_center_draw = true;
		this.loadBlackBG.SetActive(true);
		foreach (OldDataConverter.MaidData oldData in this.oldDataList)
		{
			Texture2D iconTex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
			iconTex.LoadImage(oldData.ReadThumbnailByte());
			this.maidList[this.deserializeCount].status.Deserialize(oldData.statusReader, false);
			this.maidList[this.deserializeCount].status.leader = oldData.statusReader.leader;
			if (this.maidList[this.deserializeCount].GetThumIcon() != null)
			{
				UnityEngine.Object.DestroyImmediate(this.maidList[this.deserializeCount].GetThumIcon());
			}
			this.maidList[this.deserializeCount].SetThumIcon(iconTex);
			this.deserializeCount++;
			yield return null;
		}
		this.loadBlackBG.SetActive(false);
		GameMain.Instance.LoadIcon.is_center_draw = false;
		GameMain.Instance.LoadIcon.force_draw_new = false;
		GameMain.Instance.LoadIcon.SetForceDraw(false);
		this.deserializeCount = -1;
		this.transferPossibleMaidList = this.GetEnabledMaid();
		if (this.transferPossibleMaidList.Count == 0)
		{
			UICamera.InputEnable = true;
			GameMain.Instance.SysDlg.Show("移籍可能なメイドがいませんでした", SystemDialog.TYPE.OK, null, null);
			yield break;
		}
		List<Maid> list = this.transferPossibleMaidList;
		if (MaidTransferMain.<>f__mg$cache0 == null)
		{
			MaidTransferMain.<>f__mg$cache0 = new Comparison<Maid>(CharacterSelectManager.SortMaidStandardNoSchedule);
		}
		list.Sort(MaidTransferMain.<>f__mg$cache0);
		this.charaSelectMgr.SetCallBackMaidList(delegate(List<Maid> draw_list)
		{
			draw_list.Clear();
			foreach (Maid item in this.transferPossibleMaidList)
			{
				draw_list.Add(item);
			}
		});
		this.charaSelectMgr.Create(CharacterSelectManager.Type.Select, 3, true);
		this.charaSelectMgr.big_thumbnail.Visible = false;
		this.charaSelectMgr.gameObject.SetActive(true);
		WfFadeJob.Create(this, this.loadUi, this.fadeTime, iTween.EaseType.easeOutSine);
		yield break;
	}

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

	public void OnDestroy()
	{
		foreach (Maid maid in this.maidList)
		{
			if (!(maid == null))
			{
				if (maid.GetThumIcon() != null)
				{
					UnityEngine.Object.DestroyImmediate(maid.GetThumIcon());
				}
			}
		}
	}

	public void OnSelectChara(Maid select_maid)
	{
		if (select_maid == null)
		{
			return;
		}
		this.statusMgr.UpdateMaidStatus(select_maid);
		this.selectedMaid = select_maid;
	}

	public void ExecTransfer()
	{
		if (this.selectedMaid == null || string.IsNullOrEmpty(this.loadSaveFilePath))
		{
			return;
		}
		OldDataConverter.MaidData maidData = null;
		foreach (OldDataConverter.MaidData maidData2 in this.oldDataList)
		{
			if (this.selectedMaid.status.guid == maidData2.statusReader.guid)
			{
				maidData = maidData2;
				break;
			}
		}
		if (maidData != null)
		{
			this.maidManagementMain.Transfer(this.loadSaveFilePath, maidData);
		}
	}

	public List<Maid> GetEnabledMaid()
	{
		List<Maid> list = new List<Maid>();
		Dictionary<MaidStatus.Old.Personal, bool> dictionary = new Dictionary<MaidStatus.Old.Personal, bool>();
		dictionary.Add(MaidStatus.Old.Personal.Pure, MaidStatus.Personal.IsEnabled("Pure") && MaidStatus.Personal.GetData("Pure").compatible);
		dictionary.Add(MaidStatus.Old.Personal.Pride, MaidStatus.Personal.IsEnabled("Pride") && MaidStatus.Personal.GetData("Pride").compatible);
		dictionary.Add(MaidStatus.Old.Personal.Cool, MaidStatus.Personal.IsEnabled("Cool") && MaidStatus.Personal.GetData("Cool").compatible);
		dictionary.Add(MaidStatus.Old.Personal.Yandere, MaidStatus.Personal.IsEnabled("Yandere") && MaidStatus.Personal.GetData("Yandere").compatible);
		dictionary.Add(MaidStatus.Old.Personal.Anesan, MaidStatus.Personal.IsEnabled("Anesan") && MaidStatus.Personal.GetData("Anesan").compatible);
		dictionary.Add(MaidStatus.Old.Personal.Genki, MaidStatus.Personal.IsEnabled("Genki") && MaidStatus.Personal.GetData("Genki").compatible);
		dictionary.Add(MaidStatus.Old.Personal.Sadist, MaidStatus.Personal.IsEnabled("Sadist") && MaidStatus.Personal.GetData("Sadist").compatible);
		HashSet<string> hashSet = new HashSet<string>();
		CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
		for (int i = 0; i < characterMgr.GetStockMaidCount(); i++)
		{
			Maid stockMaid = characterMgr.GetStockMaid(i);
			if (stockMaid != null && !hashSet.Contains(stockMaid.status.guid))
			{
				hashSet.Add(stockMaid.status.guid);
			}
		}
		for (int j = 0; j < this.oldDataList.Count; j++)
		{
			StatusReader statusReader = this.oldDataList[j].statusReader;
			if (!statusReader.isRentalMaid && dictionary[statusReader.personal])
			{
				if (!hashSet.Contains(statusReader.guid))
				{
					list.Add(this.maidList[j]);
				}
			}
		}
		return list;
	}

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

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

	public virtual float alpha
	{
		set
		{
			if (this.targetPanels != null && 0 < this.targetPanels.Length)
			{
				foreach (UIPanel uipanel in this.targetPanels)
				{
					if (uipanel != null)
					{
						uipanel.alpha = value;
					}
				}
			}
		}
	}

	[SerializeField]
	public GameObject maidStorageObject;

	[SerializeField]
	public float fadeTime;

	[SerializeField]
	private WfFadeBehaviour loadUi;

	[SerializeField]
	private GameObject loadBlackBG;

	[SerializeField]
	private MaidManagementMain maidManagementMain;

	[SerializeField]
	private CharacterSelectManager charaSelectMgr;

	[SerializeField]
	private CharaSelectStatusMgr statusMgr;

	[SerializeField]
	public UIPanel[] targetPanels;

	public string loadSaveFilePath;

	private List<Maid> maidList = new List<Maid>();

	private List<OldDataConverter.MaidData> oldDataList;

	private List<Maid> transferPossibleMaidList = new List<Maid>();

	private Maid selectedMaid;

	private IEnumerator initCoroutine;

	private int initCount;

	private int deserializeCount;

	[CompilerGenerated]
	private static Comparison<Maid> <>f__mg$cache0;
}
