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

public class LoadingDownLoad : LoadingBase
{
	private class ClassDetail
	{
		public Texture TextureBase;

		public string strName;
	}

	private class ClassPack
	{
		public int ID = -1;

		public ClassDetail Detail;
	}

	private enum eCharacterObjsIndex
	{
		Character
	}

	public enum eType
	{
		CardDetail,
		CharacterDetail,
		StoryDetail
	}

	private enum eSide
	{
		Left,
		Right
	}

	[SerializeField]
	private BoxCollider[] _flickColliderArray;

	[SerializeField]
	private LoadingDownLoadCardView _cardView;

	[SerializeField]
	private LoadingDownLoadStoryView _storyView;

	[SerializeField]
	private NguiObjs CharacterDetailObjs;

	[SerializeField]
	private TweenPosition TweenPosCloseUpTexture;

	[SerializeField]
	private TweenAlpha TweenAlphaName;

	[SerializeField]
	private TweenAlpha TweenAlphaDescription;

	[SerializeField]
	private UILabel LabelClassName;

	[SerializeField]
	private UILabel LabelClassDesc;

	private readonly string[] CharacterTexturePath = new string[8] { "class_01_base", "class_02_base", "class_03_base", "class_04_base", "class_05_base", "class_06_base", "class_07_base", "class_08_base" };

	private const string PREFIX_FILE_TARGET = "class_03_base";

	[SerializeField]
	private NguiObjs CharacterSelectCircle;

	public const string loadPrefix = "Images/Loading/";

	private Dictionary<int, ClassPack> m_DicPack;

	private Vector3 m_CharacterDefaultPos = new Vector3(0f, 0f, 0f);

	private Vector3 m_CharacterLeftFromPos = new Vector3(-50f, 0f, 0f);

	private Vector3 m_CharacterRightFromPos = new Vector3(50f, 0f, 0f);

	private int m_SelectCharacterIndex;

	private float _defaultEvoLineRootY;

	private const float CHARACTER_TWEEN_TIME = 0.1f;

	private const int CHARACTER_MAX = 9;

	private const int CHARACTER_ID_START = 1;

	private const int CHARACTER_ID_END = 8;

	[SerializeField]
	private UIButton[] CursorButton;

	private eType Type;

	public eType LoadingDownLoadImageType;

	private bool _isAnimatingNowCharacterView;

	private bool _canFlick = true;

