using System;
using System.Collections;
using System.Collections.Generic;
using MaidStatus;
using Schedule;
using UnityEngine;
using UnityEngine.UI;
using wf;

public class ScheduleTaskViewer : MonoBehaviour
{
	private void Awake()
	{
		Func<GameObject, string, UIButton> func = delegate(GameObject parent, string name)
		{
			GameObject gameObject = Utility.CreatePrefab(parent, "SceneShop/Prefab/CategoryButton", true);
			UICenterOnClick2 componentInChildren = gameObject.GetComponentInChildren<UICenterOnClick2>();
			if (componentInChildren != null)
			{
				UnityEngine.Object.Destroy(componentInChildren);
			}
			gameObject.name = name;
			UILabel component = UTY.GetChildObject(gameObject, "Label", false).GetComponent<UILabel>();
			component.text = gameObject.name;
			component.width -= 10;
			int m = component.width;
			component.width = 0;
			component.MakePixelPerfect();
			while (m < component.width)
			{
				component.fontSize--;
				component.width = 0;
				component.MakePixelPerfect();
			}
			return UTY.GetChildObject(gameObject, "Button", false).GetComponent<UIButton>();
		};
		foreach (KeyValuePair<int, string> keyValuePair in ScheduleCSVData.TaskCategoryNameMap)
		{
			string value = keyValuePair.Value;
			UIButton uibutton = func(this.CategoryGrid.gameObject, value);
			uibutton.name = keyValuePair.Key.ToString();
			this.category_button_dic_.Add(keyValuePair.Key, uibutton);
		}
		Dictionary<int, int> dictionary = new Dictionary<int, int>();
		foreach (KeyValuePair<int, ScheduleCSVData.ScheduleBase> keyValuePair2 in ScheduleCSVData.AllData)
		{
			int categoryID = keyValuePair2.Value.categoryID;
			if (!dictionary.ContainsKey(categoryID))
			{
				dictionary.Add(categoryID, 0);
			}
			dictionary[categoryID]++;
		}
		int num = 0;
		foreach (KeyValuePair<int, int> keyValuePair3 in dictionary)
		{
			num = System.Math.Max(num, keyValuePair3.Value);
		}
		for (int i = 0; i < 3; i++)
		{
			ScheduleTaskCtrl.TaskType key = (ScheduleTaskCtrl.TaskType)i;
			this.tmpData.Add(key, new ScheduleTaskViewer.TemporaryData());
			this.tmpData[key].place = new GameObject();
			this.tmpData[key].place.transform.SetParent(base.transform, false);
			this.tmpData[key].place.name = "tmp_" + key.ToString();
			this.tmpData[key].place.SetActive(false);
		}
		for (int j = 0; j < num; j++)
		{
			NightTaskUnit nightTaskUnit = Utility.CreatePrefab(this.tmpData[ScheduleTaskCtrl.TaskType.Yotogi].place, "SceneDaily/Schedule/Prefab/NightTaskUnit", true).AddComponent<NightTaskUnit>();
			nightTaskUnit.Init(this);
			nightTaskUnit.removePrarent = this.tmpData[ScheduleTaskCtrl.TaskType.Yotogi].place;
		}
		for (int k = 0; k < num; k++)
		{
			TrainingTaskUnit trainingTaskUnit = Utility.CreatePrefab(this.tmpData[ScheduleTaskCtrl.TaskType.Training].place, "SceneDaily/Schedule/Prefab/DaytimeTaskUnit", true).AddComponent<TrainingTaskUnit>();
			trainingTaskUnit.Init(this);
			trainingTaskUnit.removePrarent = this.tmpData[ScheduleTaskCtrl.TaskType.Training].place;
		}
		for (int l = 0; l < num; l++)
		{
			WorkTaskUnit workTaskUnit = Utility.CreatePrefab(this.tmpData[ScheduleTaskCtrl.TaskType.Work].place, "SceneDaily/Schedule/Prefab/WorkTaskUnit", true).AddComponent<WorkTaskUnit>();
			workTaskUnit.Init(this);
			workTaskUnit.removePrarent = this.tmpData[ScheduleTaskCtrl.TaskType.Work].place;
		}
	}

