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

public class CharacterSelectManager : MonoBehaviour
{
	public static void DefaultMaidList(List<Maid> draw_list)
	{
		CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
		for (int i = 0; i < characterMgr.GetStockMaidCount(); i++)
		{
			draw_list.Add(characterMgr.GetStockMaid(i));
		}
	}

	public static int SortMaidStandardNoSchedule(Maid maid_a, Maid maid_b)
	{
		if (maid_a.status.leader && !maid_b.status.leader)
		{
			return -1;
		}
		if (!maid_a.status.leader && maid_b.status.leader)
		{
			return 1;
		}
		Status status = GameMain.Instance.CharacterMgr.status;
		if (maid_a.status.creationTimeNum < maid_b.status.creationTimeNum)
		{
			return -1;
		}
		if (maid_a.status.creationTimeNum != maid_b.status.creationTimeNum)
		{
			return 1;
		}
		return string.Compare(maid_b.status.guid, maid_a.status.guid);
	}

	public static int SortMaidStandard(Maid maid_a, Maid maid_b)
	{
		if (maid_a.status.leader && !maid_b.status.leader)
		{
			return -1;
		}
		if (!maid_a.status.leader && maid_b.status.leader)
		{
			return 1;
		}
		Status status = GameMain.Instance.CharacterMgr.status;
		int scheduleMaidSetting = status.GetScheduleMaidSetting(maid_a);
		int scheduleMaidSetting2 = status.GetScheduleMaidSetting(maid_b);
		if (scheduleMaidSetting != -1 && scheduleMaidSetting2 == -1)
		{
			return -1;
		}
		if (scheduleMaidSetting == -1 && scheduleMaidSetting2 != -1)
		{
			return 1;
		}
		if (scheduleMaidSetting < scheduleMaidSetting2)
		{
			return -1;
		}
		if (scheduleMaidSetting > scheduleMaidSetting2)
		{
			return 1;
		}
		if (maid_a.status.creationTimeNum < maid_b.status.creationTimeNum)
		{
			return -1;
		}
		if (maid_a.status.creationTimeNum != maid_b.status.creationTimeNum)
		{
			return 1;
		}
		return string.Compare(maid_b.status.guid, maid_a.status.guid);
	}

	public static int SortMaidStandard(Transform a, Transform b)
	{
		Maid maid = a.gameObject.GetComponent<MaidPlate>().maid;
		Maid maid2 = b.gameObject.GetComponent<MaidPlate>().maid;
		return CharacterSelectManager.SortMaidStandard(maid, maid2);
	}

	public static int SortMaidRanking(Maid maid_a, Maid maid_b)
	{
		if (maid_a.status.popularRank != 0 && maid_b.status.popularRank == 0)
		{
			return -1;
		}
		if (maid_a.status.popularRank == 0 && maid_b.status.popularRank != 0)
		{
			return 1;
		}
		if (maid_a.status.popularRank < maid_b.status.popularRank)
		{
			return -1;
		}
		if (maid_a.status.popularRank != maid_b.status.popularRank)
		{
			return 1;
		}
		return CharacterSelectManager.SortMaidStandard(maid_a, maid_b);
	}

	public static int SortMaidRanking(Transform a, Transform b)
	{
		Maid maid = a.gameObject.GetComponent<MaidPlate>().maid;
		Maid maid2 = b.gameObject.GetComponent<MaidPlate>().maid;
		return CharacterSelectManager.SortMaidRanking(maid, maid2);
	}

	public void Awake()
	{
		NDebug.AssertNull(this.MaidPlateParentGrid != null);
		this.switch_panel_ = this.MaidPlateParentGrid.gameObject.GetComponent<UIWFSwitchPanel>();
		this.tab_panel_ = this.MaidPlateParentGrid.gameObject.GetComponent<UIWFTabPanel>();
		this.scroll_view_ = UTY.GetChildObject(base.gameObject, "Contents", false).GetComponent<UIScrollView>();
		this.scroll_bar_ = UTY.GetChildObject(base.gameObject, "Scroll Bar", false).GetComponent<UIScrollBar>();
		Transform transform = base.gameObject.transform.Find("BigThumbnail");
		if (transform != null)
		{
			this.big_thumbnail_ = transform.gameObject.GetComponent<BigThumbnail>();
		}
		this.SetSortType(CharacterSelectManager.SortType.Normal);
	}

