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

public class DanceBattle_Mgr : PartsMgrBase
{
	private float m_OneVSGaugeValue
	{
		get
		{
			return 1f / (float)this.m_GaugeList.Count;
		}
	}

	private float m_GaugeValue
	{
		get
		{
			return this.m_GaugeFillAmount;
		}
		set
		{
			this.m_GaugeFillAmount = Mathf.Clamp01(value);
			for (int i = 0; i < this.m_GaugeList.Count; i++)
			{
				this.m_GaugeList[i].fillAmount = (this.m_GaugeFillAmount - (float)i * this.m_OneVSGaugeValue) / this.m_OneVSGaugeValue;
				if (this.m_GaugeList[i].fillAmount > 0f && this.m_GaugeList[i].fillAmount <= 1f)
				{
					this.m_EffectUIFit.FitImage = this.m_GaugeList[i];
				}
				else if (this.m_GaugeFillAmount <= 0f)
				{
					this.m_EffectUIFit.FitImage = this.m_GaugeList[0];
				}
			}
		}
	}

	public static DanceEnemyParam EnemyData
	{
		get
		{
			return VsDanceDataMgr.Instance.GetCurrentEnemy(RhythmAction_Mgr.NowState == RhythmAction_Mgr.DanceState.Dance_Second);
		}
	}

	public static DanceBattle_Mgr Instance { get; private set; }

