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

namespace Wizard;

public class RedEtherCampaignPanel : MonoBehaviour
{
	private const float GAUGEUP_DELAY = 0.1f;

	private const float GAUGEUP_DURATION = 0.3f;

	private const int GAUGEUP_SE_CNT = 6;

	private const float FADE_TIME = 0.2f;

	[SerializeField]
	public ParticleSystem _gaugeEffect;

	[SerializeField]
	private ParticleSystem _lineEffect;

	[SerializeField]
	private UIGauge _uIGauge;

	[SerializeField]
	private UITexture _redEtherIcon;

	[SerializeField]
	private UIButton _tocuhCollider;

	[SerializeField]
	private UILabel _battleRewardLabel;

	[SerializeField]
	private GameObject _missionEtherRoot;

	[SerializeField]
	private GameObject _battleEtherRoot;

	[SerializeField]
	private UILabel _battleWinPoint;

	[SerializeField]
	private UILabel _battleWinTitle;

	[SerializeField]
	private RedEtherCampaignMissionLabel _missionLabelOriginal;

	[SerializeField]
	private GameObject[] _redEtherAtlasList;

	[SerializeField]
	private UIPanel _panel;

	[SerializeField]
	private UIGrid _missionTextGrid;

	private List<string> _loadFileList = new List<string>();

	private RedEtherCampaignResultData _data;

	private List<RedEtherCampaignMissionLabel> _clearMissionList = new List<RedEtherCampaignMissionLabel>();

	private bool _isTouchColliderClicked;

	private const float GAUGEUP_LABEL_DURATION = 0.5f;

	private const float GAUGEUP_LABEL_MOVE_DISTANCE = 50f;

	private const float WAIT_GAUGE_ANIMATION_AFTER = 1f;

	private Action OnFinish { get; set; }

	public bool IsAnimationComplete { get; private set; }

	public static void Create(GameObject parent, RedEtherCampaignResultData data, BattleResultUIController controller, Action onFinish)
	{
		controller.GreySpriteBGVisible = true;
		NGUITools.AddChild(parent, Resources.Load("UI/layoutParts/Other/RedEtherCampaignPanel") as GameObject).GetComponent<RedEtherCampaignPanel>().Initialize(data, onFinish);
	}

	public void Initialize(RedEtherCampaignResultData data, Action onFinish)
	{
		_panel.alpha = 0f;
		_data = data;
		_uIGauge.Value = data.BeforeGaugeValue;
		OnFinish = onFinish;
		UpdateGaugeAnimation(0f);
		_missionEtherRoot.SetActive(value: false);
		_battleEtherRoot.SetActive(value: false);
		_battleWinPoint.text = "+" + data.BattleRewardEther;
		_battleRewardLabel.text = data.BeforeDailyEther + "/" + data.MaxDailyEther;
		foreach (RedEtherCampaignResultData.ClearMissionInfo clearMission in data.ClearMissionList)
		{
			RedEtherCampaignMissionLabel component = NGUITools.AddChild(_missionLabelOriginal.transform.parent.gameObject, _missionLabelOriginal.gameObject).GetComponent<RedEtherCampaignMissionLabel>();
			component.Initialize(clearMission);
			_clearMissionList.Add(component);
		}
		_battleWinTitle.text = Data.SystemText.Get("RedEther_0010", _data.CanGainBattleWin.ToString());
		_missionTextGrid.Reposition();
		StartCoroutine(LoadResources(delegate
		{
			TweenAlpha.Begin(_panel.gameObject, 0.2f, 1f);
			UIManager.GetInstance().AddResidentAtlas(UIAtlasManager.AssetBundleNames.RedEtherCampaign);
			List<GameObject> obj_list = new List<GameObject>(_redEtherAtlasList);
			UIManager.GetInstance().AttachAtlas(obj_list);
			_redEtherIcon.mainTexture = Toolbox.ResourcesManager.LoadObject(GetIconTextureName(isFetch: true)) as Texture;
			_tocuhCollider.onClick.Add(new EventDelegate(delegate
			{
				OnClickTouchCollider();
			}));
			StartCoroutine(AnimationCoroutine());
			StartCoroutine(MissionLabelAnimation());
			_lineEffect.gameObject.SetActive(value: true);
			_lineEffect.Play();
		}));
	}

	private void StartAnimation()
	{
		SetGaugeAniamtion();
	}