	public void Create(CharacterSelectManager.Type type, int slect_max = 3, bool select_no_draw = true)
	{
		this.thumbnail_load_ = true;
		this.select_no_draw_ = select_no_draw;
		Stopwatch stopwatch = new Stopwatch();
		stopwatch.Start();
		if (this.big_thumbnail_ != null)
		{
			this.big_thumbnail_.Visible = true;
		}
		this.type_ = type;
		if (this.callback_maid_list_ == null)
		{
			if (CharacterSelectManager.<>f__mg$cache0 == null)
			{
				CharacterSelectManager.<>f__mg$cache0 = new CharacterSelectManager.CallBackMaidList(CharacterSelectManager.DefaultMaidList);
			}
			this.callback_maid_list_ = CharacterSelectManager.<>f__mg$cache0;
		}
		Transform transform = this.MaidPlateParentGrid.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			UnityEngine.Object.Destroy(transform.GetChild(i).gameObject);
		}
		transform.DetachChildren();
		List<Maid> list = new List<Maid>();
		this.callback_maid_list_(list);
		this.maid_list_count_ = list.Count;
		this.select_list_ = null;
		for (int j = 0; j < list.Count; j++)
		{
			GameObject gameObject = Utility.CreatePrefab(this.MaidPlateParentGrid.gameObject, "CharacterSelect/Prefab/MaidPlateSimple", true);
			gameObject.GetComponent<MaidPlate>().SetMaidData(list[j]);
			UIWFSelectButton component = UTY.GetChildObject(gameObject, "Button", false).GetComponent<UIWFSelectButton>();
			UIWFTabButton component2 = UTY.GetChildObject(gameObject, "Button", false).GetComponent<UIWFTabButton>();
			if (type == CharacterSelectManager.Type.Select)
			{
				UnityEngine.Object.Destroy(component);
				component2.enabled = true;
				EventDelegate.Add(component2.onSelect, new EventDelegate.Callback(this.OnSelect));
			}
			else if (type == CharacterSelectManager.Type.Multiple)
			{
				UnityEngine.Object.Destroy(component2);
				component.enabled = true;
				EventDelegate.Add(component.onSelect, new EventDelegate.Callback(this.OnSelect));
			}
			else if (type == CharacterSelectManager.Type.Click)
			{
				UnityEngine.Object.Destroy(component);
				component2.enabled = true;
				EventDelegate.Add(component2.onClick, new EventDelegate.Callback(this.OnClick));
			}
		}
		if (type == CharacterSelectManager.Type.Select)
		{
			UnityEngine.Object.Destroy(this.switch_panel_);
			this.switch_panel_ = null;
			if (this.tab_panel_ == null)
			{
				this.tab_panel_ = this.MaidPlateParentGrid.gameObject.AddComponent<UIWFTabPanel>();
			}
			this.tab_panel_.enabled = true;
			this.tab_panel_.UpdateChildren();
		}
		else if (type == CharacterSelectManager.Type.Multiple)
		{
			UnityEngine.Object.Destroy(this.tab_panel_);
			this.tab_panel_ = null;
			if (this.switch_panel_ == null)
			{
				this.switch_panel_ = this.MaidPlateParentGrid.gameObject.AddComponent<UIWFSwitchPanel>();
			}
			this.switch_panel_.SetNumberOfSelected(slect_max);
			this.switch_panel_.enabled = true;
			this.select_list_ = new MaidPlate[slect_max];
			this.switch_panel_.UpdateChildren();
		}
		this.Reposition();
		this.scroll_view_.ResetPosition();
		if (this.type_ == CharacterSelectManager.Type.Select && this.MaidPlateParentGrid.GetChildList().Count != 0)
		{
			UIWFTabButton component3 = UTY.GetChildObject(this.MaidPlateParentGrid.GetChild(0).gameObject, "Button", false).GetComponent<UIWFTabButton>();
			this.tab_panel_.Select(component3);
		}
		stopwatch.Stop();
	}

	public void SetSortType(CharacterSelectManager.SortType type)
	{
		if (type == CharacterSelectManager.SortType.Normal)
		{
			UIGrid maidPlateParentGrid = this.MaidPlateParentGrid;
			if (CharacterSelectManager.<>f__mg$cache1 == null)
			{
				CharacterSelectManager.<>f__mg$cache1 = new Comparison<Transform>(CharacterSelectManager.SortMaidStandard);
			}
			maidPlateParentGrid.onCustomSort = CharacterSelectManager.<>f__mg$cache1;
		}
		else
		{
			UIGrid maidPlateParentGrid2 = this.MaidPlateParentGrid;
			if (CharacterSelectManager.<>f__mg$cache2 == null)
			{
				CharacterSelectManager.<>f__mg$cache2 = new Comparison<Transform>(CharacterSelectManager.SortMaidRanking);
			}
			maidPlateParentGrid2.onCustomSort = CharacterSelectManager.<>f__mg$cache2;
		}
		this.Reposition();
	}

	public void SelectMaid(Maid maid)
	{
		if (this.type_ != CharacterSelectManager.Type.Select || maid == null)
		{
			return;
		}
		List<Transform> childList = this.MaidPlateParentGrid.GetChildList();
		for (int i = 0; i < childList.Count; i++)
		{
			if (childList[i].gameObject.GetComponent<MaidPlate>().maid == maid)
			{
				UIWFTabButton component = UTY.GetChildObject(childList[i].gameObject, "Button", false).GetComponent<UIWFTabButton>();
				this.tab_panel_.Select(component);
				return;
			}
		}
	}

	public void SelectAllMaid()
	{
		if (this.type_ != CharacterSelectManager.Type.Multiple || this.select_list_ == null || this.select_no_draw_)
		{
			return;
		}
		this.thumbnail_load_ = false;
		CharacterSelectManager.CallBackOnMultiSelect callBackOnMultiSelect = this.callback_on_multi_select_;
		this.callback_on_multi_select_ = null;
		this.switch_panel_.SelectAll();
		this.callback_on_multi_select_ = callBackOnMultiSelect;
		if (this.callback_on_multi_select_ != null)
		{
			Maid[] array = new Maid[this.select_list_.Length];
			for (int i = 0; i < this.select_list_.Length; i++)
			{
				if (this.select_list_[i] != null)
				{
					array[i] = this.select_list_[i].maid;
				}
			}
			this.callback_on_multi_select_(array);
		}
		this.thumbnail_load_ = true;
	}

	public void SelectAllReleaseMaid()
	{
		if (this.type_ != CharacterSelectManager.Type.Multiple || this.select_list_ == null || this.select_no_draw_)
		{
			return;
		}
		this.thumbnail_load_ = false;
		CharacterSelectManager.CallBackOnMultiSelect callBackOnMultiSelect = this.callback_on_multi_select_;
		this.callback_on_multi_select_ = null;
		this.switch_panel_.SelectAllRelease();
		this.callback_on_multi_select_ = callBackOnMultiSelect;
		if (this.callback_on_multi_select_ != null)
		{
			Maid[] array = new Maid[this.select_list_.Length];
			for (int i = 0; i < this.select_list_.Length; i++)
			{
				if (this.select_list_[i] != null)
				{
					array[i] = this.select_list_[i].maid;
				}
			}
			this.callback_on_multi_select_(array);
		}
		this.thumbnail_load_ = true;
	}

	public void UpdateMaidPlate(Maid maid)
	{
		Transform transform = this.MaidPlateParentGrid.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			MaidPlate component = transform.GetChild(i).GetComponent<MaidPlate>();
			if (component.maid == maid)
			{
				component.SetMaidData(maid);
				return;
			}
		}
		transform = this.NoDrawArea.transform;
		for (int j = 0; j < transform.childCount; j++)
		{
			MaidPlate component2 = transform.GetChild(j).GetComponent<MaidPlate>();
			if (component2.maid == maid)
			{
				component2.SetMaidData(maid);
				return;
			}
		}
	}

	public void RemoveMaidPlate(Maid maid)
	{
		List<Transform> childList = this.MaidPlateParentGrid.GetChildList();
		MaidPlate maidPlate = null;
		MaidPlate maidPlate2 = null;
		bool flag = false;
		for (int i = 0; i < childList.Count; i++)
		{
			maidPlate = childList[i].gameObject.GetComponent<MaidPlate>();
			if (maidPlate.maid == maid)
			{
				if (i + 1 < childList.Count)
				{
					maidPlate2 = childList[i + 1].gameObject.GetComponent<MaidPlate>();
				}
				flag = true;
				break;
			}
			maidPlate2 = maidPlate;
		}
		if (!flag)
		{
			NDebug.Assert("error.", false);
			return;
		}
		if (this.type_ == CharacterSelectManager.Type.Select && maidPlate2 != null && maidPlate2.maid != maid)
		{
			UIWFTabButton component = UTY.GetChildObject(maidPlate2.gameObject, "Button", false).GetComponent<UIWFTabButton>();
			this.tab_panel_.Select(component);
		}
		this.MoveNoDrawArea(maid);
		maidPlate.SetMaidData(null);
		this.Reposition();
	}

	public bool MoveNoDrawArea(Maid maid)
	{
		MaidPlate maidPlate = null;
		Transform transform = this.MaidPlateParentGrid.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			MaidPlate component = transform.GetChild(i).GetComponent<MaidPlate>();
			if (component.maid == maid)
			{
				maidPlate = component;
				break;
			}
		}
		if (maidPlate == null)
		{
			return false;
		}
		maidPlate.gameObject.transform.SetParent(this.NoDrawArea.transform, false);
		this.Reposition();
		return true;
	}

	public bool MoveGridArea(Maid maid)
	{
		MaidPlate maidPlate = null;
		Transform transform = this.NoDrawArea.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			MaidPlate component = transform.GetChild(i).GetComponent<MaidPlate>();
			if (component.maid == maid)
			{
				maidPlate = component;
				break;
			}
		}
		if (maidPlate == null)
		{
			return false;
		}
		maidPlate.gameObject.transform.SetParent(this.MaidPlateParentGrid.transform, false);
		this.Reposition();
		return true;
	}

	public void SetRightVisible(bool is_visible)
	{
		Transform transform = this.MaidPlateParentGrid.transform;
		UISprite component = UTY.GetChildObject(base.gameObject, "BG", false).GetComponent<UISprite>();
		Transform transform2 = UTY.GetChildObject(base.gameObject, "Scroll Bar", false).transform;
		UIWidget component2 = UTY.GetChildObject(base.gameObject, "DragMat", false).GetComponent<UIWidget>();
		this.right_visible_ = is_visible;
		if (is_visible)
		{
			for (int i = 0; i < transform.childCount; i++)
			{
				MaidPlate component3 = transform.GetChild(i).gameObject.GetComponent<MaidPlate>();
				component3.SetRightPlateVisible(true);
			}
			component.transform.localPosition = new Vector3(145f, 0f, 0f);
			component.width = 792;
			transform2.localPosition = new Vector3(354f, 0f, 0f);
			component2.transform.localPosition = new Vector3(140f, 0f, 0f);
			component2.width = 784;
			component2.ResizeCollider();
		}
		else
		{
			for (int j = 0; j < transform.childCount; j++)
			{
				MaidPlate component4 = transform.GetChild(j).gameObject.GetComponent<MaidPlate>();
				component4.SetRightPlateVisible(false);
			}
			component.transform.localPosition = Vector3.zero;
			component.width = 502;
			transform2.localPosition = new Vector3(64f, 0f, 0f);
			component2.transform.localPosition = new Vector3(-5f, 0f, 0f);
			component2.width = 492;
			component2.ResizeCollider();
		}
	}

	public void SetCallBackMaidList(CharacterSelectManager.CallBackMaidList callback)
	{
		this.callback_maid_list_ = callback;
	}

	public void SetCallBackOnClick(CharacterSelectManager.CallBackOnClick callback)
	{
		this.callback_on_click_ = callback;
	}

	public void SetCallBackOnMultiSelect(CharacterSelectManager.CallBackOnMultiSelect callback)
	{
		this.callback_on_multi_select_ = callback;
	}

	public void SetCallBackCallBackOnSelect(CharacterSelectManager.CallBackOnSelect callback)
	{
		this.callback_on_select_ = callback;
	}

	public void Reposition()
	{
		this.MaidPlateParentGrid.Reposition();
		this.scroll_view_.UpdatePosition();
	}

	public bool IsRightVisible()
	{
		return this.right_visible_;
	}

	public UIScrollView scroll_view
	{
		get
		{
			return this.scroll_view_;
		}
	}

	public UIScrollBar scroll_bar
	{
		get
		{
			return this.scroll_bar_;
		}
	}

	public BigThumbnail big_thumbnail
	{
		get
		{
			return this.big_thumbnail_;
		}
	}

	public int maid_list_count
	{
		get
		{
			return this.maid_list_count_;
		}
	}

	private void OnSelect()
	{
		GameObject gameObject = UIButton.current.transform.parent.gameObject;
		MaidPlate component = gameObject.GetComponent<MaidPlate>();
		bool selected = UIWFSelectButton.selected;
		if (this.type_ == CharacterSelectManager.Type.Select)
		{
			if (selected && this.callback_on_select_ != null)
			{
				if (this.big_thumbnail_ != null && this.thumbnail_load_)
				{
					this.big_thumbnail_.SetMaid(component.maid);
				}
				this.callback_on_select_(component.maid);
			}
		}
		else if (this.type_ == CharacterSelectManager.Type.Multiple)
		{
			if (selected)
			{
				for (int i = 0; i < this.select_list_.Length; i++)
				{
					if (this.select_list_[i] == null)
					{
						if (this.big_thumbnail_ != null && this.thumbnail_load_)
						{
							this.big_thumbnail_.SetMaid(component.maid);
						}
						this.select_list_[i] = component;
						if (this.select_no_draw_)
						{
							this.select_list_[i].SetSelectNoVisible(i + 1, true);
						}
						break;
					}
				}
			}
			else
			{
				for (int j = 0; j < this.select_list_.Length; j++)
				{
					if (this.select_list_[j] == component)
					{
						if (this.big_thumbnail_ != null && this.thumbnail_load_)
						{
							this.big_thumbnail_.SetMaid(component.maid);
						}
						if (this.select_no_draw_)
						{
							this.select_list_[j].SetSelectNoVisible(j + 1, false);
						}
						this.select_list_[j] = null;
						break;
					}
				}
			}
			if (this.callback_on_multi_select_ != null)
			{
				Maid[] array = new Maid[this.select_list_.Length];
				for (int k = 0; k < this.select_list_.Length; k++)
				{
					if (this.select_list_[k] != null)
					{
						array[k] = this.select_list_[k].maid;
					}
				}
				this.callback_on_multi_select_(array);
			}
		}
	}

	private void OnClick()
	{
		MaidPlate component = UIButton.current.transform.parent.GetComponent<MaidPlate>();
		if (this.callback_on_click_ != null)
		{
			this.callback_on_click_(component.maid);
		}
	}

	public int select_max
	{
		get
		{
			return (this.select_list_ != null) ? this.select_list_.Length : 0;
		}
	}

	public UIGrid MaidPlateParentGrid;

	public GameObject NoDrawArea;

	private CharacterSelectManager.CallBackMaidList callback_maid_list_;

	private CharacterSelectManager.CallBackOnClick callback_on_click_;

	private CharacterSelectManager.CallBackOnMultiSelect callback_on_multi_select_;

	private CharacterSelectManager.CallBackOnSelect callback_on_select_;

	private BigThumbnail big_thumbnail_;

	private UIScrollView scroll_view_;

	private UIScrollBar scroll_bar_;

	private UIWFSwitchPanel switch_panel_;

	private UIWFTabPanel tab_panel_;

	private CharacterSelectManager.Type type_;

	private MaidPlate[] select_list_;

	private bool right_visible_;

	private bool select_no_draw_;

	private int maid_list_count_;

	private bool thumbnail_load_;

	[CompilerGenerated]
	private static CharacterSelectManager.CallBackMaidList <>f__mg$cache0;

	[CompilerGenerated]
	private static Comparison<Transform> <>f__mg$cache1;

	[CompilerGenerated]
	private static Comparison<Transform> <>f__mg$cache2;

	public enum Type
	{
		Click,
		Select,
		Multiple
	}

	public enum SortType
	{
		Normal,
		Ranking
	}

	public delegate void CallBackMaidList(List<Maid> draw_list);

	public delegate void CallBackOnClick(Maid select_maid);

	public delegate void CallBackOnSelect(Maid select_maid);

	public delegate void CallBackOnMultiSelect(Maid[] select_maid);
}