	protected override void Start()
	{
		setMaxAssetsNum(enable: true, Toolbox.ResourcesManager.GetDownloadMaxSize());
		base.Start();
		if (!Data.Load.data._userTutorial.NeedAllResource)
		{
			Type = eType.CharacterDetail;
		}
		else
		{
			Type = LoadingDownLoadImageType;
		}
		if (Type == eType.CardDetail && !_cardView.CanView())
		{
			Type = eType.CharacterDetail;
		}
		isLabelUpdate = false;
		LoadTitleLabel.text = Data.SystemText.Get("Load_0001");
		LoadTextLabel.text = Data.SystemText.Get("Load_0002");
		CursorButton[0].onClick.Add(new EventDelegate(delegate
		{
			ChangeInfoView(eSide.Left);
		}));
		CursorButton[1].onClick.Add(new EventDelegate(delegate
		{
			ChangeInfoView(eSide.Right);
		}));
		BoxCollider[] flickColliderArray = _flickColliderArray;
		foreach (BoxCollider obj in flickColliderArray)
		{
			UIEventListener uIEventListener = UIEventListener.Get(obj.gameObject);
			uIEventListener.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uIEventListener.onDrag, new UIEventListener.VectorDelegate(OnDrag));
			UIEventListener uIEventListener2 = UIEventListener.Get(obj.gameObject);
			uIEventListener2.onDragEnd = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onDragEnd, new UIEventListener.VoidDelegate(OnDragEnd));
		}
		CharacterDetailObjs.gameObject.SetActive(value: false);
		_storyView.gameObject.SetActive(value: false);
		_cardView.gameObject.SetActive(value: false);
		if (CustomPreference.GetTextLanguage() != Global.LANG_TYPE.Jpn.ToString() && Type == eType.CardDetail)
		{
			Type = eType.StoryDetail;
		}
		switch (Type)
		{
		case eType.CardDetail:
			_cardView.gameObject.SetActive(value: true);
			_cardView.Initialize(delegate
			{
				base.gameObject.SetActive(value: true);
			});
			break;
		case eType.CharacterDetail:
			CharacterDetailObjs.gameObject.SetActive(value: true);
			InitializeCharacterView();
			break;
		case eType.StoryDetail:
			_storyView.gameObject.SetActive(value: true);
			_storyView.Initialize();
			break;
		}
	}

	private void OnDrag(GameObject g, Vector2 dir)
	{
		if (_canFlick && !_isAnimatingNowCharacterView && !_cardView.IsAnimatingNow)
		{
			if (dir.x >= 70f)
			{
				ChangeInfoView(eSide.Left);
				_canFlick = false;
			}
			else if (dir.x <= -70f)
			{
				ChangeInfoView(eSide.Right);
				_canFlick = false;
			}
		}
	}

	private void OnDragEnd(GameObject go)
	{
		_canFlick = true;
	}

	private void InitializeCharacterView()
	{
		m_SelectCharacterIndex = 1;
		IList<int> list = new List<int>();
		for (int i = 1; i < 9; i++)
		{
			list.Add(i);
		}
		if (m_DicPack == null)
		{
			m_DicPack = new Dictionary<int, ClassPack>();
			foreach (int item in list)
			{
				ClassPack classPack = new ClassPack();
				classPack.ID = item;
				classPack.Detail = null;
				m_DicPack.Add(item, classPack);
			}
			foreach (KeyValuePair<int, ClassPack> item2 in m_DicPack)
			{
				ClassPack value = item2.Value;
				ClassDetail classDetail = new ClassDetail
				{
					TextureBase = null,
					strName = null
				};
				int key = item2.Key;
				string text = CharacterTexturePath[key - 1];
				if (text == "class_03_base")
				{
					text += "_1";
				}
				classDetail.TextureBase = Resources.Load<Texture>("Images/Loading/" + text);
				classDetail.strName = getTextCharacterName(key);
				value.Detail = classDetail;
			}
		}
		SetCharacterDisp(m_DicPack[1]);
	}

	protected override float CalculateProgress()
	{
		return Toolbox.ResourcesManager.GetDownloadCompletedSize() + (float)Toolbox.ResourcesManager.GetLoadingCompleted();
	}

	public override void fadeOutLoading()
	{
		base.fadeOutLoading();
		_cardView.FadeOutLoading();
	}

	protected override void OnDestroy()
	{
		base.OnDestroy();
	}

	private void ChangeInfoView(eSide side)
	{
		if (!UIManager.GetInstance().isOpenDialog())
		{
			switch (Type)
			{
			case eType.CardDetail:
				ChangeCardView(side);
				break;
			case eType.CharacterDetail:
				ChangeCharacterView(side);
				break;
			case eType.StoryDetail:
				ChangeStoryView(side);
				break;
			}
		}
	}

	private void ChangeCardView(eSide side)
	{
		switch (side)
		{
		case eSide.Right:
			_cardView.Next();
			break;
		case eSide.Left:
			_cardView.Prev();
			break;
		}
	}

	private void ChangeStoryView(eSide side)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		_storyView.ChangeStoryImage(side == eSide.Right);
	}

	private void ChangeCharacterView(eSide side)
	{
		bool isRight = side == eSide.Right;
		StartCoroutine(ChangeCharacterAnm(isRight));
	}

	private IEnumerator ChangeCharacterAnm(bool isRight = true)
	{
		_isAnimatingNowCharacterView = true;
		float time = 0f;
		if (!isRight)
		{
			if (m_SelectCharacterIndex > 1)
			{
				m_SelectCharacterIndex--;
			}
			else
			{
				m_SelectCharacterIndex = 8;
			}
		}
		else if (m_SelectCharacterIndex < 8)
		{
			m_SelectCharacterIndex++;
		}
		else
		{
			m_SelectCharacterIndex = 1;
		}
		ClassPack characterDisp = m_DicPack[m_SelectCharacterIndex];
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
		SetCharacterDisp(characterDisp);
		TweenPosition tweenPosCloseUpTexture = TweenPosCloseUpTexture;
		if (isRight)
		{
			tweenPosCloseUpTexture.from = m_CharacterRightFromPos;
		}
		else
		{
			tweenPosCloseUpTexture.from = m_CharacterLeftFromPos;
		}
		tweenPosCloseUpTexture.to = m_CharacterDefaultPos;
		tweenPosCloseUpTexture.duration = 0.1f;
		tweenPosCloseUpTexture.ResetToBeginning();
		tweenPosCloseUpTexture.PlayForward();
		TweenAlpha tweenAlphaName = TweenAlphaName;
		tweenAlphaName.from = 0f;
		tweenAlphaName.to = 1f;
		tweenAlphaName.duration = 0.1f;
		tweenAlphaName.ResetToBeginning();
		tweenAlphaName.PlayForward();
		TweenAlpha tweenAlphaDescription = TweenAlphaDescription;
		tweenAlphaDescription.from = 0f;
		tweenAlphaDescription.to = 1f;
		tweenAlphaDescription.duration = 0.1f;
		tweenAlphaDescription.ResetToBeginning();
		tweenAlphaDescription.PlayForward();
		StartCoroutine(assetSetting());
		yield return null;
		do
		{
			time += Time.deltaTime;
		}
		while (!(time >= 0.1f));
		_isAnimatingNowCharacterView = false;
	}

	private void SetCharacterDisp(ClassPack in_Pack)
	{
		for (int i = 0; i < CharacterSelectCircle.objs.Count; i++)
		{
			CharacterSelectCircle.objs[i].SetActive(value: false);
		}
		CharacterSelectCircle.objs[m_SelectCharacterIndex - 1].SetActive(value: true);
		CharacterDetailObjs.textures[0].mainTexture = in_Pack.Detail.TextureBase;
		LabelClassName.text = in_Pack.Detail.strName;
		LabelClassDesc.SetWrapText(getTextClassDescription(m_SelectCharacterIndex));
	}

	private string getTextClassDescription(int classID)
	{
		SystemText systemText = Data.SystemText;
		return classID switch
		{
			1 => systemText.Get("Load_0010"), 
			2 => systemText.Get("Load_0011"), 
			3 => systemText.Get("Load_0012"), 
			4 => systemText.Get("Load_0013"), 
			5 => systemText.Get("Load_0014"), 
			6 => systemText.Get("Load_0015"), 
			7 => systemText.Get("Load_0016"), 
			8 => systemText.Get("Load_0018"), 
			_ => null, 
		};
	}

	private string getTextCharacterName(int classID)
	{
		SystemText systemText = Data.SystemText;
		return classID switch
		{
			1 => systemText.Get("Load_0003"), 
			2 => systemText.Get("Load_0004"), 
			3 => systemText.Get("Load_0005"), 
			4 => systemText.Get("Load_0006"), 
			5 => systemText.Get("Load_0007"), 
			6 => systemText.Get("Load_0008"), 
			7 => systemText.Get("Load_0009"), 
			8 => systemText.Get("Load_0017"), 
			_ => null, 
		};
	}
}
