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

namespace Wizard;

public class MyPageSpecialWinRewardDialog : MonoBehaviour
{
	private const string GRADE_GAUGE_NAME = "gauge_bg_scale_15";

	private float[] _memoryValue = new float[16]
	{
		0f, 0.0585f, 0.126f, 0.194f, 0.263f, 0.331f, 0.399f, 0.467f, 0.535f, 0.602f,
		0.67f, 0.738f, 0.806f, 0.875f, 0.943f, 1f
	};

	private const int TREASURE_BOX_SPRITE_GRADE = 5;

	[SerializeField]
	private UISprite _boxSprite;

	[SerializeField]
	private UIGauge _gradeGauge;

	[SerializeField]
	private UISprite _gaugeBackground;

	[SerializeField]
	private UILabel _treasureBoxLabel;

	[SerializeField]
	private UILabel _titleLabel;

	[SerializeField]
	private UILabel _completeLabel;

	[SerializeField]
	private GameObject _clickObject;

	[SerializeField]
	private BoxCollider _clickObjectCollider;

	[SerializeField]
	private UILabel _remineNumLabel;

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

	private UIAtlas _arenaAtlas;

	private BoxCollider _notTouchMypageCollider;

	private GameObject _boxOpenEffectObj;

	private GameObject _treasureEffectObj;

	public static GameObject Create(GameObject prefab, BoxCollider notTouchMypageCollider)
	{
		GameObject obj = UnityEngine.Object.Instantiate(prefab);
		obj.GetComponentInChildren<MyPageSpecialWinRewardDialog>().Initialize(notTouchMypageCollider);
		return obj;
	}

