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

public class SceneCasinoShop : KasaSceneMgr<SceneCasinoShop>
{
	public CasinoShopItem SelectTrywear
	{
		get
		{
			return this.m_SelectTrywear;
		}
	}

	protected override void Awake()
	{
		this.m_ScrollPanel = this.m_ShopItemUI.ScrollView.GetComponent<UIPanel>();
		GameMain.Instance.CharacterMgr.SetActiveMaid(CasinoDataMgr.Instance.DealerMaid, 0);
		this.m_TrywearMaid = CasinoDataMgr.Instance.DealerMaid;
		Facility.CostumeType typeCostume = CasinoDataMgr.Instance.CurrentCasino.typeCostume;
		if (typeCostume != Facility.CostumeType.Heroine)
		{
			if (typeCostume != Facility.CostumeType.Default)
			{
				if (typeCostume == Facility.CostumeType.Edit)
				{
					CasinoDataMgr.Instance.CurrentCasino.UpdateMaidCostumeToEditCostume(this.m_TrywearMaid, false);
				}
			}
			else
			{
				CasinoDataMgr.Instance.CurrentCasino.UpdateMaidCostumeToDefaultCostume(this.m_TrywearMaid);
			}
			CasinoDataMgr.Instance.DealerMaid.AllProcPropSeqStart();
		}
		this.m_TrywearMaid.Visible = true;
		this.m_TrywearMaid.EyeToCamera(Maid.EyeMoveType.目と顔を向ける, 0f);
		base.Awake();
		this.m_CamMoveSupport = base.GetComponent<WfCameraMoveSupport>();
		this.m_InfoData.UIobj = this.m_ItemInfoUI;
		this.m_InfoData.Icon = this.m_ItemInfoUI.transform.Find("Base/Icon").GetComponent<UI2DSprite>();
		this.m_InfoData.InfoLabel = this.m_ItemInfoUI.transform.Find("Base/Info").GetComponent<UILabel>();
		this.m_InfoData.Namelabel = this.m_ItemInfoUI.transform.Find("Base/Title").GetComponent<UILabel>();
		NDebug.Assert(GameMain.Instance.ScriptMgr.adv_kag.tag_backup.ContainsKey("label"), "OK時のラベルが未指定です");
		this.m_JumpLabel = GameMain.Instance.ScriptMgr.adv_kag.tag_backup["label"];
		EventDelegate.Add(this.m_TrywearButton.onClick, new EventDelegate.Callback(this.TryWear));
		EventDelegate.Add(this.m_TrycancelButton.onClick, new EventDelegate.Callback(this.TryCancel));
		EventDelegate.Add(this.m_CamRecetButton.onClick, new EventDelegate.Callback(this.CamRecet));
		EventDelegate.Add(this.m_OkButton.onClick, new EventDelegate.Callback(this.ClickOKButton));
		this.m_TrywearButton.isEnabled = false;
		this.m_TrycancelButton.isEnabled = false;
		this.CreateCategoryUI();
	}

	private void Start()
	{
		this.m_CategoryUI.TabPanel.Select(this.m_ButtonCategoryPair.Keys.First<UIWFTabButton>());
		ExChangeUI instance = ExChangeUI.Instance;
		instance.FadeOutStartAction = (Action)Delegate.Combine(instance.FadeOutStartAction, new Action(this.UpdateUIState));
		ExChangeUI instance2 = ExChangeUI.Instance;
		instance2.FadeInStartAction = (Action)Delegate.Combine(instance2.FadeInStartAction, new Action(delegate()
		{
			this.m_NguiEnable = false;
		}));
		ExChangeUI instance3 = ExChangeUI.Instance;
		instance3.FadeOutStartAction = (Action)Delegate.Combine(instance3.FadeOutStartAction, new Action(delegate()
		{
			this.m_NguiEnable = true;
		}));
		this.UpdateUIState();
		base.StartCoroutine(this.ScrollRecet());
	}

	private void LateUpdate()
	{
		this.m_NguiCam.EnableProcess &= this.m_NguiEnable;
	}

	private IEnumerator ScrollRecet()
	{
		for (int i = 0; i < 5; i++)
		{
			yield return null;
		}
		this.m_ItemSrollBar.value = 0f;
		yield break;
	}