	private void OnDestroy()
	{
		foreach (KeyValuePair<ScheduleTaskCtrl.TaskType, ScheduleTaskViewer.TemporaryData> keyValuePair in this.tmpData)
		{
			UnityEngine.Object.Destroy(keyValuePair.Value.place);
			foreach (KeyValuePair<string, Texture2D> keyValuePair2 in keyValuePair.Value.icons)
			{
				UnityEngine.Object.DestroyImmediate(keyValuePair2.Value);
			}
			keyValuePair.Value.icons.Clear();
		}
	}

	public void Call(Maid maid, ScheduleMgr.ScheduleTime scheduleTime, Dictionary<ScheduleTaskCtrl.TaskType, List<ScheduleTaskViewer.ViewData>> viewDic, Dictionary<ScheduleMgr.ScheduleTime, ScheduleCSVData.ScheduleBase> currentSetWorkDic)
	{
		if (this.category_button_dic_ == null)
		{
			this.Awake();
		}
		ScheduleTaskViewer.scheduleTime = scheduleTime;
		HashSet<int> hashSet = new HashSet<int>();
		int maxValue = int.MaxValue;
		UIWFTabButton uiwftabButton = null;
		UIWFTabButton uiwftabButton2 = null;
		foreach (KeyValuePair<ScheduleTaskCtrl.TaskType, List<ScheduleTaskViewer.ViewData>> keyValuePair in viewDic)
		{
			this.CallEnableButton(maid, keyValuePair.Key, keyValuePair.Value, ref hashSet, ref maxValue);
		}
		this.currentWorkDic = currentSetWorkDic;
		int num = maxValue;
		ScheduleCSVData.ScheduleBase work_data = null;
		foreach (KeyValuePair<int, UIButton> keyValuePair2 in this.category_button_dic_)
		{
			keyValuePair2.Value.onClick.Clear();
			bool flag = hashSet.Contains(keyValuePair2.Key);
			keyValuePair2.Value.transform.parent.gameObject.SetActive(flag);
			if (flag)
			{
				EventDelegate.Add(keyValuePair2.Value.onClick, new EventDelegate.Callback(this.OnClickFromMainCategoryButton));
				if (uiwftabButton2 == null && num == keyValuePair2.Key)
				{
					uiwftabButton2 = keyValuePair2.Value.gameObject.GetComponent<UIWFTabButton>();
				}
				ScheduleCSVData.ScheduleBase scheduleBase = currentSetWorkDic[scheduleTime];
				if (uiwftabButton == null && scheduleBase != null && scheduleBase.categoryID == keyValuePair2.Key)
				{
					work_data = scheduleBase;
					uiwftabButton = keyValuePair2.Value.gameObject.GetComponent<UIWFTabButton>();
				}
			}
		}
		if (uiwftabButton == null)
		{
			uiwftabButton = uiwftabButton2;
		}
		Utility.ResetNGUI(this.CategoryGrid);
		Utility.ResetNGUI(this.CategoryScrollView);
		UIWFTabPanel componentInChildren = this.CategoryGrid.GetComponentInChildren<UIWFTabPanel>();
		componentInChildren.ResetSelect();
		componentInChildren.UpdateChildren();
		if (uiwftabButton != null)
		{
			componentInChildren.Select(uiwftabButton);
		}
		if (this.descDic == null)
		{
			DescScheduleYotogi component = UTY.GetChildObject(base.gameObject, "DescScheduleYotogi", false).GetComponent<DescScheduleYotogi>();
			component.Init(this.taskCtrl);
			DescScheduleTraining component2 = UTY.GetChildObject(base.gameObject, "DescScheduleTraining", false).GetComponent<DescScheduleTraining>();
			component2.Init(this.taskCtrl);
			DescScheduleWork component3 = UTY.GetChildObject(base.gameObject, "DescScheduleWork", false).GetComponent<DescScheduleWork>();
			component3.Init(this.taskCtrl);
			this.descDic = new Dictionary<ScheduleTaskCtrl.TaskType, DescScheduleBase>();
			this.descDic.Add(ScheduleTaskCtrl.TaskType.Yotogi, component);
			this.descDic.Add(ScheduleTaskCtrl.TaskType.Training, component2);
			this.descDic.Add(ScheduleTaskCtrl.TaskType.Work, component3);
		}
		this.UpdateForDescriptionViewer(work_data);
	}