	private void Initialize(BoxCollider notTouchMypageCollider)
	{
		SystemText systemText = Data.SystemText;
		_notTouchMypageCollider = notTouchMypageCollider;
		SpecialTreasureInfo info = Data.MyPageNotifications.data.CampaignBattleWin.SpecialTreasureInfo;
		_titleLabel.text = systemText.Get("MyPage_0109", info.SpecialTreasureBoxNecessaryNumbers.ToString());
		_remineNumLabel.text = systemText.Get("MyPage_0113", (info.SpecialTreasureBoxNecessaryNumbers - info.ReceivedBoxNumbers).ToString());
		_gradeGauge.Value = _memoryValue[info.ReceivedBoxNumbers];
		_treasureBoxLabel.text = (info.IsGotSpecialTreasureBox ? systemText.Get("TreasureBoxCp_0009") : "");
		UIManager.SetObjectToGrey(_boxSprite.gameObject, info.IsGotSpecialTreasureBox);
		_completeLabel.gameObject.SetActive(info.ReceivedBoxNumbers == info.SpecialTreasureBoxNecessaryNumbers);
		_clickObject.SetActive(info.IsTreasureBoxReadyToOpen);
		_clickObjectCollider.enabled = info.IsTreasureBoxReadyToOpen;
		string atlasName = UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.Arena, null);
		_loadFileList.Add(atlasName);
		StartCoroutine(Toolbox.ResourcesManager.LoadAssetAsync(atlasName, delegate
		{
			atlasName = UIManager.GetInstance().GetSceneAssetPath(UIAtlasManager.AssetBundleNames.Arena, null, isload: true);
			_arenaAtlas = Toolbox.ResourcesManager.LoadObject<GameObject>(atlasName).GetComponent<UIAtlas>();
			_boxSprite.atlas = _arenaAtlas;
			_gaugeBackground.atlas = UIManager.GetInstance().GetAtlasList().FirstOrDefault((UIAtlas s) => s.name == "MypageComon");
			_boxSprite.spriteName = (info.IsGotSpecialTreasureBox ? $"box_2pick_0{5}_open" : $"box_2pick_0{5}_close");
			_gaugeBackground.spriteName = "gauge_bg_scale_15";
		}));
		UIEventListener.Get(_boxSprite.gameObject).onClick = OnClickBoxReceiveRewards;
	}

	private void OnClickBoxReceiveRewards(GameObject g)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_LOTTERY_BOX_TOUCH);
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
		MypageReceiveSpecialTreasureTask receiveSpecialTreasureTask = new MypageReceiveSpecialTreasureTask();
		receiveSpecialTreasureTask.SetParameter();
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(receiveSpecialTreasureTask, delegate
		{
			UIManager.GetInstance().StartCoroutine(RunOpenBoxEffect(receiveSpecialTreasureTask.Result, delegate
			{
				Data.MyPageNotifications.data.CampaignBattleWin.SpecialTreasureInfo.SetGotSpecialTreasureBox(isGot: true);
				CreateRewardDialog(receiveSpecialTreasureTask, RedrawDialog);
				RunHideBoxEffect();
			}));
		}));
	}

	private IEnumerator RunOpenBoxEffect(MypageReceiveSpecialTreasureTask.MypageTreasureBoxCpOpenTaskData result, Action endAction)
	{
		UIManager.GetInstance().OpenNotTouch();
		_notTouchMypageCollider.enabled = true;
		yield return new WaitForSeconds(1f);
		_boxOpenEffectObj = NGUITools.AddChild(base.gameObject, Resources.Load("UI/layoutParts/RankWinnerRewardResultPanel") as GameObject);
		NguiObjs component = _boxOpenEffectObj.GetComponent<NguiObjs>();
		UISprite bg = component.sprites[0];
		UISprite window = component.sprites[1];
		UISprite box = component.sprites[2];
		box.atlas = _arenaAtlas;
		UILabel label = component.labels[0];
		label.text = Data.SystemText.Get("MyPage_0111");
		UIPanel panel = _boxOpenEffectObj.GetComponent<UIPanel>();
		panel.alpha = 0f;
		box.spriteName = $"box_2pick_0{5}_close";
		string loadEffectName = $"cmn_arena_treasure_{6}";
		List<string> list = new List<string>();
		list.Add(Toolbox.ResourcesManager.GetAssetTypePath(loadEffectName, ResourcesManager.AssetLoadPathType.Effect2D));
		yield return UIManager.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(list, null));
		_treasureEffectObj = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(loadEffectName, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true)) as GameObject);
		_treasureEffectObj.transform.parent = _boxOpenEffectObj.transform;
		GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(_treasureEffectObj, null);
		_boxOpenEffectObj.SetLayer(LayerMask.NameToLayer("SystemUI"), isSetChildren: true);
		bg.alpha = 0f;
		box.alpha = 0f;
		label.alpha = 0f;
		panel.alpha = 1f;
		TweenAlpha.Begin(bg.gameObject, 0.5f, 0.65f);
		TweenAlpha.Begin(label.gameObject, 0.5f, 1f);
		TweenAlpha.Begin(box.gameObject, 0.5f, 1f);
		label.transform.localPosition = new Vector3(100f, 0f, 0f);
		window.transform.localScale = new Vector3(1f, 0.01f, 1f);
		box.transform.localPosition = new Vector3(180f, 0f, 0f);
		iTween.MoveTo(label.gameObject, iTween.Hash("x", 20f, "time", 0.5f, "islocal", true, "easetype", iTween.EaseType.easeOutExpo));
		iTween.ScaleTo(window.gameObject, iTween.Hash("y", 1f, "time", 0.5f, "easetype", iTween.EaseType.easeInOutExpo));
		iTween.MoveTo(box.gameObject, iTween.Hash("y", 20f, "time", 0.5f, "islocal", true, "easetype", iTween.EaseType.easeOutExpo));
		yield return new WaitForSeconds(0.8f);
		TweenAlpha.Begin(label.gameObject, 0.5f, 0f);
		iTween.MoveTo(box.gameObject, iTween.Hash("x", 0f, "time", 0.7f, "islocal", true, "easetype", iTween.EaseType.easeInOutExpo));
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_2PICK_BOX_OPEN);
		yield return new WaitForSeconds(1f);
		_treasureEffectObj.transform.localPosition = box.transform.localPosition;
		_treasureEffectObj.transform.localScale = Vector3.one * 320f * 1.75f;
		_treasureEffectObj.SetActive(value: true);
		box.gameObject.SetActive(value: false);
		yield return new WaitForSeconds(1.2f);
		endAction.Call();
	}

	private void CreateRewardDialog(MypageReceiveSpecialTreasureTask info, Action onClose)
	{
		UIManager.GetInstance().createInSceneCenterLoading();
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetLayer("MyPage");
		dialogBase.SetPanelDepth(105, isSetBackViewDepthImmediately: true);
		dialogBase.SetTitleLabel(Data.SystemText.Get("TreasureBoxCp_0026"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
		RewardBase component = NGUITools.AddChild(dialogBase.gameObject, Resources.Load<GameObject>("UI/layoutParts/StoryRewardPanel")).GetComponent<RewardBase>();
		for (int i = 0; i < info.Result.RewardDataList.Count; i++)
		{
			component.AddReward(info.Result.RewardDataList[i]);
		}
		component.EndCreate();
		dialogBase.OnClose = delegate
		{
			onClose.Call();
		};
	}

	private void RunHideBoxEffect()
	{
		_treasureEffectObj.SetActive(value: false);
		_boxOpenEffectObj.SetActive(value: false);
		UnityEngine.Object.Destroy(_boxOpenEffectObj);
		UnityEngine.Object.Destroy(_treasureEffectObj);
		UIManager.GetInstance().offNotTouch();
		_notTouchMypageCollider.enabled = false;
	}

	private void RedrawDialog()
	{
		SpecialTreasureInfo specialTreasureInfo = Data.MyPageNotifications.data.CampaignBattleWin.SpecialTreasureInfo;
		_boxSprite.spriteName = (specialTreasureInfo.IsGotSpecialTreasureBox ? $"box_2pick_0{5}_open" : $"box_2pick_0{5}_close");
		UIManager.SetObjectToGrey(_boxSprite.gameObject, specialTreasureInfo.IsGotSpecialTreasureBox);
		_completeLabel.gameObject.SetActive(specialTreasureInfo.ReceivedBoxNumbers == specialTreasureInfo.SpecialTreasureBoxNecessaryNumbers);
		_treasureBoxLabel.text = (specialTreasureInfo.IsGotSpecialTreasureBox ? Data.SystemText.Get("TreasureBoxCp_0009") : "");
		_clickObject.SetActive(specialTreasureInfo.IsTreasureBoxReadyToOpen);
		MyPageMenu.Instance.SetAfterSpecialWinRewardOpened();
		MyPageMenu.Instance.SpecialWinRewardOpened();
	}
}