	protected override void LoadEnd()
	{
		GameMain.Instance.ScriptMgr.LoadMotionScript(0, false, "h_kaiwa_tati_001.ks", "*会話立ち待機", string.Empty, string.Empty, false, true);
		base.StartCoroutine(this.AnimateWait());
	}

	private IEnumerator AnimateWait()
	{
		for (int i = 0; i < 5; i++)
		{
			yield return null;
		}
		Transform head_bone = this.m_TrywearMaid.body0.GetBone("Bip01 Head");
		this.m_TargetPos = head_bone.position + this.m_HeadCamOffset;
		base.SetCamera();
		this.m_CamMoveSupport.SaveCameraPosition();
		base.LoadEnd();
		yield break;
	}

	private void CreateCategoryUI()
	{
		foreach (CasinoShopItem.Category category in (CasinoShopItem.Category[])Enum.GetValues(typeof(CasinoShopItem.Category)))
		{
			GameObject gameObject = Utility.CreatePrefab(this.m_CategoryUI.Grid.gameObject, "SceneCasinoShop/Prefab/CategoryUI", true);
			UILabel componentInChildren = gameObject.GetComponentInChildren<UILabel>();
			componentInChildren.text = ((category != CasinoShopItem.Category.Costume) ? "施設強化素材" : "コスチューム");
			UIWFTabButton componentInChildren2 = gameObject.GetComponentInChildren<UIWFTabButton>();
			this.m_ButtonCategoryPair.Add(componentInChildren2, category);
			EventDelegate.Add(componentInChildren2.onSelect, new EventDelegate.Callback(this.SelectCategory));
		}
		this.m_CategoryUI.Grid.repositionNow = true;
		this.m_CategoryUI.ScrollView.ResetPosition();
		this.m_CategoryUI.TabPanel.UpdateChildren();
	}

	private void SelectCategory()
	{
		IEnumerator enumerator = this.m_ShopItemUI.Grid.transform.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				Transform transform = (Transform)obj;
				UnityEngine.Object.Destroy(transform.gameObject);
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		this.m_TrywearButton.isEnabled = false;
		this.CreateItemUI();
		this.m_AllowposStart = this.m_CategoryAllow.transform.position;
		this.m_AllowposEnd = this.m_AllowposStart;
		this.m_AllowposEnd.y = UIWFTabButton.current.transform.position.y;
		base.StartCoroutine(KasaiUtility.TimeCroutine(this.m_AllowMove, new Action<float>(this.AllowMovement), null));
	}

	private void AllowMovement(float time)
	{
		float t = KasaiUtility.SinRate01(time, this.m_AllowMove, false, false);
		this.m_CategoryAllow.transform.position = Vector3.Lerp(this.m_AllowposStart, this.m_AllowposEnd, t);
	}

	private void CreateItemUI()
	{
		CasinoShopItem.Category category = this.m_ButtonCategoryPair[UIWFTabButton.current];
		this.m_ItemUIList.Clear();
		foreach (CasinoShopItem item_data in CasinoDataMgr.Instance.GetShopItem(category))
		{
			GameObject gameObject = Utility.CreatePrefab(this.m_ShopItemUI.Grid.gameObject, "SceneCasinoShop/Prefab/CasinoShopItem", true);
			CasinoItemUI component = gameObject.GetComponent<CasinoItemUI>();
			component.Init(item_data);
			this.m_ItemUIList.Add(component);
		}
		this.m_ShopItemUI.Grid.repositionNow = true;
		this.m_ShopItemUI.ScrollView.ResetPosition();
		this.m_ShopItemUI.TabPanel.UpdateChildren();
	}

	private void RecetTrywear(Action load_end = null)
	{
		Facility.CostumeType costume_type = CasinoDataMgr.Instance.CurrentCasino.typeCostume;
		MPN[] facilityCostumeEnableMPN = FacilityDataTable.GetFacilityCostumeEnableMPN();
		foreach (MPN mpn in facilityCostumeEnableMPN)
		{
			if (this.m_BackUpwear.ContainsKey(mpn))
			{
				this.m_TrywearMaid.SetProp(mpn, this.m_BackUpwear[mpn].strFileName, this.m_BackUpwear[mpn].nFileNameRID, false, false);
			}
		}
		this.m_TrywearMaid.body0.SetMaskMode(TBody.MaskMode.None);
		this.m_TrywearMaid.AllProcPropSeqStart();
		if (costume_type != Facility.CostumeType.Heroine)
		{
			Action end_call = delegate()
			{
				if (costume_type != Facility.CostumeType.Default)
				{
					if (costume_type == Facility.CostumeType.Edit)
					{
						CasinoDataMgr.Instance.CurrentCasino.UpdateMaidCostumeToEditCostume(this.m_TrywearMaid, false);
					}
				}
				else
				{
					CasinoDataMgr.Instance.CurrentCasino.UpdateMaidCostumeToDefaultCostume(this.m_TrywearMaid);
				}
				this.m_TrywearMaid.AllProcPropSeqStart();
				this.StartCoroutine(KasaiUtility.CharaLoadWait(load_end));
			};
			base.StartCoroutine(KasaiUtility.CharaLoadWait(end_call));
		}
		else
		{
			base.StartCoroutine(KasaiUtility.CharaLoadWait(load_end));
		}
	}

