using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using wf;

public class LoadReplaceMaid : MonoBehaviour
{
	public void Awake()
	{
		this.chara_mgr_ = GameMain.Instance.CharacterMgr;
		EventDelegate.Add(this.OKButton.onClick, new EventDelegate.Callback(this.OnClickOK));
		this.ui_panel_ = base.gameObject.GetComponentInChildren<UIPanel>();
		this.positionBackup.Add(this.OKButton.transform, this.OKButton.transform.localPosition);
		this.bgBackupSize = new Vector2((float)this.bgWidget.width, (float)this.bgWidget.height);
	}

	public void OnEnable()
	{
		if (this.scroll_view_ != null)
		{
			return;
		}
		this.scroll_view_ = NGUITools.FindInParents<UIScrollView>(this.ContentGrid.transform);
		this.ui_panel_.alpha = 0f;
	}

	public bool CheckAndStart(List<KeyValuePair<string, string>> check_guid_maid_list)
	{
		if (!base.gameObject.activeSelf)
		{
			base.gameObject.SetActive(true);
		}
		this.not_exist_maid_list_.Clear();
		this.already_used_maid_list_.Clear();
		this.replace_result_dic_.Clear();
		for (int i = 0; i < check_guid_maid_list.Count; i++)
		{
			string key = check_guid_maid_list[i].Key;
			Maid stockMaid = this.chara_mgr_.GetStockMaid(key);
			if (stockMaid == null)
			{
				this.not_exist_maid_list_.Add(new KeyValuePair<string, string>(check_guid_maid_list[i].Key, check_guid_maid_list[i].Value));
			}
			else
			{
				this.already_used_maid_list_.Add(stockMaid);
			}
		}
		bool flag = 0 < this.not_exist_maid_list_.Count;
		if (flag)
		{
			this.ui_panel_.alpha = 1f;
			this.SelectReplaceMaid();
		}
		else
		{
			this.ui_panel_.alpha = 0f;
			base.gameObject.SetActive(false);
			if (this.OnReplaceEndEvent != null)
			{
				this.OnReplaceEndEvent(this.replace_result_dic_);
			}
		}
		return flag;
	}

	private void ChangeMode(int not_exist_maid_count, string maid_name)
	{
		if (ScenePresetWindow.SelectData != null)
		{
			this.bgWidget.SetDimensions(818, 500);
			Vector3 vector = this.positionBackup[this.OKButton.transform];
			this.OKButton.transform.localPosition = new Vector3(1140f, vector.y, vector.z);
			if (0 <= ScenePresetWindow.SelectData.charaFocusImages.Length - not_exist_maid_count && ScenePresetWindow.SelectData.charaFocusImages.Length - not_exist_maid_count < ScenePresetWindow.SelectData.charaFocusImages.Length)
			{
				this.ui2dSprite.gameObject.SetActive(true);
				int num = ScenePresetWindow.SelectData.charaFocusImages.Length - not_exist_maid_count;
				string str = ScenePresetWindow.SelectData.charaFocusImages[num];
				Texture2D texture2D = ImportCM.CreateTexture(str + ".tex");
				Sprite sprite2D = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
				if (this.ui2dSprite.sprite2D != null && this.ui2dSprite.sprite2D.texture != null)
				{
					UnityEngine.Object.DestroyImmediate(this.ui2dSprite.sprite2D.texture);
				}
				this.ui2dSprite.sprite2D = sprite2D;
				this.ui2dSprite.SetDimensions(texture2D.width, texture2D.height);
				this.TextLabel.text = "ポジション" + (num + 1) + "に読み込むメイドを選択してください。";
			}
		}
		else
		{
			this.bgWidget.SetDimensions((int)this.bgBackupSize.x, (int)this.bgBackupSize.y);
			this.OKButton.transform.localPosition = this.positionBackup[this.OKButton.transform];
			this.ui2dSprite.gameObject.SetActive(false);
			this.TextLabel.text = string.Format(LoadReplaceMaid.kText, maid_name);
		}
	}