	protected override void Start()
	{
		GameMain.Instance.CharacterMgr.status.SetFlag("ダンス勝敗", 0);
		if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.VS_Skip)
		{
			GameMain.Instance.CharacterMgr.status.SetFlag("ダンス勝敗", 1);
		}
		base.Start();
		DanceBattle_Mgr.Instance = this;
		if (!base.IsActive)
		{
			base.gameObject.SetActive(false);
			return;
		}
		this.m_PlayerLineArray = this.m_PlayerLineGroup.GetComponentsInChildren<UISprite>();
		this.m_EnemyLineArray = this.m_EnemyLineGroup.GetComponentsInChildren<UISprite>();
		this.m_EffectUIFit = this.m_GaugeEffect.GetComponent<UI_ImageFitPos>();
		ParticleSystem[] componentsInChildren = this.m_GaugeEffect.GetComponentsInChildren<ParticleSystem>();
		foreach (ParticleSystem particle in componentsInChildren)
		{
			RhythmAction_Mgr.Instance.AddParticleSystem(particle);
		}
		IEnumerator enumerator = this.m_PlayerVSGauge.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				Transform transform = (Transform)obj;
				UISprite component = transform.GetComponent<UISprite>();
				if (component)
				{
					this.m_GaugeList.Add(component);
					component.fillAmount = 0f;
				}
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		this.m_GaugeSetSpace = Mathf.Abs((this.m_GaugeList[0].transform.position - this.m_GaugeList[1].transform.position).x);
		this.m_GaugeValue = 0.5f;
	}

	private IEnumerator CheckBattleSituation()
	{
		float appeal_timer = 0f;
		for (;;)
		{
			if (!RhythmAction_Mgr.Instance.IsPause)
			{
				if (Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Player) != 0L || Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Enemy) != 0L)
				{
					this.m_GaugeValue = this.GetScoreRate(DanceBattle_Mgr.CharaType.Player, false);
				}
				if (RhythmAction_Mgr.Instance.DanceTimer > this.m_CanReversalTime)
				{
					switch (this.m_SuperiorityChara)
					{
					case DanceBattle_Mgr.CharaType.Player:
						if (Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Player) < Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Enemy))
						{
							this.m_SuperiorityChara = DanceBattle_Mgr.CharaType.Enemy;
							Appeal_Mgr.Instance.ReversalCutIn(this.m_SuperiorityChara);
						}
						break;
					case DanceBattle_Mgr.CharaType.Enemy:
						if (Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Player) > Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Enemy))
						{
							this.m_SuperiorityChara = DanceBattle_Mgr.CharaType.Player;
							Appeal_Mgr.Instance.ReversalCutIn(this.m_SuperiorityChara);
						}
						break;
					case DanceBattle_Mgr.CharaType.None:
						if (Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Player) < Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Enemy))
						{
							this.m_SuperiorityChara = DanceBattle_Mgr.CharaType.Enemy;
						}
						else
						{
							this.m_SuperiorityChara = DanceBattle_Mgr.CharaType.Player;
						}
						break;
					}
					if (RhythmAction_Mgr.NowDance != RhythmAction_Mgr.DanceType.BenchMark && Appeal_Mgr.Instance.GetAppealData(DanceBattle_Mgr.CharaType.Enemy).IsCanApplel && !Note_Mgr.Instance.IsNoteEnd(DanceBattle_Mgr.CharaType.Enemy))
					{
						appeal_timer += RhythmAction_Mgr.Instance.DanceDeltaTime;
						if (appeal_timer >= this.m_AppealCheckTime)
						{
							if (UnityEngine.Random.Range(0, 100) < DanceBattle_Mgr.EnemyData.ApeealRate)
							{
								Appeal_Mgr.Instance.AppealCutIn(DanceBattle_Mgr.CharaType.Enemy);
							}
							appeal_timer = 0f;
						}
					}
				}
			}
			yield return null;
		}
		yield break;
	}

	private IEnumerator GaugeLineAnime()
	{
		float timer = 0f;
		Vector3 gauge_first = this.m_GaugeList.First<UISprite>().transform.position;
		Vector3 gauge_last = this.m_GaugeList.Last<UISprite>().transform.position;
		Vector3 start_pos = Vector3.zero;
		Vector3 end_pos = Vector3.zero;
		Action<UISprite[], Color> line_setting = delegate(UISprite[] line_array, Color line_col)
		{
			float num = (float)line_array.Count<UISprite>() * this.m_LineRate;
			for (int i = 0; i < line_array.Count<UISprite>(); i++)
			{
				float num2 = Mathf.Max(timer - this.m_LineRate * (float)i, 0f);
				float num3 = num2 - (float)Mathf.FloorToInt(num2 / num) * num;
				float num4 = Mathf.Clamp01(num3 / num);
				if (num4 > 0f && !line_array[i].gameObject.activeSelf)
				{
					line_array[i].gameObject.SetActive(true);
				}
				Color color = line_col;
				color.a = 1f - num4;
				line_array[i].color = color;
				line_array[i].transform.position = Vector3.Lerp(start_pos, end_pos, num4);
				line_array[i].transform.localScale = Vector3.one * this.m_LineMaxSize * (1f - num4);
			}
		};
		for (;;)
		{
			if (!RhythmAction_Mgr.Instance.IsPause)
			{
				this.m_PlayerGlitterEffect.SizeRateX = this.GetScoreRate(DanceBattle_Mgr.CharaType.Player, false);
				this.m_EnemyGlitterEffect.SizeRateX = this.GetScoreRate(DanceBattle_Mgr.CharaType.Enemy, false);
				timer += RhythmAction_Mgr.Instance.DanceDeltaTime;
				start_pos = this.m_PlayerLineGroup.position;
				float d = Mathf.Min(this.m_GaugeSetSpace * (float)(this.m_PlayerLineArray.Count<UISprite>() - 1), start_pos.x - gauge_first.x);
				end_pos = start_pos + Vector3.left * d;
				line_setting(this.m_PlayerLineArray, Color.green);
				start_pos = this.m_EnemyLineGroup.position;
				d = Mathf.Min(this.m_GaugeSetSpace * (float)(this.m_EnemyLineArray.Count<UISprite>() - 1), gauge_last.x - start_pos.x);
				end_pos = start_pos + Vector3.right * d;
				line_setting(this.m_EnemyLineArray, Color.magenta);
			}
			yield return null;
		}
		yield break;
	}

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

	public override void EndAction()
	{
		RhythmAction_Mgr.DanceState nowState = RhythmAction_Mgr.NowState;
		if (nowState != RhythmAction_Mgr.DanceState.Dance_First)
		{
			if (nowState == RhythmAction_Mgr.DanceState.Dance_Second)
			{
				Result_Display.SetResultData("BattleCount", "2nd", Result_Display.ResultType.Image);
				if (this.IsWin() && RhythmAction_Mgr.Instance.IsTakeEnd)
				{
					Result_Display.SetResultData("BattleResult", "WIN", Result_Display.ResultType.Image);
					GameMain.Instance.CharacterMgr.status.SetFlag("ダンス勝敗", 1);
				}
				else
				{
					Result_Display.SetResultData("BattleResult", "LOSE", Result_Display.ResultType.Image);
					GameMain.Instance.CharacterMgr.status.SetFlag("ダンス勝敗", 0);
				}
			}
		}
		else
		{
			Result_Display.SetResultData("BattleCount", "1st", Result_Display.ResultType.Image);
			if (this.IsWin() && RhythmAction_Mgr.Instance.IsTakeEnd)
			{
				Result_Display.SetResultData("BattleResult", "WIN", Result_Display.ResultType.Image);
				GameMain.Instance.CharacterMgr.status.SetFlag("ダンス勝敗", 1);
			}
			else
			{
				Result_Display.SetResultData("BattleResult", "LOSE", Result_Display.ResultType.Image);
				GameMain.Instance.CharacterMgr.status.SetFlag("ダンス勝敗", 0);
			}
		}
	}

	public float GetScoreRate(DanceBattle_Mgr.CharaType chara_type, bool is_percentage = false)
	{
		float num = (float)Score_Mgr.Instance.GetScore(chara_type) / (float)(Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Player) + Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Enemy));
		if (Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Player) + Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Enemy) == 0L)
		{
			num = 0.5f;
		}
		if (is_percentage)
		{
			num *= 100f;
		}
		return num;
	}

	public bool IsWin()
	{
		return base.IsActive && Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Player) >= Score_Mgr.Instance.GetScore(DanceBattle_Mgr.CharaType.Enemy);
	}

	public bool IsEven()
	{
		return base.IsActive && this.GetScoreRate(DanceBattle_Mgr.CharaType.Player, true) >= this.m_MinAntagonism && this.GetScoreRate(DanceBattle_Mgr.CharaType.Player, true) <= this.m_MaxAntagonism;
	}

	public static DanceBattle_Mgr.CharaType GetOpponentType(DanceBattle_Mgr.CharaType chara_type)
	{
		if (chara_type == DanceBattle_Mgr.CharaType.Player)
		{
			return DanceBattle_Mgr.CharaType.Enemy;
		}
		return DanceBattle_Mgr.CharaType.Player;
	}

	public static bool IsAllBattleWin()
	{
		if (RhythmAction_Mgr.NowDance != RhythmAction_Mgr.DanceType.VS)
		{
			return false;
		}
		Status status = GameMain.Instance.CharacterMgr.status;
		return status.GetFlag("ダンス勝敗") == 1 && status.GetFlag("バトル回数") == 2;
	}

	[SerializeField]
	private float m_MinAntagonism = 40f;

	[SerializeField]
	private float m_MaxAntagonism = 60f;

	[SerializeField]
	[Header("アピールを行うかの抽選を行う時間")]
	private float m_AppealCheckTime = 5f;

	[SerializeField]
	[Header("逆転カットイン可能になる時間")]
	private float m_CanReversalTime = 15f;

	[SerializeField]
	[Header("プレイヤーのVSゲージの親")]
	private Transform m_PlayerVSGauge;

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

	private float m_GaugeFillAmount;

	[SerializeField]
	private GameObject m_GaugeEffect;

	[SerializeField]
	[Header("ライン発生間隔")]
	private float m_LineRate = 0.3f;

	[SerializeField]
	private float m_LineMaxSize = 1.25f;

	[SerializeField]
	private Transform m_PlayerLineGroup;

	[SerializeField]
	private Transform m_EnemyLineGroup;

	[Header("ゲージ上に粒子がかかるようなエフェクト")]
	[SerializeField]
	private UI_ParticleImageFit m_PlayerGlitterEffect;

	[SerializeField]
	private UI_ParticleImageFit m_EnemyGlitterEffect;

	private UISprite[] m_PlayerLineArray;

	private UISprite[] m_EnemyLineArray;

	private UI_ImageFitPos m_EffectUIFit;

	private DanceBattle_Mgr.CharaType m_SuperiorityChara = DanceBattle_Mgr.CharaType.None;

	private float m_GaugeSetSpace;

	private bool m_readEnd;

	public enum CharaType
	{
		Player,
		Enemy,
		None
	}
}
