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

public class FreeModeSceneSelectBase : WfScreenChildren
{
	public override void Awake()
	{
		base.Awake();
		EventDelegate.Add(UTY.GetChildObject(base.root_obj, "Cancel", false).GetComponent<UIButton>().onClick, new EventDelegate.Callback(this.OnClickCancel));
		this.ok_btn_ = UTY.GetChildObject(base.root_obj, "OK", false).GetComponent<UIButton>();
		EventDelegate.Add(this.ok_btn_.onClick, new EventDelegate.Callback(this.OnClickOk));
		if (this.mode == FreeModeSceneSelectBase.SelectMode.Vip)
		{
			this.night_task_viewer_ = UTY.GetChildObject(base.root_obj, "TaskViewer", false).GetComponent<ScheduleTaskViewer>();
			this.night_task_viewer_.gameObject.SetActive(true);
			this.night_task_viewer_.AlwaysDrawDescriptionViewer = true;
		}
	}

	protected override void OnCall()
	{
		SubtitleMovieManager.DestroyGlobalInstance();
		this.is_scenario_load_ = false;
		if (this.maid_ != null)
		{
			this.maid_.Visible = true;
			return;
		}
		this.mgr_ = (base.parent_mgr as SceneFreeModeSelectManager);
		NDebug.AssertNull(this.mgr_ != null);
		this.maid_ = GameMain.Instance.CharacterMgr.GetMaid(0);
		this.freemode_item_list_ = UTY.GetChildObject(base.root_obj, "FreeModeItemList", false).GetComponent<FreeModeItemList>();
		bool isEnabled;
		if (this.mode == FreeModeSceneSelectBase.SelectMode.Story)
		{
			isEnabled = this.freemode_item_list_.SetList(FreeModeItemEveryday.CreateItemEverydayList(FreeModeItemEveryday.ScnearioType.Story, null).ToArray());
		}
		else if (this.mode == FreeModeSceneSelectBase.SelectMode.Everyday)
		{
			isEnabled = this.freemode_item_list_.SetList(FreeModeItemEveryday.CreateItemEverydayList(FreeModeItemEveryday.ScnearioType.Nitijyou, this.maid_.status).ToArray());
		}
		else
		{
			this.freemode_item_list_.gameObject.SetActive(false);
			this.night_task_viewer_.gameObject.SetActive(true);
			ScheduleCSVData.Yotogi yotogi = null;
			int num = int.MaxValue;
			int num2 = int.MaxValue;
			this.vip_list_ = FreeModeItemVip.CreateItemVipList(this.maid_.status);
			List<ScheduleTaskViewer.ViewData> list = new List<ScheduleTaskViewer.ViewData>();
			foreach (FreeModeItemVip freeModeItemVip in this.vip_list_)
			{
				ScheduleTaskViewer.ViewData item = default(ScheduleTaskViewer.ViewData);
				item.is_enabled = freeModeItemVip.is_enabled;
				item.schedule = freeModeItemVip.vip_data;
				if (!GameMain.Instance.CharacterMgr.status.lockNTRPlay || !ScheduleCSVData.NetorareFlag.Contains(item.schedule.id))
				{
					if (ScheduleAPI.VisibleNightWork(item.schedule.id, this.maid_, false))
					{
						list.Add(item);
						if (item.is_enabled)
						{
							if (item.schedule.categoryID == num)
							{
								if (item.schedule.id <= num2)
								{
									yotogi = (ScheduleCSVData.Yotogi)item.schedule;
									num2 = item.schedule.id;
								}
							}
							else if (item.schedule.categoryID <= num)
							{
								yotogi = (ScheduleCSVData.Yotogi)item.schedule;
								num = item.schedule.categoryID;
								num2 = item.schedule.id;
							}
						}
					}
				}
			}
			this.night_task_viewer_.onClickWorkUnitEvent = delegate(ScheduleCSVData.ScheduleBase click_data)
			{
				if (click_data == null)
				{
					this.freemode_item_list_.select_item_data = null;
					return;
				}
				foreach (FreeModeItemVip freeModeItemVip2 in this.vip_list_)
				{
					if (freeModeItemVip2.vip_data == click_data)
					{
						this.freemode_item_list_.select_item_data = freeModeItemVip2;
						break;
					}
				}
			};
			this.night_task_viewer_.onClickWorkUnitEvent(yotogi);
			Dictionary<ScheduleTaskCtrl.TaskType, List<ScheduleTaskViewer.ViewData>> dictionary = new Dictionary<ScheduleTaskCtrl.TaskType, List<ScheduleTaskViewer.ViewData>>();
			Dictionary<ScheduleMgr.ScheduleTime, ScheduleCSVData.ScheduleBase> dictionary2 = new Dictionary<ScheduleMgr.ScheduleTime, ScheduleCSVData.ScheduleBase>();
			dictionary.Add(ScheduleTaskCtrl.TaskType.Yotogi, list);
			dictionary2.Add(ScheduleMgr.ScheduleTime.Night, yotogi);
			this.night_task_viewer_.Call(this.maid_, ScheduleMgr.ScheduleTime.Night, dictionary, dictionary2);
			isEnabled = (this.freemode_item_list_.select_item_data != null);
		}
		this.ok_btn_.isEnabled = isEnabled;
		this.maid_.Visible = true;
	}