	private void CallEnableButton(Maid maid, ScheduleTaskCtrl.TaskType type, List<ScheduleTaskViewer.ViewData> viewList, ref HashSet<int> enabled_category, ref int min_category_id)
	{
		this.tmpData[type].views = new List<ScheduleTaskViewer.ViewData>(viewList);
		for (int i = 0; i < this.tmpData[type].views.Count; i++)
		{
			if (!enabled_category.Contains(this.tmpData[type].views[i].schedule.categoryID))
			{
				ScheduleCSVData.ScheduleBase schedule = this.tmpData[type].views[i].schedule;
				int categoryID = schedule.categoryID;
				if (DailyMgr.IsLegacy || schedule.mode != ScheduleCSVData.ScheduleBase.Mode.CM3D2)
				{
					if (!DailyMgr.IsLegacy || schedule.mode != ScheduleCSVData.ScheduleBase.Mode.COM3D)
					{
						if (DailyMgr.IsLegacy)
						{
							if (ScheduleTaskViewer.scheduleTime == ScheduleMgr.ScheduleTime.DayTime)
							{
								if (categoryID != 51)
								{
									goto IL_135;
								}
							}
							else if (ScheduleTaskViewer.scheduleTime == ScheduleMgr.ScheduleTime.Night && (categoryID == 51 || categoryID == 52))
							{
								goto IL_135;
							}
						}
						if (maid.status.heroineType != HeroineType.Sub || (categoryID != 51 && categoryID != 100))
						{
							enabled_category.Add(categoryID);
							min_category_id = System.Math.Min(min_category_id, categoryID);
						}
					}
				}
			}
			IL_135:;
		}
	}

	private void AddButtonEvent(Button button, int num)
	{
		button.onClick.AddListener(delegate()
		{
			this.DoSomething(num);
		});
	}

	private void DoSomething(int num)
	{
		MonoBehaviour.print(num);
	}

	public void onHoverOverEvent(ScheduleCSVData.ScheduleBase data)
	{
		this.UpdateForDescriptionViewer(data);
	}

	public void onHoverOutEvent(ScheduleCSVData.ScheduleBase data)
	{
		if (this.currentWorkDic[ScheduleTaskViewer.scheduleTime] != null && data != null && this.currentWorkDic[ScheduleTaskViewer.scheduleTime].categoryID == data.categoryID)
		{
			this.UpdateForDescriptionViewer(this.currentWorkDic[ScheduleTaskViewer.scheduleTime]);
		}
		else
		{
			this.UpdateForDescriptionViewer(null);
		}
	}

	public void OnClickTaskUnit(TaskUnit select_unit)
	{
		ScheduleTaskCtrl.TaskType taskType = select_unit.taskType;
		for (int i = 0; i < this.UnitGrid.transform.childCount; i++)
		{
			TaskUnit component = this.UnitGrid.transform.GetChild(i).GetComponent<TaskUnit>();
			bool flag = component == select_unit;
			component.SetSelect(flag);
			if (flag)
			{
				this.currentWorkDic[ScheduleTaskViewer.scheduleTime] = component.schedule;
				if (component.schedule.type == ScheduleTaskCtrl.TaskType.Training)
				{
					ScheduleAPI.AddTrainingFacility(this.taskCtrl.ScheduleCtrl.SelectedMaid, component.schedule.id, ScheduleTaskViewer.scheduleTime);
				}
			}
		}
		this.description_viewer_out_draw_ = true;
		this.UpdateForDescriptionViewer(this.currentWorkDic[ScheduleTaskViewer.scheduleTime]);
		if (this.onClickWorkUnitEvent != null)
		{
			this.onClickWorkUnitEvent(select_unit.schedule);
		}
	}

	public TaskUnit GetSelectedTaskUnit()
	{
		for (int i = 0; i < this.UnitGrid.transform.childCount; i++)
		{
			TaskUnit component = this.UnitGrid.transform.GetChild(i).GetComponent<TaskUnit>();
			if (component.isSelected)
			{
				return component;
			}
		}
		return null;
	}

