using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using wf;

public class SceneScenarioSelect : MonoBehaviour
{
	private void Start()
	{
		if (GameMain.Instance.CharacterMgr.status.isDaytime)
		{
			GameMain.Instance.BgMgr.ChangeBg(this.m_DayBGName);
		}
		else
		{
			GameMain.Instance.BgMgr.ChangeBg(this.m_NightBGName);
		}
		GameMain.Instance.SoundMgr.PlayBGM(this.m_BGMName, 0f, true);
		GameMain.Instance.MainLight.Reset();
		GameMain.Instance.CharacterMgr.DeactivateCharaAll();
		GameMain.Instance.MainCamera.Reset(CameraMain.CameraType.Target, true);
		GameMain.Instance.SoundMgr.VoiceStopAll();
		GameMain.Instance.SoundMgr.StopSe();
		this.m_AdvkagMgr = GameMain.Instance.ScriptMgr.adv_kag;
		if (!this.m_AdvkagMgr.tag_backup.ContainsKey("cancel_label"))
		{
			Debug.LogError("SceneScenarioSelect.cs:Cancel実行時に飛ぶラベルが設定されてません");
		}
		else
		{
			this.m_CancelLabel = this.m_AdvkagMgr.tag_backup["cancel_label"];
		}
		if (!this.m_AdvkagMgr.tag_backup.ContainsKey("label"))
		{
			Debug.LogError("SceneScenarioSelect.cs:イベント実行時に飛ぶラベルが設定されてません");
		}
		else
		{
			this.m_OkLabel = this.m_AdvkagMgr.tag_backup["label"];
		}
		EventDelegate.Add(this.m_OkButton.onClick, new EventDelegate.Callback(this.PushOkButton));
		EventDelegate.Add(this.m_CancelButton.onClick, new EventDelegate.Callback(this.PushCancelButton));
		this.SetPlayableText();
		this.SetScenarioPlate();
		GameMain.Instance.MainCamera.FadeIn(0.5f, false, null, true, true, default(Color));
	}

	private void PushOkButton()
	{
		if (string.IsNullOrEmpty(this.m_OkLabel))
		{
			return;
		}
		this.m_JumpLabel = this.m_OkLabel;
		GameMain.Instance.MainCamera.FadeOut(0.5f, false, new CameraMain.dgOnCompleteFade(this.JumpNextLabel), true, default(Color));
	}

	private void PushCancelButton()
	{
		if (string.IsNullOrEmpty(this.m_CancelLabel))
		{
			return;
		}
		this.m_JumpLabel = this.m_CancelLabel;
		GameMain.Instance.MainCamera.FadeOut(0.5f, false, new CameraMain.dgOnCompleteFade(this.JumpNextLabel), true, default(Color));
	}

	private void JumpNextLabel()
	{
		if (this.m_JumpLabel == this.m_OkLabel)
		{
			this.m_CurrentScenario.ScenarioPlay(this.m_SelectedMaid);
			NDebug.Assert(!string.IsNullOrEmpty(this.m_CurrentScenario.ScenarioScript), "SceneScenarioSelect.cs:選択したシナリオのスクリプトファイル名が設定されてません");
			NDebug.Assert(!string.IsNullOrEmpty(this.m_CurrentScenario.ScriptLabel), "SceneScenarioSelect.cs:選択したシナリオのラベル名が設定されてません");
			string text = this.m_CurrentScenario.ScenarioScript;
			if (this.m_SelectedMaid.Count > 0 && text.IndexOf("?") >= 0)
			{
				text = ScriptManager.ReplacePersonal(this.m_SelectedMaid[0], text);
			}
			GameMain.Instance.ScriptMgr.EvalScript("&tf['scenario_file_name'] = '" + text + "';");
			GameMain.Instance.ScriptMgr.EvalScript("&tf['label_name'] = '" + this.m_CurrentScenario.ScriptLabel + "';");
		}
		this.m_AdvkagMgr.JumpLabel(this.m_JumpLabel);
		this.m_AdvkagMgr.Exec();
	}

	private void OnSelectScenario()
	{
		if (!UIWFSelectButton.current.isSelected)
		{
			return;
		}
		this.m_CurrentScenario = this.m_ScenarioButtonpair[(UIWFTabButton)UIWFSelectButton.current];
		this.m_ContentsLabel.text = this.m_CurrentScenario.EventContents;
		for (int i = 0; i < this.m_PlayableTextUIList.Count; i++)
		{
			GameObject gameObject = this.m_PlayableTextUIList[i].transform.parent.gameObject;
			gameObject.SetActive(false);
			if (i < this.m_CurrentScenario.ConditionText.Count<string>())
			{
				string text = this.m_CurrentScenario.ConditionText[i];
				if (!string.IsNullOrEmpty(text))
				{
					gameObject.SetActive(true);
					this.m_PlayableTextUIList[i].text = text;
				}
			}
		}
		this.UpdateCharaUI();
	}

