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

public class Product_Mgr : PartsMgrBase
{
	protected override void Start()
	{
		base.Start();
		switch (RhythmAction_Mgr.NowDance)
		{
		case RhythmAction_Mgr.DanceType.Free:
		case RhythmAction_Mgr.DanceType.View:
			this.m_nAudience = DanceSetting.Settings.Audience;
			this.m_nProduct = DanceSetting.Settings.Product;
			break;
		case RhythmAction_Mgr.DanceType.Challenge:
			this.m_nAudience = Product_Mgr.AudienceState.Few;
			this.m_nProduct = Product_Mgr.ProductRank.Normal;
			break;
		case RhythmAction_Mgr.DanceType.VS:
			this.m_nAudience = Product_Mgr.AudienceState.Normal;
			this.m_nProduct = Product_Mgr.ProductRank.Normal;
			break;
		case RhythmAction_Mgr.DanceType.Encore:
			this.m_nAudience = Product_Mgr.AudienceState.Many;
			this.m_nProduct = Product_Mgr.ProductRank.Special;
			break;
		case RhythmAction_Mgr.DanceType.BenchMark:
			this.m_nAudience = Product_Mgr.AudienceState.Normal;
			this.m_nProduct = Product_Mgr.ProductRank.Special;
			break;
		}
		if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.Encore)
		{
			this.m_nAudience = Product_Mgr.AudienceState.Many;
			this.m_nProduct = Product_Mgr.ProductRank.Special;
		}
		int voltage = this.m_AllAudience.Single((Product_Mgr.Audience e) => e.State == this.m_nAudience).Voltage;
		foreach (Product_Mgr.Audience audience in this.m_AllAudience)
		{
			bool active = audience.Voltage <= voltage;
			foreach (GameObject gameObject in audience.AudienceObj)
			{
				gameObject.SetActive(active);
			}
		}
		foreach (GameObject gameObject2 in this.m_SpecialProduct)
		{
			this.CheckSpecialObj(gameObject2.transform);
		}
		if (this.m_SpecialEffect)
		{
			this.m_SpecialEffect.gameObject.SetActive(this.m_nProduct == Product_Mgr.ProductRank.Special);
			if (this.m_SpecialEffect.gameObject.activeSelf || RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.VS)
			{
				IEnumerator enumerator3 = this.m_SpecialEffect.GetEnumerator();
				try
				{
					while (enumerator3.MoveNext())
					{
						object obj = enumerator3.Current;
						Transform transform = (Transform)obj;
						ParticleSystem component = transform.GetComponent<ParticleSystem>();
						ParticleSystem particleSystem = null;
						if (transform.childCount > 0)
						{
							particleSystem = transform.GetChild(0).GetComponent<ParticleSystem>();
						}
						if (component)
						{
							RhythmAction_Mgr.Instance.AddParticleSystem(component);
						}
						if (particleSystem)
						{
							RhythmAction_Mgr.Instance.AddParticleSystem(particleSystem);
						}
					}
				}
				finally
				{
					IDisposable disposable;
					if ((disposable = (enumerator3 as IDisposable)) != null)
					{
						disposable.Dispose();
					}
				}
			}
		}
		if (!base.IsActive)
		{
			base.gameObject.SetActive(false);
			return;
		}
		this.ReadTimeData();
	}

	private void CheckSpecialObj(Transform parent)
	{
		if (parent.childCount > 0)
		{
			IEnumerator enumerator = parent.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object obj = enumerator.Current;
					Transform parent2 = (Transform)obj;
					this.CheckSpecialObj(parent2);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
		else
		{
			Animator component = parent.GetComponent<Animator>();
			MeshRenderer component2 = parent.GetComponent<MeshRenderer>();
			bool flag = this.m_nProduct == Product_Mgr.ProductRank.Special || this.m_nProduct == Product_Mgr.ProductRank.Normal;
			bool flag2 = RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.VS;
			if (!component || !flag2)
			{
				if (!this.m_ProductSpecialObj.Contains(parent.gameObject) && flag2)
				{
					this.m_ProductSpecialObj.Add(parent.gameObject);
				}
				parent.gameObject.SetActive(flag);
			}
			else if (component2 && !this.m_ProductSpecialRender.Contains(component2))
			{
				this.m_ProductSpecialRender.Add(component2);
				component2.enabled = flag;
			}
		}
	}

	private void ReadTimeData()
	{
		string text = RhythmAction_Mgr.Instance.MusicCSV_Path + "time_data.nei";
		if (!GameUty.FileSystem.IsExistentFile(text))
		{
			NDebug.Assert("表がありません。" + text, false);
		}
		using (AFileBase afileBase = GameUty.FileSystem.FileOpen(text))
		{
			using (CsvParser csvParser = new CsvParser())
			{
				bool condition = csvParser.Open(afileBase);
				NDebug.Assert(condition, text + "\nopen failed.");
				for (int i = 1; i < csvParser.max_cell_y; i++)
				{
					if (csvParser.IsCellToExistData(0, i))
					{
						this.m_All_BranchPoint.Add(csvParser.GetCellAsReal(1, i));
					}
				}
			}
		}
	}

	private Product_Mgr.AudienceState GetNowAudience()
	{
		Product_Mgr.AudienceState result = Product_Mgr.AudienceState.Few;
		foreach (Product_Mgr.Audience audience in this.m_AllAudience)
		{
			if (audience.Voltage > Voltage_Mgr.Instance.VoltageStage)
			{
				break;
			}
			result = audience.State;
		}
		return result;
	}

	private IEnumerator ProductCheck()
	{
		float timer = 0f;
		for (;;)
		{
			if (!RhythmAction_Mgr.Instance.IsPause)
			{
				timer += RhythmAction_Mgr.Instance.DanceDeltaTime;
				this.m_ProgressGauge.transform.localPosition = Vector3.right * timer / RhythmAction_Mgr.Instance.TakeTime * (float)this.m_ProgressGauge.width;
				if (timer / 60f < 10f)
				{
					this.m_MinuteTimeText.text = "0" + Mathf.FloorToInt(timer / 60f).ToString();
				}
				else
				{
					this.m_MinuteTimeText.text = Mathf.FloorToInt(timer / 60f).ToString();
				}
				if ((int)timer % 60 < 10)
				{
					this.m_SecondsTimeText.text = "0" + ((int)timer % 60).ToString();
				}
				else
				{
					this.m_SecondsTimeText.text = ((int)timer % 60).ToString();
				}
				if (this.m_All_BranchPoint.Count > 0 && timer >= this.m_All_BranchPoint.First<float>())
				{
					base.StartCoroutine(this.GaugeFlash());
					Voltage_Mgr.Instance.DecideVoltageValue();
					if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.VS || RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.BenchMark)
					{
						Product_Mgr.AudienceState nAudience = this.m_nAudience;
						this.m_nAudience = this.GetNowAudience();
						if (nAudience != this.m_nAudience)
						{
							foreach (Product_Mgr.Audience audience in this.m_AllAudience)
							{
								bool active = audience.Voltage <= Voltage_Mgr.Instance.VoltageStage;
								foreach (GameObject gameObject in audience.AudienceObj)
								{
									gameObject.SetActive(active);
								}
							}
						}
						if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.VS)
						{
							Product_Mgr.ProductRank nProduct = this.m_nProduct;
							if (DanceBattle_Mgr.Instance.IsEven())
							{
								this.m_nProduct = Product_Mgr.ProductRank.Normal;
							}
							else if (DanceBattle_Mgr.Instance.IsWin())
							{
								this.m_nProduct = Product_Mgr.ProductRank.Special;
							}
							else
							{
								this.m_nProduct = Product_Mgr.ProductRank.Low;
							}
							if (nProduct != this.m_nProduct)
							{
								bool flag = this.m_nProduct == Product_Mgr.ProductRank.Special || this.m_nProduct == Product_Mgr.ProductRank.Normal;
								foreach (MeshRenderer meshRenderer in this.m_ProductSpecialRender)
								{
									meshRenderer.enabled = flag;
								}
								foreach (GameObject gameObject2 in this.m_ProductSpecialObj)
								{
									gameObject2.SetActive(flag);
								}
								if (this.m_SpecialEffect)
								{
									this.m_SpecialEffect.gameObject.SetActive(this.m_nProduct == Product_Mgr.ProductRank.Special);
								}
							}
						}
					}
					this.m_All_BranchPoint.RemoveAt(0);
				}
			}
			yield return null;
		}
		yield break;
	}

	private IEnumerator GaugeFlash()
	{
		this.m_FlashGauge.gameObject.SetActive(true);
		Color col = this.m_FlashGauge.color;
		float timer = 0f;
		for (;;)
		{
			if (!RhythmAction_Mgr.Instance.IsPause)
			{
				timer += RhythmAction_Mgr.Instance.DanceDeltaTime;
				float alpha = this.m_MaxFlashAlpha * Mathf.Sin(Mathf.Clamp01(timer / this.m_MaxFlashAlpha) * 180f * 0.0174532924f);
				this.m_FlashGauge.alpha = alpha;
				if (timer > this.m_MaxFlashAlpha)
				{
					break;
				}
			}
			yield return null;
		}
		this.m_FlashGauge.gameObject.SetActive(false);
		yield break;
		yield break;
	}

	public override void StartAction()
	{
		foreach (float num in this.m_All_BranchPoint)
		{
			GameObject gameObject = Utility.CreatePrefab(this.m_MarkParent, "SceneDance/Rhythm_Action/Prefab/" + this.m_ProgressMarkName, true);
			float d = Mathf.Clamp01(num / RhythmAction_Mgr.Instance.TakeTime) * (float)this.m_ProgressGauge.width;
			gameObject.transform.localPosition = Vector3.right * d;
			UISprite component = gameObject.GetComponent<UISprite>();
			this.m_ProgressMarkList.Add(component);
		}
		base.StartCoroutine(this.ProductCheck());
	}

	private Product_Mgr.AudienceState m_nAudience;

	[SerializeField]
	[Header("通過マークオブジェクト名")]
	private string m_ProgressMarkName = "ProgressMark";

	private List<UISprite> m_ProgressMarkList = new List<UISprite>();

	[SerializeField]
	[Header("通過マークの親")]
	private GameObject m_MarkParent;

	[SerializeField]
	[Header("プログレスゲージ")]
	private UISprite m_ProgressGauge;

	[SerializeField]
	[Header("経過時間UI(1分以上)")]
	private UILabel m_MinuteTimeText;

	[SerializeField]
	[Header("経過時間UI(1分以下)")]
	private UILabel m_SecondsTimeText;

	[SerializeField]
	[Header("通常・上位演出用のオブジェクトグループ")]
	private List<GameObject> m_SpecialProduct = new List<GameObject>();

	[SerializeField]
	private Product_Mgr.Audience[] m_AllAudience = new Product_Mgr.Audience[]
	{
		new Product_Mgr.Audience(Product_Mgr.AudienceState.Few, 1),
		new Product_Mgr.Audience(Product_Mgr.AudienceState.Normal, 3),
		new Product_Mgr.Audience(Product_Mgr.AudienceState.Many, 5)
	};

	[SerializeField]
	[Header("上位演出エフェクト")]
	private Transform m_SpecialEffect;

	[SerializeField]
	[Header("ゲージ光らせる時間")]
	private float m_GaugeFlashTime;

	[SerializeField]
	[Range(0f, 1f)]
	private float m_MaxFlashAlpha = 1f;

	[SerializeField]
	private UISprite m_FlashGauge;

	private Product_Mgr.ProductRank m_nProduct;

	private List<float> m_All_BranchPoint = new List<float>();

	private List<MeshRenderer> m_ProductSpecialRender = new List<MeshRenderer>();

	private List<GameObject> m_ProductSpecialObj = new List<GameObject>();

	public enum ProductRank
	{
		Low,
		Special,
		Normal
	}

	public enum AudienceState
	{
		Few,
		Normal,
		Many
	}

	[Serializable]
	private class Audience
	{
		public Audience(Product_Mgr.AudienceState state, int voltage)
		{
			this.State = state;
			this.Voltage = voltage;
		}

		[Header("表示する時の客数状態")]
		public Product_Mgr.AudienceState State;

		[Header("このボルテージ以上の時表示")]
		public int Voltage;

		[Header("客オブジェクト")]
		public List<GameObject> AudienceObj = new List<GameObject>();
	}
}