	private void OnClickOk()
	{
		if (this.freemode_item_list_ == null || this.freemode_item_list_.select_item_data == null)
		{
			return;
		}
		GameMain.Instance.SoundMgr.StopBGM(0.3f);
		this.is_scenario_load_ = true;
		AbstractFreeModeItem select_item_data = this.freemode_item_list_.select_item_data;
		string text = select_item_data.play_file_name;
		text = ScriptManager.ReplacePersonal(this.maid_, text);
		GameMain.Instance.ScriptMgr.EvalScript("tf['シーン再生_ファイル名'] = '" + text + "';");
		if (select_item_data.type == AbstractFreeModeItem.ItemType.Vip)
		{
			this.mgr_.CallScenePlayVip();
		}
		else if (select_item_data.type == AbstractFreeModeItem.ItemType.Normal)
		{
			this.mgr_.CallScenePlayEveryday();
		}
		else if (select_item_data.type == AbstractFreeModeItem.ItemType.MainStory)
		{
			this.mgr_.CallScenePlayMainStory();
		}
	}

	protected override void OnFinish()
	{
		base.OnFinish();
		if (this.is_scenario_load_)
		{
			GameMain.Instance.MainLight.Reset();
			GameMain.Instance.CharacterMgr.ResetCharaPosAll();
			GameMain.Instance.MainCamera.Reset(CameraMain.CameraType.Target, true);
			GameMain.Instance.SoundMgr.VoiceStopAll();
			GameMain.Instance.CharacterMgr.SetCharaAllPos(Vector3.zero);
			GameMain.Instance.CharacterMgr.SetCharaAllRot(Vector3.zero);
			GameMain.Instance.MainCamera.SetTargetOffset(Vector3.zero, false);
			GameMain.Instance.ScriptMgr.StopMotionScript();
		}
		this.is_scenario_load_ = false;
	}

	private void OnClickCancel()
	{
		this.mgr_.CallScreen("Select");
	}

	public FreeModeSceneSelectBase.SelectMode mode;

	private Maid maid_;

	private SceneFreeModeSelectManager mgr_;

	private UIButton ok_btn_;

	private FreeModeItemList freemode_item_list_;

	private ScheduleTaskViewer night_task_viewer_;

	private bool is_scenario_load_;

	private List<FreeModeItemVip> vip_list_;

	public enum SelectMode
	{
		Everyday,
		Vip,
		Story
	}
}