	private void UpdateCharaUI()
	{
		this.m_SelectedMaid.Clear();
		if (this.m_CurrentScenario.IsFixedMaid)
		{
			this.m_CharaSelectMgr.Create(CharacterSelectManager.Type.Multiple, this.m_CurrentScenario.EventMaidCount, true);
			for (int i = 0; i < this.m_CurrentScenario.EventMaidCount; i++)
			{
				this.m_SelectedMaid.Add(this.m_CurrentScenario.GetEventMaid(i));
			}
			base.StartCoroutine(this.ColiderOff());
		}
		else
		{
			CharacterSelectManager.CallBackOnSelect callBackCallBackOnSelect = delegate(Maid maid)
			{
				this.m_SelectedMaid.Clear();
				this.m_SelectedMaid.Add(maid);
			};
			this.m_CharaSelectMgr.SetCallBackCallBackOnSelect(callBackCallBackOnSelect);
			this.m_CharaSelectMgr.Create(CharacterSelectManager.Type.Select, 3, true);
		}
		this.m_CharaSelectMgr.big_thumbnail.Visible = false;
		for (int j = 0; j < GameMain.Instance.CharacterMgr.GetStockMaidCount(); j++)
		{
			Maid stockMaid = GameMain.Instance.CharacterMgr.GetStockMaid(j);
			if (!this.m_CurrentScenario.ExistEventMaid(stockMaid))
			{
				this.m_CharaSelectMgr.RemoveMaidPlate(stockMaid);
			}
		}
		if (this.m_CurrentScenario.IsFixedMaid || this.m_CurrentScenario.EventMaidCount == 0)
		{
			return;
		}
		if (this.m_CurrentScenario.EventMaidCount != GameMain.Instance.CharacterMgr.GetStockMaidCount())
		{
			List<Transform> childList = this.m_CharaSelectMgr.MaidPlateParentGrid.GetChildList();
			this.m_CharaSelectMgr.SelectMaid(childList[0].GetComponent<MaidPlate>().maid);
		}
	}

	private IEnumerator ColiderOff()
	{
		yield return null;
		List<Transform> maid_plates = this.m_CharaSelectMgr.MaidPlateParentGrid.GetChildList();
		foreach (Transform transform in maid_plates)
		{
			transform.GetComponentInChildren<BoxCollider>().enabled = false;
		}
		yield break;
	}

	private void SetScenarioPlate()
	{
		foreach (ScenarioData scenarioData in GameMain.Instance.ScenarioSelectMgr.GetAllScenarioData())
		{
			if (scenarioData.IsPlayable)
			{
				GameObject gameObject = Utility.CreatePrefab(this.m_ScenarioScroll.Grid.gameObject, "SceneScenarioSelect/Prefab/ScenarioPlate", true);
				UTY.GetChildObject(gameObject, "Title", false).GetComponent<UILabel>().text = scenarioData.Title;
				gameObject.name = scenarioData.NotLineTitle;
				UTY.GetChildObject(gameObject, "Icon", false).GetComponent<UI2DSprite>().sprite2D = Resources.Load<Sprite>("SceneScenarioSelect/Sprite/" + scenarioData.IconName);
				UIWFTabButton componentInChildren = gameObject.GetComponentInChildren<UIWFTabButton>();
				EventDelegate.Add(componentInChildren.onSelect, new EventDelegate.Callback(this.OnSelectScenario));
				this.m_ScenarioButtonpair.Add(componentInChildren, scenarioData);
			}
		}
		this.m_ScenarioScroll.Grid.repositionNow = true;
		this.m_ScenarioScroll.ScrollView.ResetPosition();
		this.m_UIWFTabPanel.UpdateChildren();
		if (this.m_ScenarioButtonpair.Count<KeyValuePair<UIWFTabButton, ScenarioData>>() > 0)
		{
			this.m_UIWFTabPanel.Select(this.m_ScenarioButtonpair.Keys.First<UIWFTabButton>());
		}
		else
		{
			this.m_OkButton.isEnabled = false;
		}
	}

	private void SetPlayableText()
	{
		for (int i = 0; i < Enum.GetValues(typeof(ScenarioData.PlayableCondition)).Length; i++)
		{
			GameObject gameObject = Utility.CreatePrefab(this.m_PlayableScroll.Grid.gameObject, "SceneScenarioSelect/Prefab/PlayableText", true);
			UTY.GetChildObject(gameObject, "Number", false).GetComponent<UILabel>().text = (i + 1).ToString();
			this.m_PlayableTextUIList.Add(UTY.GetChildObject(gameObject, "Text", false).GetComponent<UILabel>());
			gameObject.SetActive(false);
		}
		this.m_PlayableScroll.Grid.repositionNow = true;
		this.m_PlayableScroll.ScrollView.ResetPosition();
	}

	[SerializeField]
	[Header("昼背景名")]
	private string m_DayBGName = "Shitsumu";

	[SerializeField]
	[Header("夜背景名")]
	private string m_NightBGName = "Shitsumu_Night";

	[SerializeField]
	[Header("BGM名")]
	private string m_BGMName = "BGM009.ogg";

	[SerializeField]
	[Header("シナリオプレート列挙に使うもの")]
	private SceneScenarioSelect.NeedScrollUI m_ScenarioScroll;

	[SerializeField]
	private UIWFTabPanel m_UIWFTabPanel;

	[SerializeField]
	[Header("シナリオ内容表示UI")]
	private UILabel m_ContentsLabel;

	[SerializeField]
	private SceneScenarioSelect.NeedScrollUI m_PlayableScroll;

	private List<UILabel> m_PlayableTextUIList = new List<UILabel>();

	[SerializeField]
	[Header("")]
	private UIButton m_OkButton;

	[SerializeField]
	private UIButton m_CancelButton;

	[SerializeField]
	[Header("キャラ選択管理クラス")]
	private CharacterSelectManager m_CharaSelectMgr;

	private string m_OkLabel;

	private string m_CancelLabel;

	private string m_JumpLabel;

	private ADVKagManager m_AdvkagMgr;

	private ScenarioData m_CurrentScenario;

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

	private Dictionary<UIWFTabButton, ScenarioData> m_ScenarioButtonpair = new Dictionary<UIWFTabButton, ScenarioData>();

	[Serializable]
	private class NeedScrollUI
	{
		public UIScrollView ScrollView;

		public UIGrid Grid;
	}
}