	private bool SelectReplaceMaid()
	{
		this.select_maid_ = null;
		if (this.not_exist_maid_list_.Count <= 0)
		{
			return false;
		}
		this.ChangeMode(this.not_exist_maid_list_.Count, this.not_exist_maid_list_[0].Value);
		List<Maid> list = new List<Maid>();
		for (int i = 0; i < this.chara_mgr_.GetStockMaidCount(); i++)
		{
			if (!this.already_used_maid_list_.Contains(this.chara_mgr_.GetStockMaid(i)))
			{
				list.Add(this.chara_mgr_.GetStockMaid(i));
			}
		}
		List<Maid> list2 = list;
		if (LoadReplaceMaid.<>f__mg$cache0 == null)
		{
			LoadReplaceMaid.<>f__mg$cache0 = new Comparison<Maid>(CharacterSelectManager.SortMaidStandardNoSchedule);
		}
		list2.Sort(LoadReplaceMaid.<>f__mg$cache0);
		list.Insert(0, null);
		for (int j = 0; j < this.ContentGrid.transform.childCount; j++)
		{
			UnityEngine.Object.Destroy(this.ContentGrid.transform.GetChild(j).gameObject);
		}
		this.ContentGrid.gameObject.transform.DetachChildren();
		this.TabPanel.ResetSelect();
		UIWFTabButton uiwftabButton = null;
		for (int k = 0; k < list.Count; k++)
		{
			GameObject f_goParent;
			if (list[k] == null)
			{
				f_goParent = Utility.CreatePrefab(this.ContentGrid.gameObject, "ScenePhotoMode/SimpleMaidPlateSelectNot", true);
			}
			else
			{
				f_goParent = Utility.CreatePrefab(this.ContentGrid.gameObject, "ScenePhotoMode/SimpleMaidPlateSelect", true);
			}
			SimpleMaidPlate component = UTY.GetChildObject(f_goParent, "Plate", false).GetComponent<SimpleMaidPlate>();
			component.SetMaidData(list[k]);
			UIWFTabButton component2 = UTY.GetChildObject(f_goParent, "Plate/Button", false).GetComponent<UIWFTabButton>();
			EventDelegate.Add(component2.onSelect, new EventDelegate.Callback(this.OnSelectMaid));
			if (list[k] == null)
			{
				component2.gameObject.name = string.Empty;
			}
			else
			{
				component2.gameObject.name = list[k].status.guid;
			}
			if (uiwftabButton == null)
			{
				uiwftabButton = component2;
			}
		}
		this.ContentGrid.Reposition();
		this.scroll_view_.ResetPosition();
		this.TabPanel.UpdateChildren();
		this.TabPanel.Select(uiwftabButton);
		return true;
	}

	public void OnClickOK()
	{
		this.replace_result_dic_.Add(this.not_exist_maid_list_[0].Key, this.select_maid_);
		this.not_exist_maid_list_.RemoveAt(0);
		this.already_used_maid_list_.Add(this.select_maid_);
		this.select_maid_ = null;
		if (!this.SelectReplaceMaid())
		{
			this.ui_panel_.alpha = 0f;
			base.gameObject.SetActive(false);
			if (this.OnReplaceEndEvent != null)
			{
				this.OnReplaceEndEvent(this.replace_result_dic_);
			}
		}
	}

	public void OnSelectMaid()
	{
		if (!UIWFSelectButton.current.isSelected)
		{
			return;
		}
		if (string.IsNullOrEmpty(UIWFSelectButton.current.gameObject.name))
		{
			this.select_maid_ = null;
		}
		else
		{
			this.select_maid_ = this.chara_mgr_.GetStockMaid(UIWFSelectButton.current.gameObject.name);
		}
	}

	public UIWFTabPanel TabPanel;

	public UIGrid ContentGrid;

	public UILabel TextLabel;

	public UIButton OKButton;

	public UIWidget bgWidget;

	public UI2DSprite ui2dSprite;

	public Action<Dictionary<string, Maid>> OnReplaceEndEvent;

	private Dictionary<Transform, Vector3> positionBackup = new Dictionary<Transform, Vector3>();

	private Vector2 bgBackupSize;

	private static string kText = "メイド「{0}」が見つかりませんでした。\n変わりとして読み込むメイドを選択してください。";

	private CharacterMgr chara_mgr_;

	private List<KeyValuePair<string, string>> not_exist_maid_list_ = new List<KeyValuePair<string, string>>();

	private HashSet<Maid> already_used_maid_list_ = new HashSet<Maid>();

	private Dictionary<string, Maid> replace_result_dic_ = new Dictionary<string, Maid>();

	private Maid select_maid_;

	private UIScrollView scroll_view_;

	private UIPanel ui_panel_;

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