	private void TryWear()
	{
		if (this.m_TrywearMaid.IsBusy || this.m_SelectTrywear == null || !this.m_SelectTrywear.IsCategoryCostume)
		{
			return;
		}
		Facility.CostumeType typeCostume = CasinoDataMgr.Instance.CurrentCasino.typeCostume;
		MPN[] facilityCostumeEnableMPN = FacilityDataTable.GetFacilityCostumeEnableMPN();
		foreach (MPN mpn in facilityCostumeEnableMPN)
		{
			if (typeCostume != Facility.CostumeType.Heroine)
			{
				this.m_TrywearMaid.ResetProp(mpn, false);
			}
			if (this.m_TrywearItem == null)
			{
				if (!this.m_BackUpwear.ContainsKey(mpn))
				{
					MaidProp prop = this.m_TrywearMaid.GetProp(mpn);
					MaidProp maidProp = new MaidProp();
					maidProp.strFileName = prop.strFileName;
					maidProp.nFileNameRID = prop.nFileNameRID;
					this.m_BackUpwear.Add(mpn, maidProp);
				}
			}
		}
		this.m_TrywearItem = this.m_SelectTrywear;
		Action action = delegate()
		{
			this.m_TrywearItem.TryWear(this.m_TrywearMaid);
			this.m_TrywearMaid.AllProcPropSeqStart();
			this.m_TrycancelButton.isEnabled = true;
		};
		if (typeCostume == Facility.CostumeType.Heroine)
		{
			action();
		}
		else
		{
			this.m_TrywearMaid.AllProcPropSeqStart();
			base.StartCoroutine(KasaiUtility.CharaLoadWait(action));
		}
	}

	private void TryCancel()
	{
		if (this.m_TrywearMaid.IsBusy || this.m_TrywearItem == null || !this.m_TrywearItem.IsCategoryCostume)
		{
			return;
		}
		this.m_TrycancelButton.isEnabled = false;
		this.RecetTrywear(null);
		this.m_TrywearItem = null;
		this.m_BackUpwear.Clear();
	}

	private void CamRecet()
	{
		this.m_CamMoveSupport.ResetCamera();
	}

	private void ClickOKButton()
	{
		this.FadeOut();
	}

	protected override void FadeOutEnd()
	{
		if (this.m_TrywearItem != null)
		{
			this.RecetTrywear(new Action(base.FadeOutEnd));
		}
		else
		{
			base.FadeOutEnd();
		}
	}

	private void UpdateUIState()
	{
		ExChangeUI.Instance.SetButtonState(true);
		ExChangeUI.Instance.TextUIUpdate();
		ExChangeUI.Instance.UIStateUpdate();
		foreach (CasinoItemUI casinoItemUI in this.m_ItemUIList)
		{
			casinoItemUI.UIUpdate();
		}
	}

	public void ItemBuy(CasinoShopItem item_data)
	{
		Status status = GameMain.Instance.CharacterMgr.status;
		status.casinoCoin -= (long)item_data.Price;
		item_data.ItemBuy();
		this.UpdateUIState();
		GameMain.Instance.SysDlg.Show(item_data.Name + "を購入しました", SystemDialog.TYPE.OK, null, null);
	}

	public void SetTrywearItem(CasinoShopItem item_data)
	{
		this.m_SelectTrywear = item_data;
		this.m_TrywearButton.isEnabled = true;
		foreach (CasinoItemUI casinoItemUI in this.m_ItemUIList)
		{
			casinoItemUI.SwitchFrame();
		}
	}