	private IEnumerator LoadResources(Action callBack)
	{
		List<string> loadList = new List<string>
		{
			GetIconTextureName(isFetch: false),
			GetAtlasName(isFetch: false)
		};
		yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(loadList, null));
		_loadFileList.AddRange(loadList);
		List<GameObject> list = new List<GameObject>();
		list.Add(_gaugeEffect.gameObject);
		list.Add(_lineEffect.gameObject);
		_loadFileList.AddRange(GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(list, delegate
		{
			callBack.Call();
		}));
	}

	private void OnDestroy()
	{
		UIManager.GetInstance().RemoveResidentAtlas(UIAtlasManager.AssetBundleNames.RedEtherCampaign);
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadFileList);
		_loadFileList.Clear();
	}

	private string GetIconTextureName(bool isFetch)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath("icon_liquid_m", ResourcesManager.AssetLoadPathType.Item, isFetch);
	}

	private string GetAtlasName(bool isFetch)
	{
		return UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.RedEtherCampaign, "", isFetch);
	}

	private IEnumerator MissionLabelAnimation()
	{
		_battleEtherRoot.SetActive(_data.BattleRewardEther > 0);
		foreach (RedEtherCampaignMissionLabel clearMission in _clearMissionList)
		{
			clearMission.gameObject.SetActive(value: true);
		}
		_missionTextGrid.Reposition();
		_battleEtherRoot.SetActive(value: false);
		foreach (RedEtherCampaignMissionLabel clearMission2 in _clearMissionList)
		{
			clearMission2.gameObject.SetActive(value: false);
		}
		yield return new WaitForSeconds(0.5f);
		if (_data.BattleRewardEther > 0)
		{
			_battleEtherRoot.SetActive(value: true);
			TextAnimation(_battleWinTitle.gameObject);
			TextAnimation(_battleWinPoint.gameObject);
		}
		foreach (RedEtherCampaignMissionLabel clearMission3 in _clearMissionList)
		{
			clearMission3.gameObject.SetActive(value: true);
			TextAnimation(clearMission3._missionName.gameObject);
			TextAnimation(clearMission3._redEtherCount.gameObject);
		}
	}

	private void TextAnimation(GameObject label)
	{
		label.SetActive(value: true);
		TweenAlpha.Begin(label.gameObject, 0.5f, 1f);
		iTween.MoveFrom(label, iTween.Hash("x", label.transform.localPosition.x - 50f, "time", 0.5f, "islocal", true, "easetype", iTween.EaseType.easeOutExpo));
	}

	private IEnumerator AnimationCoroutine()
	{
		yield return new WaitForSeconds(0.5f);
		StartAnimation();
		yield return new WaitForSeconds(1.4f);
		_isTouchColliderClicked = false;
		while (!_isTouchColliderClicked)
		{
			yield return null;
		}
		FadeOutAnimation(0.2f);
		yield return new WaitForSeconds(0.2f);
		_lineEffect.gameObject.SetActive(value: false);
		OnFinish.Call();
	}

	private void OnClickTouchCollider()
	{
		_isTouchColliderClicked = true;
	}

	private void SetGaugeAniamtion()
	{
		_uIGauge.Value = _data.BeforeGaugeValue;
		iTween.ValueTo(base.gameObject, iTween.Hash("from", 0f, "to", 1f, "time", 0.3f, "delay", 0.1f, "onstart", "StartGaugeAnimation", "onupdate", "UpdateGaugeAnimation", "oncomplete", "OnFinishGaugeAnimation", "easetype", iTween.EaseType.easeOutQuad));
	}

	private void StartGaugeAnimation()
	{
		PlayGaugeUpSE();
		_gaugeEffect.gameObject.SetActive(value: true);
		_gaugeEffect.Play();
	}

	private void UpdateGaugeAnimation(float t)
	{
		_uIGauge.Value = Mathf.Lerp(_data.BeforeGaugeValue, _data.AfterGaugeValue, t);
		int num = (int)Mathf.Lerp(_data.BeforeDailyEther, _data.AfterDailyEther, t);
		_battleRewardLabel.text = num + "/" + _data.MaxDailyEther;
	}

	private void OnFinishGaugeAnimation()
	{
		_gaugeEffect.Stop();
	}

	private void PlayGaugeUpSE()
	{
		StartCoroutine(GaugeUpSECoroutine());
	}

	private IEnumerator GaugeUpSECoroutine()
	{
		for (int i = 0; i < 6; i++)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_RESULT_GAUGEUP);
			yield return new WaitForSeconds(0.05f);
		}
	}

	private void FadeOutAnimation(float fadeTime)
	{
		TweenAlpha.Begin(base.gameObject, 0f, 1f);
		TweenAlpha.Begin(base.gameObject, fadeTime, 0f);
	}
}