	public void OnClickTaskUnit_Work(TaskUnit select_unit)
	{
		WorkTaskUnit workTaskUnit = (WorkTaskUnit)select_unit;
		if (workTaskUnit.work.workTyp == ScheduleCSVData.WorkType.Basic)
		{
			DescScheduleWork descScheduleWork = (DescScheduleWork)this.descDic[ScheduleTaskCtrl.TaskType.Work];
			descScheduleWork.TaskUnit = workTaskUnit;
			descScheduleWork.EnableFacillityPanel();
		}
		else
		{
			this.OnClickTaskUnit(select_unit);
		}
	}

	private void OnClickFromMainCategoryButton()
	{
		UIButton current = UIButton.current;
		int num = int.Parse(current.name);
		this.selectedCategoryID = num;
		ScheduleTaskCtrl.TaskType type = ScheduleTaskCtrl.TaskType.Yotogi;
		if (num == 51)
		{
			type = ScheduleTaskCtrl.TaskType.Training;
		}
		else if (num == 52)
		{
			type = ScheduleTaskCtrl.TaskType.Work;
		}
		this.CreateTaskUnit(type, current, num);
	}

	private void CreateTaskUnit(ScheduleTaskCtrl.TaskType type, UIButton current, int select_category_id)
	{
		if (type == ScheduleTaskCtrl.TaskType.Yotogi)
		{
			this.description_viewer_out_draw_ = (this.currentWorkDic[ScheduleTaskViewer.scheduleTime] != null && this.currentWorkDic[ScheduleTaskViewer.scheduleTime].categoryID == select_category_id);
			if (this.description_viewer_out_draw_ || this.AlwaysDrawDescriptionViewer)
			{
				this.UpdateForDescriptionViewer(this.currentWorkDic[ScheduleTaskViewer.scheduleTime]);
			}
			else
			{
				this.UpdateForDescriptionViewer(null);
			}
		}
		else if (type == ScheduleTaskCtrl.TaskType.Training)
		{
			if (this.currentWorkDic[ScheduleTaskViewer.scheduleTime].type == ScheduleTaskCtrl.TaskType.Training)
			{
				this.UpdateForDescriptionViewer(this.currentWorkDic[ScheduleTaskViewer.scheduleTime]);
			}
			else
			{
				this.UpdateForDescriptionViewer(null);
			}
		}
		else if (type == ScheduleTaskCtrl.TaskType.Work)
		{
			if (this.currentWorkDic[ScheduleTaskViewer.scheduleTime].type == ScheduleTaskCtrl.TaskType.Work)
			{
				this.UpdateForDescriptionViewer(this.currentWorkDic[ScheduleTaskViewer.scheduleTime]);
			}
			else
			{
				this.UpdateForDescriptionViewer(null);
			}
		}
		this.RemoveTaskUnitAll();
		for (int i = 0; i < this.tmpData[type].views.Count; i++)
		{
			ScheduleTaskViewer.ViewData view_data = this.tmpData[type].views[i];
			if (view_data.schedule.categoryID == this.selectedCategoryID)
			{
				if (view_data.schedule.IsCommon || !view_data.schedule.IsLegacy != DailyMgr.IsLegacy)
				{
					if (type == ScheduleTaskCtrl.TaskType.Training)
					{
						if (this.AddTaskUnitCheck(ScheduleTaskCtrl.TaskType.Training, view_data))
						{
							this.AddTrainingTaskUnit(view_data);
						}
					}
					else if (type == ScheduleTaskCtrl.TaskType.Yotogi)
					{
						if (this.AddTaskUnitCheck(ScheduleTaskCtrl.TaskType.Yotogi, view_data))
						{
							this.AddYotogiTaskUnit(view_data);
						}
					}
					else if (type == ScheduleTaskCtrl.TaskType.Work && this.AddTaskUnitCheck(ScheduleTaskCtrl.TaskType.Work, view_data))
					{
						this.AddWorkTaskUnit(view_data);
					}
				}
			}
		}
		Utility.ResetNGUI(this.UnitGrid);
		Utility.ResetNGUI(this.UnitScrollView);
		GameObject childObject = UTY.GetChildObject(this.CategoryScrollView.transform.parent.gameObject, "Arrow", true);
		if (childObject != null)
		{
			Vector3 position = current.transform.TransformPoint(new Vector3(0f, 0f, 0f));
			Vector3 local_pos = childObject.transform.parent.InverseTransformPoint(position);
			local_pos.x = childObject.transform.localPosition.x;
			local_pos.y += 18f;
			Hashtable args = TweenHash.EaseOutQuint(TweenHash.Type.Position, local_pos, 0.3f);
			iTween.MoveTo(childObject, args);
		}
	}