	public void RecetTrywearItem()
	{
		this.m_SelectTrywear = null;
		this.m_TrywearButton.isEnabled = false;
		foreach (CasinoItemUI casinoItemUI in this.m_ItemUIList)
		{
			casinoItemUI.SwitchFrame();
		}
	}

	public void ShowItenInfo(CasinoShopItem item_data, Transform offset_base)
	{
		this.m_InfoData.UIobj.SetActive(true);
		this.m_InfoData.Icon.sprite2D = item_data.Icon;
		this.m_InfoData.InfoLabel.text = item_data.Document;
		this.m_InfoData.Namelabel.text = item_data.Name;
		int num = Mathf.FloorToInt(this.m_ScrollPanel.height / this.m_ShopItemUI.Grid.cellHeight + 0.5f);
		int num2 = CasinoDataMgr.Instance.GetShopItem(item_data.MyCategory).Count - num;
		int num3 = num + Mathf.FloorToInt((float)num2 * this.m_ShopItemUI.ScrollView.verticalScrollBar.value) - 1;
		for (int i = 0; i < CasinoDataMgr.Instance.GetShopItem(item_data.MyCategory).Count; i++)
		{
			CasinoShopItem casinoShopItem = CasinoDataMgr.Instance.GetShopItem(item_data.MyCategory).ElementAt(i);
			if (casinoShopItem == item_data)
			{
				if (i >= num3 - 1)
				{
					this.m_InfoData.UIobj.transform.position = offset_base.TransformPoint(-this.m_OffsetInfo);
				}
				else
				{
					this.m_InfoData.UIobj.transform.position = offset_base.TransformPoint(this.m_OffsetInfo);
				}
				break;
			}
		}
	}

	public void HideItemInfo()
	{
		this.m_InfoData.UIobj.SetActive(false);
	}

	private WfCameraMoveSupport m_CamMoveSupport;

	[SerializeField]
	[Header("メイドの頭とのオフセット値")]
	private Vector3 m_HeadCamOffset = Vector3.zero;

	private Maid m_TrywearMaid;

	[SerializeField]
	[Header("各ボタン")]
	private UIButton m_TrywearButton;

	[SerializeField]
	private UIButton m_TrycancelButton;

	[SerializeField]
	private UIButton m_CamRecetButton;

	[SerializeField]
	private UIButton m_OkButton;

	[SerializeField]
	[Header("カテゴリー列挙用UI")]
	private SceneCasinoShop.ScrollUIData m_CategoryUI;

	[SerializeField]
	private GameObject m_CategoryAllow;

	[SerializeField]
	private float m_AllowMove = 0.5f;

	private Vector3 m_AllowposStart = Vector3.zero;

	private Vector3 m_AllowposEnd = Vector3.zero;

	[SerializeField]
	[Header("アイテム列挙用UI")]
	private SceneCasinoShop.ScrollUIData m_ShopItemUI;

	[SerializeField]
	private UIScrollBar m_ItemSrollBar;

	[SerializeField]
	[Header("アイテム説明用UI")]
	private GameObject m_ItemInfoUI;

	[SerializeField]
	private Vector3 m_OffsetInfo = Vector3.zero;

	[SerializeField]
	private UICamera m_NguiCam;

	private SceneCasinoShop.InfoUIData m_InfoData = new SceneCasinoShop.InfoUIData();

	private CasinoShopItem m_TrywearItem;

	private CasinoShopItem m_SelectTrywear;

	private Dictionary<MPN, MaidProp> m_BackUpwear = new Dictionary<MPN, MaidProp>();

	private Dictionary<UIWFTabButton, CasinoShopItem.Category> m_ButtonCategoryPair = new Dictionary<UIWFTabButton, CasinoShopItem.Category>();

	private List<CasinoItemUI> m_ItemUIList = new List<CasinoItemUI>();

	private UIPanel m_ScrollPanel;

	private bool m_NguiEnable = true;

	[Serializable]
	private class ScrollUIData
	{
		public UIGrid Grid;

		public UIScrollView ScrollView;

		public UIWFTabPanel TabPanel;
	}

	private class InfoUIData
	{
		public GameObject UIobj;

		public UI2DSprite Icon;

		public UILabel Namelabel;

		public UILabel InfoLabel;
	}

	private class BoughtData
	{
		public CasinoShopItem ItemData;

		public UIButton Button;

		public UISprite ButtonIcon;
	}
}
