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

public class Score_Mgr : PartsMgrBase
{
	public bool IsExistMiss
	{
		get
		{
			return this.m_IsExistMiss;
		}
	}

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

	protected override void Start()
	{
		Score_Mgr.m_Instance = this;
		base.Start();
		if (!base.IsActive)
		{
			base.gameObject.SetActive(false);
			return;
		}
		this.m_FlashCombo.gameObject.SetActive(false);
		this.m_FlashCombo_Child.gameObject.SetActive(false);
		this.m_CharaScoreList.Clear();
		this.m_EnemyScoreUI.SetActive(false);
		this.m_CharaScoreList.Add(DanceBattle_Mgr.CharaType.Player, new Score_Mgr.CharaScoreData());
		this.SetTextandIcon(this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player], this.m_PlayerScoreUI);
		if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.VS || RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.BenchMark)
		{
			this.m_EnemyScoreUI.SetActive(true);
			this.m_CharaScoreList.Add(DanceBattle_Mgr.CharaType.Enemy, new Score_Mgr.CharaScoreData());
			this.SetTextandIcon(this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Enemy], this.m_EnemyScoreUI);
		}
		else if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.Challenge)
		{
			this.CSVRead();
		}
	}

	private void CSVRead()
	{
		string text = RhythmAction_Mgr.Instance.MusicCSV_Path + "reward_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))
					{
						Score_Mgr.RewardData rewardData = new Score_Mgr.RewardData();
						for (int j = 0; j < csvParser.max_cell_x; j++)
						{
							this.SetRewardData(csvParser, j, i, rewardData);
						}
						this.m_AllRewardData.Add(rewardData);
					}
				}
			}
		}
	}

	private void SetRewardData(CsvParser csv, int cell_X, int cell_Y, Score_Mgr.RewardData data)
	{
		switch (cell_X)
		{
		case 0:
			data.Rank = csv.GetCellAsString(cell_X, cell_Y);
			break;
		case 1:
			data.Condition = csv.GetCellAsString(cell_X, cell_Y);
			break;
		case 2:
			data.TargetScore = csv.GetCellAsInteger(cell_X, cell_Y);
			break;
		case 3:
			data.Reward = csv.GetCellAsInteger(cell_X, cell_Y);
			break;
		}
	}

	private void SetTextandIcon(Score_Mgr.CharaScoreData data, GameObject score_ui)
	{
		Transform transform = score_ui.transform.Find("Text/ScoreText");
		Transform transform2 = score_ui.transform.Find("IconMask/Icon");
		if (transform)
		{
			data.ScoreText = transform.GetComponent<UILabel>();
		}
		if (transform2)
		{
			data.MaidIcon = transform2.GetComponent<UI2DSprite>();
		}
	}

	private void SetNameUI(DanceBattle_Mgr.CharaType chara_type, GameObject score_ui)
	{
		if (chara_type == DanceBattle_Mgr.CharaType.Player)
		{
			this.m_CharaScoreList[chara_type].CharaName = RhythmAction_Mgr.Instance.UserMaid.status.fullNameJpStyle;
		}
		else
		{
			this.m_CharaScoreList[chara_type].CharaName = DanceBattle_Mgr.EnemyData.Name;
		}
		if (score_ui.transform.Find("Text/NameText"))
		{
			score_ui.transform.Find("Text/NameText").GetComponent<UILabel>().text = this.m_CharaScoreList[chara_type].CharaName;
		}
	}

	private void Update()
	{
		foreach (KeyValuePair<DanceBattle_Mgr.CharaType, Score_Mgr.CharaScoreData> keyValuePair in this.m_CharaScoreList)
		{
			keyValuePair.Value.ScoreText.text = keyValuePair.Value.nScore.ToString();
			if (keyValuePair.Key == DanceBattle_Mgr.CharaType.Player)
			{
				this.m_ComboText.gameObject.SetActive(keyValuePair.Value.nCombo != 0);
				this.m_ComboText.text = keyValuePair.Value.nCombo.ToString();
				this.m_FlashCombo.text = this.m_ComboText.text;
				this.m_ComboText.color = this.GetTextColor(keyValuePair.Value.nCombo);
				this.m_ComboText_Child.color = this.GetTextColor(keyValuePair.Value.nCombo);
			}
		}
	}

	private IEnumerator TextScaling()
	{
		float timer = 0f;
		for (;;)
		{
			if (!RhythmAction_Mgr.Instance.IsPause)
			{
				timer += RhythmAction_Mgr.Instance.DanceDeltaTime;
			}
			if (!this.m_FlashCombo.gameObject.activeSelf)
			{
				this.m_FlashCombo.gameObject.SetActive(true);
				this.m_FlashCombo_Child.gameObject.SetActive(true);
			}
			Color flash_col = this.m_FlashCombo.color;
			float rate = Mathf.Sin(Mathf.Clamp01(timer / this.m_ScalingTime) * 180f * 0.0174532924f);
			flash_col.a = rate;
			this.m_FlashCombo.color = flash_col;
			this.m_FlashCombo_Child.color = flash_col;
			this.m_ComboText.transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * this.m_ScalingValue, rate);
			if (timer > this.m_ScalingTime)
			{
				break;
			}
			yield return null;
		}
		this.m_FlashCombo.gameObject.SetActive(false);
		this.m_FlashCombo_Child.gameObject.SetActive(false);
		yield break;
		yield break;
	}

	private void ScoreScaling(float time)
	{
		float t = Mathf.Sin(Mathf.Clamp01(time / this.m_ScoreScalingTime) * 180f * 0.0174532924f);
		this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].ScoreText.transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * this.m_ScoreScaling, t);
	}

	private int BaceScore(Dance_Note.Evaluation evalue)
	{
		if (this.m_EvalueSetList.Any((Score_Mgr.EvalueSetting e) => e.Evalue == evalue))
		{
			return this.m_EvalueSetList.SingleOrDefault((Score_Mgr.EvalueSetting e) => e.Evalue == evalue).BaceScore;
		}
		return 0;
	}

	private int JudgeNumber(Dance_Note.Evaluation evalue)
	{
		if (this.m_EvalueSetList.Any((Score_Mgr.EvalueSetting e) => e.Evalue == evalue))
		{
			return this.m_EvalueSetList.SingleOrDefault((Score_Mgr.EvalueSetting e) => e.Evalue == evalue).JudgeNo;
		}
		return 0;
	}

	private int Combo_Correct(int combo)
	{
		int result = 0;
		if (combo >= 50)
		{
			result = Mathf.Min(combo / 50 * 50 + 50, this.m_Max_Correct);
		}
		else if (combo >= 10)
		{
			result = 50;
		}
		return result;
	}

	private Color GetTextColor(int combo)
	{
		Color result = Color.white;
		foreach (Score_Mgr.ComboTextColor comboTextColor2 in this.m_ComboTextColor)
		{
			if (combo < comboTextColor2.Combo)
			{
				break;
			}
			result = comboTextColor2.TextColor;
		}
		return result;
	}

	private float Score_Rate()
	{
		int num = this.m_EvalueCombo[Dance_Note.Evaluation.PERFECT] + this.m_EvalueCombo[Dance_Note.Evaluation.GREAT];
		if (num > 0)
		{
			return (float)num / (float)this.m_NoteCount * 100f;
		}
		return 0f;
	}

	private bool GoalToTarget(Score_Mgr.RewardData data)
	{
		bool result = false;
		if (data.Condition == ">")
		{
			result = (this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore > (long)data.TargetScore);
		}
		else if (data.Condition == ">=")
		{
			result = (this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore >= (long)data.TargetScore);
		}
		else if (data.Condition == "<")
		{
			result = (this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore < (long)data.TargetScore);
		}
		else if (data.Condition == "<=")
		{
			result = (this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore <= (long)data.TargetScore);
		}
		return result;
	}

	private int GetVoltageCorrect(int voltage, Dance_Note.Evaluation evalue)
	{
		if (this.m_VoltageCorrect.Any((Score_Mgr.VoltageCorrect e) => e.Voltage == voltage) && evalue != Dance_Note.Evaluation.MISS)
		{
			return this.m_VoltageCorrect.SingleOrDefault((Score_Mgr.VoltageCorrect e) => e.Voltage == voltage).Correct;
		}
		return 0;
	}

	private void DecideReward()
	{
		long num = 0L;
		long num2 = 0L;
		foreach (Score_Mgr.RewardData rewardData in this.m_AllRewardData)
		{
			num2 = num - this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore;
			Result_Display.SetResultData("Rank", rewardData.Rank, Result_Display.ResultType.Image);
			Result_Display.SetResultData("Money", Utility.ConvertMoneyText(rewardData.Reward), Result_Display.ResultType.String);
			if ((long)rewardData.TargetScore <= this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore)
			{
				RhythmAction_Mgr.Instance.SetDanceReward(rewardData.Reward);
				GameMain.Instance.CharacterMgr.status.money += (long)rewardData.Reward;
				break;
			}
			num = (long)rewardData.TargetScore;
		}
		if (num2 <= 0L)
		{
			Result_Display.SetResultData("NextRank", "RankMax", Result_Display.ResultType.Image);
		}
		else
		{
			Result_Display.SetResultData("NextRank", Utility.ConvertMoneyText(num2), Result_Display.ResultType.String);
		}
	}

	private int GetAppealMultiple(DanceBattle_Mgr.CharaType chara_type)
	{
		if (!Appeal_Mgr.Instance.IsActive || !Appeal_Mgr.Instance.GetAppealData(chara_type).IsAppealNow)
		{
			return 1;
		}
		Appeal_Mgr.Instance.AddAppealCombo(chara_type);
		if (RhythmAction_Mgr.IsVSDance)
		{
			return this.m_AppealMagnification;
		}
		return 1;
	}

	public long GetScore(DanceBattle_Mgr.CharaType type)
	{
		if (this.m_CharaScoreList.ContainsKey(type))
		{
			return this.m_CharaScoreList[type].nScore;
		}
		return 0L;
	}

	public int GetCombo(DanceBattle_Mgr.CharaType type)
	{
		if (this.m_CharaScoreList.ContainsKey(type))
		{
			return this.m_CharaScoreList[type].nCombo;
		}
		return 0;
	}

	public void AddScore(Dance_Note.Evaluation evalue, DanceBattle_Mgr.CharaType chara_type, bool is_special)
	{
		if (this.m_CharaScoreList.ContainsKey(chara_type))
		{
			if (evalue != Dance_Note.Evaluation.PERFECT && evalue != Dance_Note.Evaluation.GREAT)
			{
				this.m_CharaScoreList[chara_type].nCombo = 0;
				if (chara_type == DanceBattle_Mgr.CharaType.Player)
				{
					this.m_IsExistMiss = true;
				}
			}
			else
			{
				this.m_CharaScoreList[chara_type].nCombo++;
				if (chara_type == DanceBattle_Mgr.CharaType.Player)
				{
					base.StartCoroutine(this.TextScaling());
				}
			}
			if (this.m_CharaScoreList[chara_type].nCombo > 99999)
			{
				this.m_CharaScoreList[chara_type].nCombo = 99999;
			}
			int voltage = (chara_type != DanceBattle_Mgr.CharaType.Player) ? DanceBattle_Mgr.EnemyData.UseVoltage : Voltage_Mgr.Instance.VoltageStage;
			if (is_special)
			{
				for (int i = 0; i < Note_Mgr.Instance.GetSpecial; i++)
				{
					int num = this.BaceScore(evalue) + this.JudgeNumber(evalue) * this.Combo_Correct(this.m_CharaScoreList[chara_type].nCombo + i) + this.GetVoltageCorrect(voltage, evalue);
					num *= this.GetAppealMultiple(chara_type);
					this.m_CharaScoreList[chara_type].nScore += (long)num;
				}
				if (chara_type == DanceBattle_Mgr.CharaType.Player)
				{
					base.StartCoroutine(RhythmAction_Mgr.Instance.DanceTimeCoroutine(this.m_ScoreScalingTime, new Action<float>(this.ScoreScaling), null));
				}
			}
			else
			{
				int num2 = this.BaceScore(evalue) + this.JudgeNumber(evalue) * this.Combo_Correct(this.m_CharaScoreList[chara_type].nCombo) + this.GetVoltageCorrect(voltage, evalue);
				if (MotionAction_Mgr.IsCaptureOK && chara_type == DanceBattle_Mgr.CharaType.Player)
				{
					num2 += this.m_McSuccesBonus;
				}
				num2 *= this.GetAppealMultiple(chara_type);
				this.m_CharaScoreList[chara_type].nScore += (long)num2;
			}
			if (this.m_CharaScoreList[chara_type].nScore > 9999999999L)
			{
				this.m_CharaScoreList[chara_type].nScore = 9999999999L;
			}
			if (chara_type == DanceBattle_Mgr.CharaType.Player)
			{
				this.m_ComboCount = Mathf.Max(this.m_ComboCount, this.m_CharaScoreList[chara_type].nCombo);
				Dictionary<Dance_Note.Evaluation, int> evalueCombo;
				(evalueCombo = this.m_EvalueCombo)[evalue] = evalueCombo[evalue] + 1;
				this.m_NoteCount++;
			}
			if (this.m_CharaScoreList[chara_type].nCombo > 1 && this.m_CharaScoreList[chara_type].nCombo % Note_Mgr.Instance.SpecialCombo == 0)
			{
				Note_Mgr.Instance.SetSpecialFlag(chara_type, true);
			}
		}
	}

	public void NotMissClear()
	{
		if (!this.m_CharaScoreList.ContainsKey(DanceBattle_Mgr.CharaType.Enemy))
		{
			return;
		}
		this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore += this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Enemy].nScore;
	}

	public override void StartAction()
	{
		Sprite sprite = null;
		if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.BenchMark)
		{
			sprite = Resources.Load<Sprite>("SceneDance/Rhythm_Action/Sprite/TrialIcon");
		}
		else
		{
			Texture2D texture2D = (!RhythmAction_Mgr.Instance.UserMaid) ? null : RhythmAction_Mgr.Instance.UserMaid.GetThumIcon();
			if (texture2D)
			{
				sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), Vector2.one * 0.5f);
			}
		}
		if (sprite)
		{
			this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].MaidIcon.sprite2D = sprite;
			this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].MaidIcon.width = this.m_IconWidth;
			this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].MaidIcon.height = this.m_IconHeight;
		}
		this.SetNameUI(DanceBattle_Mgr.CharaType.Player, this.m_PlayerScoreUI);
		if (this.m_CharaScoreList.ContainsKey(DanceBattle_Mgr.CharaType.Enemy))
		{
			this.SetNameUI(DanceBattle_Mgr.CharaType.Enemy, this.m_EnemyScoreUI);
			this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Enemy].MaidIcon.sprite2D = DanceBattle_Mgr.EnemyData.Icon;
		}
	}

	public override void EndAction()
	{
		Result_Display.SetResultData("Score", Utility.ConvertMoneyText(this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore), Result_Display.ResultType.String);
		switch (RhythmAction_Mgr.NowDance)
		{
		case RhythmAction_Mgr.DanceType.Free:
		case RhythmAction_Mgr.DanceType.Challenge:
			Result_Display.SetResultData("Perfect", this.m_EvalueCombo[Dance_Note.Evaluation.PERFECT].ToString(), Result_Display.ResultType.String);
			Result_Display.SetResultData("Great", this.m_EvalueCombo[Dance_Note.Evaluation.GREAT].ToString(), Result_Display.ResultType.String);
			Result_Display.SetResultData("Good", this.m_EvalueCombo[Dance_Note.Evaluation.GOOD].ToString(), Result_Display.ResultType.String);
			Result_Display.SetResultData("Bad", this.m_EvalueCombo[Dance_Note.Evaluation.BAD].ToString(), Result_Display.ResultType.String);
			Result_Display.SetResultData("Miss", this.m_EvalueCombo[Dance_Note.Evaluation.MISS].ToString(), Result_Display.ResultType.String);
			Result_Display.SetResultData("Rate", Mathf.Floor(this.Score_Rate()).ToString(), Result_Display.ResultType.String);
			Result_Display.SetResultData("Combo", this.m_ComboCount.ToString(), Result_Display.ResultType.String);
			if (RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.Challenge)
			{
				this.DecideReward();
			}
			break;
		case RhythmAction_Mgr.DanceType.VS:
		{
			RhythmAction_Mgr.DanceState nowState = RhythmAction_Mgr.NowState;
			if (nowState != RhythmAction_Mgr.DanceState.Dance_First)
			{
				if (nowState == RhythmAction_Mgr.DanceState.Dance_Second)
				{
					Result_Display.SetResultData("2ndScore", Utility.ConvertMoneyText(this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore), Result_Display.ResultType.String);
				}
			}
			else
			{
				Result_Display.SetResultData("1stScore", Utility.ConvertMoneyText(this.m_CharaScoreList[DanceBattle_Mgr.CharaType.Player].nScore), Result_Display.ResultType.String);
				Result_Display.SetResultData("2ndScore", "NotSecond", Result_Display.ResultType.Image);
			}
			break;
		}
		}
	}

	[SerializeField]
	private int m_Max_Correct = 100;

	private const long m_Max_Score = 9999999999L;

	private const int m_Max_Combo = 99999;

	[SerializeField]
	private GameObject m_PlayerScoreUI;

	[SerializeField]
	private GameObject m_EnemyScoreUI;

	private Dictionary<DanceBattle_Mgr.CharaType, Score_Mgr.CharaScoreData> m_CharaScoreList = new Dictionary<DanceBattle_Mgr.CharaType, Score_Mgr.CharaScoreData>();

	[SerializeField]
	[Header("コンボ数に応じた文字の色")]
	private Score_Mgr.ComboTextColor[] m_ComboTextColor = new Score_Mgr.ComboTextColor[]
	{
		new Score_Mgr.ComboTextColor(0, Color.white),
		new Score_Mgr.ComboTextColor(101, Color.white),
		new Score_Mgr.ComboTextColor(201, Color.white),
		new Score_Mgr.ComboTextColor(301, Color.white),
		new Score_Mgr.ComboTextColor(500, Color.white)
	};

	private Dictionary<Dance_Note.Evaluation, int> m_EvalueCombo = new Dictionary<Dance_Note.Evaluation, int>
	{
		{
			Dance_Note.Evaluation.MISS,
			0
		},
		{
			Dance_Note.Evaluation.BAD,
			0
		},
		{
			Dance_Note.Evaluation.GOOD,
			0
		},
		{
			Dance_Note.Evaluation.GREAT,
			0
		},
		{
			Dance_Note.Evaluation.PERFECT,
			0
		}
	};

	private List<Score_Mgr.RewardData> m_AllRewardData = new List<Score_Mgr.RewardData>();

	[SerializeField]
	[Header("各評価ごとに設定する項目")]
	private List<Score_Mgr.EvalueSetting> m_EvalueSetList = new List<Score_Mgr.EvalueSetting>
	{
		new Score_Mgr.EvalueSetting(Dance_Note.Evaluation.MISS, 0, 0),
		new Score_Mgr.EvalueSetting(Dance_Note.Evaluation.BAD, 0, 50),
		new Score_Mgr.EvalueSetting(Dance_Note.Evaluation.GOOD, 2, 100),
		new Score_Mgr.EvalueSetting(Dance_Note.Evaluation.GREAT, 3, 200),
		new Score_Mgr.EvalueSetting(Dance_Note.Evaluation.PERFECT, 4, 300)
	};

	private int m_NoteCount;

	[SerializeField]
	private UILabel m_ComboText;

	[SerializeField]
	private UISprite m_ComboText_Child;

	[SerializeField]
	private UILabel m_FlashCombo;

	[SerializeField]
	private UISprite m_FlashCombo_Child;

	[SerializeField]
	[Header("コンボ時の文字拡縮設定")]
	private float m_ScalingTime = 0.5f;

	[SerializeField]
	private float m_ScalingValue = 1.5f;

	private bool m_ScalingNow;

	[SerializeField]
	[Header("アイコンのサイズ")]
	private int m_IconWidth = 60;

	[SerializeField]
	private int m_IconHeight = 60;

	[SerializeField]
	[Header("各ボルテージごとの補正スコア")]
	private Score_Mgr.VoltageCorrect[] m_VoltageCorrect = new Score_Mgr.VoltageCorrect[]
	{
		new Score_Mgr.VoltageCorrect(1),
		new Score_Mgr.VoltageCorrect(2),
		new Score_Mgr.VoltageCorrect(3),
		new Score_Mgr.VoltageCorrect(4),
		new Score_Mgr.VoltageCorrect(5),
		new Score_Mgr.VoltageCorrect(6)
	};

	private int m_ComboCount;

	[SerializeField]
	[Header("アピール中のスコア倍率")]
	private int m_AppealMagnification = 3;

	[SerializeField]
	[Header("MC成功時のボーナスポイント")]
	private int m_McSuccesBonus = 100;

	[SerializeField]
	[Header("スコア文字拡縮")]
	private float m_ScoreScaling = 1.5f;

	[SerializeField]
	private float m_ScoreScalingTime = 1f;

	private bool m_IsExistMiss;

	private static Score_Mgr m_Instance;

	private class CharaScoreData
	{
		public long nScore;

		public int nCombo;

		public UILabel ScoreText;

		public UI2DSprite MaidIcon;

		public string CharaName;
	}

	[Serializable]
	private class ComboTextColor
	{
		public ComboTextColor(int combo, Color color)
		{
			this.Combo = combo;
			this.TextColor = color;
		}

		[Header("コンボ数")]
		public int Combo;

		[Header("文字色")]
		public Color TextColor;
	}

	private class RewardData
	{
		public string Rank;

		public string Condition;

		public int TargetScore;

		public int Reward;
	}

	[Serializable]
	private class EvalueSetting
	{
		public EvalueSetting(Dance_Note.Evaluation evalue, int judge, int score)
		{
			this.Evalue = evalue;
			this.JudgeNo = judge;
			this.BaceScore = score;
		}

		public Dance_Note.Evaluation Evalue;

		[Header("判定番号")]
		public int JudgeNo;

		[Header("基本スコア")]
		public int BaceScore;
	}

	[Serializable]
	private class VoltageCorrect
	{
		public VoltageCorrect(int voltage)
		{
			this.Voltage = voltage;
		}

		public int Voltage;

		public int Correct;
	}
}