	private bool AddTaskUnitCheck(ScheduleTaskCtrl.TaskType type, ScheduleTaskViewer.ViewData view_data)
	{
		if (this.tmpData[type].place.transform.childCount <= 0)
		{
			return false;
		}
		ScheduleCSVData.ScheduleBase schedule = view_data.schedule;
		if (!this.tmpData[type].icons.ContainsKey(schedule.icon))
		{
			string text = schedule.icon;
			if (!text.Contains(".tex"))
			{
				text += ".tex";
			}
			if (!GameUty.FileSystem.IsExistentFile(text))
			{
				Debug.LogError("スケジュール" + type.ToString() + "のアイコン画像が見つかりませんでした\n" + text);
				this.tmpData[type].icons.Add(schedule.icon, null);
			}
			else
			{
				Texture2D value = ImportCM.CreateTexture(text);
				this.tmpData[type].icons.Add(schedule.icon, value);
			}
		}
		return true;
	}

	private void AddTrainingTaskUnit(ScheduleTaskViewer.ViewData view_data)
	{
		ScheduleCSVData.Training training = (ScheduleCSVData.Training)view_data.schedule;
		if (training.trainingType == ScheduleCSVData.TrainingType.Trainee && !ScheduleAPI.CanTrainee(this.taskCtrl.ScheduleCtrl.SelectedMaid))
		{
			return;
		}
		TrainingTaskUnit component = this.tmpData[ScheduleTaskCtrl.TaskType.Training].place.transform.GetChild(0).gameObject.GetComponent<TrainingTaskUnit>();
		component.SetData(this, view_data);
		component.transform.SetParent(this.UnitGrid.transform, false);
		component.transform.localPosition = new Vector3(0f, (float)(this.UnitGrid.transform.childCount * 10), 0f);
		component.SetSelect(component.schedule == this.currentWorkDic[ScheduleTaskViewer.scheduleTime]);
		component.isEnabled = view_data.is_enabled;
	}

	private void AddYotogiTaskUnit(ScheduleTaskViewer.ViewData view_data)
	{
		if (GameMain.Instance.CharacterMgr.status.lockNTRPlay)
		{
			ScheduleCSVData.ScheduleBase schedule = view_data.schedule;
			ScheduleCSVData.Yotogi yotogi = (ScheduleCSVData.Yotogi)schedule;
			if (ScheduleCSVData.NetorareFlag.Contains(yotogi.id))
			{
				return;
			}
		}
		NightTaskUnit component = this.tmpData[ScheduleTaskCtrl.TaskType.Yotogi].place.transform.GetChild(0).gameObject.GetComponent<NightTaskUnit>();
		component.SetData(this, view_data);
		component.transform.SetParent(this.UnitGrid.transform, false);
		component.transform.localPosition = new Vector3(0f, (float)(this.UnitGrid.transform.childCount * 10), 0f);
		component.SetSelect(component.schedule == this.currentWorkDic[ScheduleTaskViewer.scheduleTime]);
		component.isEnabled = view_data.is_enabled;
	}

