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

public class Voltage_Mgr : PartsMgrBase
{
	public int VoltageStage
	{
		get
		{
			return this.m_VoltageFacter / 200 + 1;
		}
		private set
		{
			this.m_SaveVoltage = Mathf.Clamp((value - 1) * 200 + 100, 1, 1000);
		}
	}

	public static Voltage_Mgr Instance
	{
		get
		{
			return Voltage_Mgr.m_Instance;
		}
	}

	protected override void Start()
	{
		Voltage_Mgr.m_Instance = this;
		base.Start();
		if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.Free)
		{
			this.VoltageStage = DanceSetting.Settings.Voltage;
		}
		else
		{
			this.VoltageStage = 3;
		}
		this.m_VoltageFacter = this.m_SaveVoltage;
		foreach (ParticleSystem particleSystem in this.m_VoltageUpEffect)
		{
			particleSystem.gameObject.SetActive(false);
		}
		foreach (ParticleSystem particleSystem2 in this.m_VoltageDownEffect)
		{
			particleSystem2.gameObject.SetActive(false);
		}
		if (!base.IsActive)
		{
			base.gameObject.SetActive(false);
			return;
		}
		this.m_GaugeValue = this.m_VoltageFacter;
		this.m_GaugeMaxMark.SetActive(this.m_VoltageFacter == 1000);
		for (int k = 0; k < this.m_GaugeGroup.childCount; k++)
		{
			Transform child = this.m_GaugeGroup.GetChild(k);
			UISprite component = child.GetComponent<UISprite>();
			this.m_GaugeList.Add(component);
			component.fillAmount = (float)(this.m_VoltageFacter - k * 200) / 200f;
		}
		IEnumerator enumerator = this.m_FlashGauge.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				Transform transform = (Transform)obj;
				this.m_FlashGaugeList.Add(transform.GetComponent<UISprite>());
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		RhythmAction_Mgr.Instance.AddParticleSystem(this.m_VoltageUpEffect);
		RhythmAction_Mgr.Instance.AddParticleSystem(this.m_VoltageDownEffect);
	}

	private IEnumerator GaugeAnimation()
	{
		float timer = 0f;
		int first_voltage = this.m_GaugeValue;
		int voltage_diff = this.m_SaveVoltage - this.m_GaugeValue;
		for (;;)
		{
			timer += Time.deltaTime;
			this.m_GaugeValue = (int)((float)first_voltage + (float)voltage_diff * Mathf.Sin(timer / 0.75f * 90f * 0.0174532924f));
			for (int i = 0; i < this.m_GaugeList.Count; i++)
			{
				this.m_GaugeList[i].fillAmount = (float)(this.m_GaugeValue - i * 200) / 200f;
			}
			if (timer >= 0.75f)
			{
				break;
			}
			yield return null;
		}
		this.m_GaugeValue = this.m_VoltageFacter;
		this.m_GaugeMaxMark.SetActive(this.m_VoltageFacter == 1000);
		yield break;
		yield break;
	}

	private IEnumerator GaugeFlash()
	{
		float timer = 0f;
		for (;;)
		{
			if (this.m_VoltageFacter < 200)
			{
				timer = 0f;
				this.m_FlashGauge.gameObject.SetActive(false);
			}
			else
			{
				if (!RhythmAction_Mgr.Instance.IsPause)
				{
					timer += RhythmAction_Mgr.Instance.DanceDeltaTime;
				}
				this.m_FlashGauge.gameObject.SetActive(true);
				Color white = Color.white;
				white.a = Mathf.Sin(timer / this.m_FlashTime * 90f * 0.0174532924f);
				for (int i = 0; i < this.m_FlashGaugeList.Count; i++)
				{
					if (200 * (i + 1) <= this.m_GaugeValue)
					{
						this.m_FlashGaugeList[i].gameObject.SetActive(true);
						this.m_FlashGaugeList[i].color = white;
					}
					else
					{
						this.m_FlashGaugeList[i].gameObject.SetActive(false);
					}
				}
				if (this.m_GaugeValue == 1000)
				{
					this.m_FlashGaugeMax.gameObject.SetActive(true);
					this.m_FlashGaugeMax.color = white;
				}
				else
				{
					this.m_FlashGaugeMax.gameObject.SetActive(false);
				}
			}
			yield return null;
		}
		yield break;
	}

	public void DecideVoltageValue()
	{
		int num = this.m_VoltageFacter / 200 + 1;
		this.m_VoltageFacter = this.m_SaveVoltage;
		if (num <= 3 && this.VoltageStage >= 4)
		{
			foreach (ParticleSystem particleSystem in this.m_VoltageUpEffect)
			{
				particleSystem.gameObject.SetActive(true);
				particleSystem.Play();
			}
			GameMain.Instance.SoundMgr.PlaySe(this.m_VoltageUpSe, false);
		}
		else if (num >= 4 && this.VoltageStage <= 3)
		{
			foreach (ParticleSystem particleSystem2 in this.m_VoltageDownEffect)
			{
				particleSystem2.gameObject.SetActive(true);
				particleSystem2.Play();
			}
			GameMain.Instance.SoundMgr.PlaySe(this.m_VoltageDownSe, false);
		}
		base.StartCoroutine(this.GaugeAnimation());
	}

	public void DownVoltage(Dance_Note.Evaluation evalue)
	{
		if (this.m_SaveVoltage == 1)
		{
			return;
		}
		foreach (Voltage_Mgr.DownData downData in this.m_VoltageDown_Data)
		{
			if (downData.Evalue == evalue)
			{
				this.m_SaveVoltage -= downData.DownValue;
				break;
			}
		}
		if (this.m_SaveVoltage < 1)
		{
			this.m_SaveVoltage = 1;
		}
	}

	public void AddVoltage()
	{
		if (this.m_SaveVoltage == 1000)
		{
			return;
		}
		this.m_SaveVoltage += this.m_AddValue;
		if (this.m_SaveVoltage > 1000)
		{
			this.m_SaveVoltage = 1000;
		}
	}

	public override void StartAction()
	{
		base.StartCoroutine(this.GaugeFlash());
	}

	private const float m_AnimeTime = 0.75f;

	public const int VoltageStageDefo = 3;

	public const int VoltageStageMin = 1;

	private const int m_MinFacter = 1;

	private const int m_MaxFacter = 1000;

	private const int m_VoltageUp = 200;

	private int m_VoltageFacter;

	private int m_GaugeValue;

	private int m_SaveVoltage;

	[SerializeField]
	private Transform m_GaugeGroup;

	[SerializeField]
	private GameObject m_GaugeMaxMark;

	[SerializeField]
	private float m_FlashTime;

	[SerializeField]
	private Transform m_FlashGauge;

	[SerializeField]
	private UISprite m_FlashGaugeMax;

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

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

	[SerializeField]
	[Header("アピール時のボルテージ増加量")]
	private int m_AddValue = 50;

	[SerializeField]
	[Header("各評価ごとのボルテージの減少値")]
	private Voltage_Mgr.DownData[] m_VoltageDown_Data = new Voltage_Mgr.DownData[]
	{
		new Voltage_Mgr.DownData(Dance_Note.Evaluation.MISS, 30),
		new Voltage_Mgr.DownData(Dance_Note.Evaluation.BAD, 20),
		new Voltage_Mgr.DownData(Dance_Note.Evaluation.GOOD, 10)
	};

	[SerializeField]
	[Header("ボルテージがUpしたときの各エフェクト")]
	private ParticleSystem[] m_VoltageUpEffect;

	[SerializeField]
	private string m_VoltageUpSe = "SE008.ogg";

	[SerializeField]
	[Header("ボルテージがDownしたときの各エフェクト")]
	private ParticleSystem[] m_VoltageDownEffect;

	[SerializeField]
	private string m_VoltageDownSe = "SE003.ogg";

	private static Voltage_Mgr m_Instance;

	[Serializable]
	private class DownData
	{
		public DownData(Dance_Note.Evaluation evalue, int value)
		{
			this.Evalue = evalue;
			this.DownValue = value;
		}

		public Dance_Note.Evaluation Evalue;

		public int DownValue;
	}
}
