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

public class YotogiOldVRStageSelectManager : WfFadeBehaviour
{
	public YotogiOld.StageData selectStageData { get; private set; }

	public int selectSpotNo { get; private set; }

	public bool visibledCancelBtn
	{
		get
		{
			return this.visibledCancelBtnValue;
		}
		set
		{
			this.visibledCancelBtnValue = value;
			this.UpdateCancelButton();
		}
	}

	public void Awake()
	{
		Skill.Old.CreateData();
		this.stageList = new List<YotogiOld.StageData>();
		foreach (KeyValuePair<YotogiOld.Stage, YotogiOld.StageData> keyValuePair in YotogiOld.vr_stage_data_list)
		{
			this.stageList.Add(keyValuePair.Value);
		}
		this.stageList.Sort((YotogiOld.StageData a, YotogiOld.StageData b) => a.sort_id - b.sort_id);
		this.unitList = new List<StageOldUnitCircle>();
		for (int i = 0; i < this.stageList.Count; i++)
		{
			GameObject gameObject = Utility.CreatePrefab(this.contentGrid.gameObject, "SceneYotogi/StageSelect/Prefab/Old/StageUnitCircle", true);
			StageOldUnitCircle component = gameObject.GetComponent<StageOldUnitCircle>();
			component.SetStageData(this.stageList[i]);
			component.SetOnSelectEvent(new Action<StageOldUnitCircle>(this.OnUnitClick));
			component.SetOnFinishAnimeEvent(new Action<StageOldUnitCircle>(this.OnUnitAnimeEnd));
			this.unitList.Add(component);
		}
		this.contentGrid.Reposition();
		EventDelegate.Add(this.cancelBtn.onClick, new EventDelegate.Callback(this.OnCancel));
		this.selectStageData = null;
		this.selectSpotNo = -1;
		this.mode = YotogiOldVRStageSelectManager.Mode.StageSelect;
		this.widget.alpha = 0f;
	}

	public override void OnStartedFadeIn()
	{
		this.widget.alpha = 0f;
		this.fadeInStartBgAlpha = this.bgSprite.alpha;
		this.fadeInStartBgSize = new Vector2((float)this.bgSprite.width, (float)this.bgSprite.height);
		this.mode = YotogiOldVRStageSelectManager.Mode.StageSelect;
		this.cancelBtn.normalSprite = "vr_cancel";
		this.UpdateCancelButton();
		for (int i = 0; i < this.unitList.Count; i++)
		{
			this.unitList[i].SetStageData(this.stageList[i]);
			this.unitList[i].transform.localRotation = Quaternion.identity;
		}
	}

	public override void OnStartedFadeOut()
	{
		this.uiMask.enabled = true;
	}

	public override void OnUpdateFadeIn(float val)
	{
		this.uiMask.enabled = true;
		this.bgSprite.alpha = Mathf.Lerp(this.fadeInStartBgAlpha, 1f, val);
		this.bgSprite.width = (int)Mathf.Lerp(this.fadeInStartBgSize.x, this.bgSize.x, val);
		this.bgSprite.height = (int)Mathf.Lerp(this.fadeInStartBgSize.y, this.bgSize.y, val);
		this.widget.alpha = val;
	}

	public override void OnUpdateFadeOut(float val)
	{
		this.widget.alpha = val;
	}

	public override void OnCompleteFadeIn()
	{
		this.uiMask.enabled = false;
	}

	public override void OnCompleteFadeOut()
	{
		this.OnCompleteFadeIn();
	}

	public void SetStageData(YotogiOld.Stage stage, int spot_no)
	{
		NDebug.Assert(YotogiOld.vr_stage_data_list.ContainsKey(stage), "vrステージではありません\n" + stage.ToString());
		NDebug.Assert(spot_no < YotogiOld.vr_stage_data_list[stage].spot_data.Length, "スポット数が不正です\n" + stage.ToString() + " : " + spot_no.ToString());
		this.selectStageData = YotogiOld.vr_stage_data_list[stage];
		this.selectSpotNo = spot_no;
	}