	private void AddWorkTaskUnit(ScheduleTaskViewer.ViewData view_data)
	{
		ScheduleCSVData.Work work = (ScheduleCSVData.Work)view_data.schedule;
		if (!work.facility.isBusiness && work.workTyp != ScheduleCSVData.WorkType.PowerUp)
		{
			return;
		}
		if (work.workTyp == ScheduleCSVData.WorkType.Basic)
		{
			Facility[] facilityArray = GameMain.Instance.FacilityMgr.GetFacilityArray();
			bool flag = false;
			foreach (Facility facility in facilityArray)
			{
				if (!(facility == null))
				{
					if (facility.defaultData.ID == work.facility.ID)
					{
						flag = true;
						break;
					}
				}
			}
			if (!flag)
			{
				return;
			}
		}
		WorkTaskUnit component = this.tmpData[ScheduleTaskCtrl.TaskType.Work].place.transform.GetChild(0).gameObject.GetComponent<WorkTaskUnit>();
		component.SetData(this, view_data);
		component.transform.SetParent(this.UnitGrid.transform, false);
		component.transform.localPosition = new Vector3(0f, (float)(this.UnitGrid.transform.childCount * 10), 0f);
		component.SetSelect(component.schedule == this.currentWorkDic[ScheduleTaskViewer.scheduleTime]);
		component.isEnabled = view_data.is_enabled;
	}

	private void RemoveTaskUnitAll()
	{
		Transform transform = this.UnitGrid.transform;
		List<Transform> list = new List<Transform>();
		for (int i = 0; i < transform.childCount; i++)
		{
			list.Add(transform.GetChild(i));
		}
		for (int j = 0; j < list.Count; j++)
		{
			TaskUnit component = list[j].GetComponent<TaskUnit>();
			component.isEnabled = true;
			component.SetSelect(false);
			list[j].SetParent(component.removePrarent.transform, false);
		}
	}

	private void UpdateForDescriptionViewer(ScheduleCSVData.ScheduleBase work_data)
	{
		if (this.descDic == null)
		{
			return;
		}
		foreach (KeyValuePair<ScheduleTaskCtrl.TaskType, DescScheduleBase> keyValuePair in this.descDic)
		{
			if (work_data != null && work_data.type == keyValuePair.Key)
			{
				keyValuePair.Value.UpdateViewCommon(work_data);
			}
			else
			{
				keyValuePair.Value.UpdateViewCommon(null);
			}
		}
	}

	public ScheduleTaskViewer.TemporaryData TmpData(ScheduleTaskCtrl.TaskType type)
	{
		return this.tmpData[type];
	}

	public static ScheduleMgr.ScheduleTime ScheduleTime
	{
		get
		{
			return ScheduleTaskViewer.scheduleTime;
		}
		set
		{
			ScheduleTaskViewer.scheduleTime = value;
		}
	}

	public ScheduleTaskCtrl taskCtrl;

	public UIGrid CategoryGrid;

	public UIScrollView CategoryScrollView;

	public UIGrid UnitGrid;

	public UIScrollView UnitScrollView;

	public GameObject ParameterViewer;

	public Dictionary<ScheduleTaskCtrl.TaskType, DescScheduleBase> descDic;

	[SerializeField]
	private int selectedCategoryID;

	public bool AlwaysDrawDescriptionViewer;

	public Action<ScheduleCSVData.ScheduleBase> onClickWorkUnitEvent;

	private Dictionary<int, UIButton> category_button_dic_ = new Dictionary<int, UIButton>();

	private Dictionary<ScheduleTaskCtrl.TaskType, ScheduleTaskViewer.TemporaryData> tmpData = new Dictionary<ScheduleTaskCtrl.TaskType, ScheduleTaskViewer.TemporaryData>();

	private Dictionary<ScheduleMgr.ScheduleTime, ScheduleCSVData.ScheduleBase> currentWorkDic = new Dictionary<ScheduleMgr.ScheduleTime, ScheduleCSVData.ScheduleBase>();

	private static ScheduleMgr.ScheduleTime scheduleTime;

	private bool description_viewer_out_draw_;

	public struct ViewData
	{
		public ScheduleTaskCtrl.TaskButton taskButton;

		public ScheduleCSVData.ScheduleBase schedule;

		public bool is_enabled;
	}

	public class TemporaryData
	{
		public GameObject place;

		public List<ScheduleTaskViewer.ViewData> views;

		public Dictionary<string, Texture2D> icons = new Dictionary<string, Texture2D>();
	}
}