	private void OnCancel()
	{
		if (this.mode == YotogiOldVRStageSelectManager.Mode.StageSelect)
		{
			EventDelegate.Execute(this.onClickCancelEvent);
			return;
		}
		this.mode = YotogiOldVRStageSelectManager.Mode.StageSelect;
		this.cancelBtn.normalSprite = "vr_cancel";
		this.UpdateCancelButton();
		int num = 0;
		while (num < this.unitList.Count && num < this.selectStageData.spot_data.Length)
		{
			this.unitList[num].PlayAnimeFadeOut();
			num++;
		}
	}

	private void OnUnitClick(StageOldUnitCircle unit)
	{
		if (this.mode == YotogiOldVRStageSelectManager.Mode.StageSelect)
		{
			this.uiMask.enabled = true;
			this.selectStageData = unit.stage_data;
			this.selectSpotNo = -1;
			this.mode = YotogiOldVRStageSelectManager.Mode.SpotSelect;
			for (int i = 0; i < this.unitList.Count; i++)
			{
				this.unitList[i].PlayAnimeFadeOut();
			}
			this.cancelBtn.normalSprite = "vr_back";
			this.cancelBtn.gameObject.SetActive(true);
		}
		else if (this.mode == YotogiOldVRStageSelectManager.Mode.SpotSelect)
		{
			this.selectSpotNo = unit.spot_no;
			EventDelegate.Execute(this.onClickEnterEvent);
		}
	}

	private void OnUnitAnimeEnd(StageOldUnitCircle unit)
	{
		int num = 0;
		while (num < this.unitList.Count && unit != this.unitList[num])
		{
			num++;
		}
		if (this.unitList.Count <= num)
		{
			return;
		}
		this.uiMask.enabled = false;
		if (this.mode == YotogiOldVRStageSelectManager.Mode.SpotSelect)
		{
			if (num < this.selectStageData.spot_data.Length)
			{
				unit.SetStageSpotData(this.selectStageData, num);
				unit.PlayAnimeFadeIn();
			}
			this.cancelBtn.gameObject.SetActive(true);
		}
		else if (this.mode == YotogiOldVRStageSelectManager.Mode.StageSelect)
		{
			if (num == 0)
			{
				for (int i = 0; i < this.unitList.Count; i++)
				{
					if (i >= this.selectStageData.spot_data.Length)
					{
						this.unitList[i].SetStageData(this.stageList[i]);
						this.unitList[i].PlayAnimeFadeIn();
					}
				}
				this.UpdateCancelButton();
			}
			this.selectSpotNo = -1;
			unit.SetStageData(this.stageList[num]);
			unit.PlayAnimeFadeIn();
		}
	}

	private void UpdateCancelButton()
	{
		if (this.mode == YotogiOldVRStageSelectManager.Mode.StageSelect)
		{
			this.cancelBtn.gameObject.SetActive(this.visibledCancelBtnValue);
		}
		else
		{
			this.cancelBtn.gameObject.SetActive(true);
		}
	}

	[SerializeField]
	private UISprite bgSprite;

	[SerializeField]
	private Vector2 bgSize;

	[SerializeField]
	private UIWFHitMasks uiMask;

	[SerializeField]
	private UIButton cancelBtn;

	[SerializeField]
	private UIGrid contentGrid;

	[SerializeField]
	private UIWidget widget;

	[HideInInspector]
	public List<EventDelegate> onClickEnterEvent = new List<EventDelegate>();

	[HideInInspector]
	public List<EventDelegate> onClickCancelEvent = new List<EventDelegate>();

	private YotogiOldVRStageSelectManager.Mode mode;

	private List<YotogiOld.StageData> stageList;

	private List<StageOldUnitCircle> unitList;

	private Vector2 fadeInStartBgSize;

	private float fadeInStartBgAlpha;

	private bool visibledCancelBtnValue;

	private enum Mode
	{
		StageSelect,
		SpotSelect
	}
}
