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

[Serializable]
public class AreaSelectBG
{
	private const float BGTEXTURE_WIDTH = 1024f;

	private const float BGTEXTURE_HEIGHT = 1024f;

	private const int BGTEXTURE_NUM_X = 4;

	private const int BGTEXTURE_NUM_Y = 3;

	private const float BGTEXTURE_WIDTH_HALF_LEFT = 2048f;

	private const float BGTEXTURE_WIDTH_HALF_RIGHT = 2560f;

	private const float BGTEXTURE_HEIGHT_HALF_UP = 1536f;

	private const float BGTEXTURE_HEIGHT_HALF_BOTTOM = 1536f;

	private const float BGTEXTURE_DRAG_MARGIN = 5f;

	private const float BGTEXTURE_DRAG_DECELERATION = 0.875f;

	private const float BGTEXTURE_DRAGARROW_ANIM_SPEED = 1f;

	private const float BGDRAG_SEC_MAXSPEED = 0.25f;

	private const float BGDRAG_SEC_RESETUPTOTIME_MAX = 0.5f;

	private AreaSelectUI _areaSelectUI;

	[SerializeField]
	private GameObject _BGRoot;

	[SerializeField]
	private UITexture[] _BGTexture;

	[SerializeField]
	private AreaBGInfo _areaBGInfo;

	private ParticleSystem _bgEffect;

	private AreaSelectEffectControlBase _bgEffectControl;

	[SerializeField]
	private GameObject _BGDragCollision;

	private Vector2 _BGDragDelta = Vector2.zero;

	private bool _isBGDragEnable;

	private float _BGDragSec;

	private float _BGDragSecResetUpToTime;

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

	private bool _isLoadEndBGTexture = true;

	private bool _isLoadEndParticle = true;

	private StorySectionData _sectionData;

	private int? _sectionClassId;

	private List<ChapterExtraData> _extraChapters = new List<ChapterExtraData>();

	private bool _isNormalBgSet = true;

	private bool _changeChapterFirstCall = true;

	private float _currentAspectRatio;

	private Vector3 _currentBGScale = Vector3.one;

	private Vector3 _currentParentPos = Vector3.zero;

	private Vector2 _minMovablePos = Vector3.one;

	private Vector2 _maxMovablePos = Vector3.one;

	public ChapterExtraData ChapterExtraData { get; private set; }

	public ChapterExtraData TransitionChapterExtraData { get; private set; }

	public int BeforeChapterId { get; private set; }

	public GameObject GetBGRoot()
	{
		return _BGRoot;
	}

	public bool GetLoadEnd()
	{
		if (_isLoadEndBGTexture)
		{
			return _isLoadEndParticle;
		}
		return false;
	}

	public void SetActiveBGEffect(bool isActive)
	{
		_bgEffect.gameObject.SetActive(isActive);
		if (_bgEffectControl != null)
		{
			_bgEffectControl.SetActiveBGEffect(isActive);
		}
	}

	public bool IsBGDragEnable()
	{
		return _isBGDragEnable;
	}

	public void Init(AreaSelectUI areaSelectUI)
	{
		_areaSelectUI = areaSelectUI;
		UIEventListener component = _BGDragCollision.GetComponent<UIEventListener>();
		if (null != component)
		{
			component.onDrag = OnDragBG;
		}
		SetBGDragEnable(enable: false);
	}

	public void Term()
	{
		int i = 0;
		for (int num = _BGTexture.Length; i < num; i++)
		{
			_BGTexture[i].mainTexture = null;
		}
	}

	private string GetBackGroundPath(int backGroundId, int index, bool isFetch = false)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath("bg_story_" + backGroundId.ToString("00") + "_" + (index + 1).ToString("00"), ResourcesManager.AssetLoadPathType.Background, isFetch);
	}

	private string GetExtraBackGroundPath(StorySectionData sectionData, int index, string suffix, bool isFetch = false)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath("bg_story_" + sectionData.BackGroundId.ToString("00") + "_" + (index + 1).ToString("00") + suffix, ResourcesManager.AssetLoadPathType.Background, isFetch);
	}

	private string GetExtraBackGroundPath(int backgroundId, int index, string suffix, bool isFetch = false)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath("bg_story_" + backgroundId.ToString("00") + "_" + (index + 1).ToString("00") + suffix, ResourcesManager.AssetLoadPathType.Background, isFetch);
	}

	private string GetMapEffectPath(int backGroundId, bool isFetch = false)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath("scn_map_world_" + backGroundId, ResourcesManager.AssetLoadPathType.Effect2D, isFetch);
	}

	private string GetTreeEffectPath(int backGroundId, bool isFetch = false)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath("scn_map_world_tree_" + backGroundId, ResourcesManager.AssetLoadPathType.Effect2D, isFetch);
	}

	public void LoadBG(StorySectionData sectionData, int? sectionClassId)
	{
		_sectionData = sectionData;
		_sectionClassId = sectionClassId;
		_extraChapters = _areaBGInfo.GetExtraChapters(_sectionData.Id, sectionClassId);
		_loadedResources.Add(GetMapEffectPath(sectionData.BackGroundId));
		_isLoadEndBGTexture = false;
		for (int i = 0; i < _BGTexture.Length; i++)
		{
			_loadedResources.Add(GetBackGroundPath(sectionData.BackGroundId, i));
		}
		foreach (ChapterExtraData extraChapter in _extraChapters)
		{
			int num = sectionData.BackGroundId;
			if (extraChapter.IsUseOtherSectionBG())
			{
				num = extraChapter.BGSectionId;
				for (int j = 0; j < _BGTexture.Length; j++)
				{
					_loadedResources.Add(GetBackGroundPath(num, j));
				}
				_loadedResources.Add(GetMapEffectPath(num));
				if (extraChapter.AddTreeEffect)
				{
					_loadedResources.Add(GetTreeEffectPath(num));
				}
			}
			foreach (int item in extraChapter.ExtraTextureIndex)
			{
				_loadedResources.Add(GetExtraBackGroundPath(num, item, extraChapter.BGSuffix));
			}
			if (!string.IsNullOrEmpty(extraChapter.BGExtraEffectPath))
			{
				_loadedResources.Add(Toolbox.ResourcesManager.GetAssetTypePath(extraChapter.BGExtraEffectPath, ResourcesManager.AssetLoadPathType.Effect2D));
			}
			if (!string.IsNullOrEmpty(extraChapter.BGFirstClearEffectPath))
			{
				_loadedResources.Add(Toolbox.ResourcesManager.GetAssetTypePath(extraChapter.BGFirstClearEffectPath, ResourcesManager.AssetLoadPathType.Effect2D));
			}
		}
		_areaSelectUI.StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadedResources, _OnLoadEndBG));
		_isLoadEndParticle = false;
	}

	public void UnLoadBG()
	{
		Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResources);
		_loadedResources.Clear();
		_sectionData = null;
		_sectionClassId = null;
	}

	private void _OnLoadEndBG()
	{
		List<GameObject> list = new List<GameObject>();
		_bgEffect = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(GetMapEffectPath(_sectionData.BackGroundId, isFetch: true)) as GameObject).GetComponent<ParticleSystem>();
		Vector3 localScale = _bgEffect.transform.localScale;
		_bgEffect.transform.parent = _BGRoot.transform;
		_bgEffect.transform.localScale = localScale;
		_bgEffect.transform.localPosition = Vector3.zero;
		_bgEffectControl = _bgEffect.gameObject.GetComponent<AreaSelectEffectControlBase>();
		list.Add(_bgEffect.gameObject);
		if (_bgEffectControl != null)
		{
			_bgEffectControl._backGroundEffects.Add(_bgEffectControl.BASE_EFFECT_INDEX, _bgEffect);
			_bgEffectControl._backGroundEffects.Add(_sectionData.BackGroundId, _bgEffect);
		}
		for (int i = 0; i < _BGTexture.Length; i++)
		{
			_BGTexture[i].mainTexture = Toolbox.ResourcesManager.LoadObject(GetBackGroundPath(_sectionData.BackGroundId, i, isFetch: true)) as Texture;
		}
		foreach (ChapterExtraData extraChapter in _extraChapters)
		{
			Dictionary<int, Texture> bGTexture = extraChapter.BGTexture;
			int num = _sectionData.BackGroundId;
			if (extraChapter.IsUseOtherSectionBG())
			{
				num = extraChapter.BGSectionId;
				for (int j = 0; j < _BGTexture.Length; j++)
				{
					bGTexture.Add(j, Toolbox.ResourcesManager.LoadObject(GetBackGroundPath(num, j, isFetch: true)) as Texture);
				}
				if (num != _sectionData.BackGroundId && !_bgEffectControl._backGroundEffects.ContainsKey(num))
				{
					ParticleSystem particleSystem = InitParticle(num, extraChapter.AddTreeEffect);
					_bgEffectControl._backGroundEffects.Add(num, particleSystem);
					list.Add(particleSystem.gameObject);
				}
			}
			foreach (int item in extraChapter.ExtraTextureIndex)
			{
				if (!bGTexture.ContainsKey(item))
				{
					bGTexture.Add(item, Toolbox.ResourcesManager.LoadObject(GetExtraBackGroundPath(num, item, extraChapter.BGSuffix, isFetch: true)) as Texture);
				}
			}
			if (!string.IsNullOrEmpty(extraChapter.BGExtraEffectPath))
			{
				string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(extraChapter.BGExtraEffectPath, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true);
				extraChapter.ExtraEffect = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(assetTypePath) as GameObject);
				if (extraChapter.AttachExtraEffectToBgRoot)
				{
					extraChapter.ExtraEffect.transform.parent = _BGRoot.transform;
					extraChapter.ExtraEffect.transform.localPosition = Vector3.zero;
				}
				else
				{
					extraChapter.ExtraEffect.transform.parent = _areaSelectUI.transform;
				}
				extraChapter.ExtraEffect.SetActive(value: false);
				List<string> collection = GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(extraChapter.ExtraEffect, null);
				_loadedResources.AddRange(collection);
			}
			if (!string.IsNullOrEmpty(extraChapter.BGFirstClearEffectPath))
			{
				string assetTypePath2 = Toolbox.ResourcesManager.GetAssetTypePath(extraChapter.BGFirstClearEffectPath, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true);
				extraChapter.FirstClearEffect = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(assetTypePath2) as GameObject);
				extraChapter.FirstClearEffect.transform.parent = _areaSelectUI.transform;
				extraChapter.FirstClearEffect.SetActive(value: false);
				List<string> collection2 = GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(extraChapter.FirstClearEffect, null);
				_loadedResources.AddRange(collection2);
			}
		}
		List<string> collection3 = GameMgr.GetIns().GetEffectMgr().SetUIParticleShader(list, delegate
		{
			_isLoadEndParticle = true;
		});
		_loadedResources.AddRange(collection3);
		_isLoadEndBGTexture = true;
	}

	private ParticleSystem InitParticle(int bgId, bool addTreeEffect = false)
	{
		Vector3 vector = default(Vector3);
		ParticleSystem component = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(GetMapEffectPath(bgId, isFetch: true)) as GameObject).GetComponent<ParticleSystem>();
		vector = component.transform.localScale;
		component.transform.parent = _BGRoot.transform;
		component.transform.localScale = vector;
		component.transform.localPosition = Vector3.zero;
		if (addTreeEffect)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(GetTreeEffectPath(bgId, isFetch: true)) as GameObject);
			gameObject.transform.parent = component.transform;
			gameObject.transform.localScale = Vector3.one;
			gameObject.transform.localPosition = Vector3.zero;
		}
		return component;
	}

	public void OnChangeSelectChapter(StoryChapterData chapterData, bool isFirstClear)
	{
		TransitionChapterExtraData = null;
		if (_extraChapters.Count > 0)
		{
			int intChapterId = int.Parse(chapterData.ChapterId);
			ChapterExtraData = _extraChapters.FirstOrDefault((ChapterExtraData n) => n.ExtraTextureChapter == intChapterId);
		}
		if (_bgEffectControl != null)
		{
			_bgEffectControl.OnChangeSelectChapter(this, _sectionData, _sectionClassId, chapterData, isFirstClear);
		}
	}

	public void SetExtraTexture(int chapterId)
	{
		if (_extraChapters.Count == 0)
		{
			return;
		}
		ChapterExtraData chapterExtraData = _extraChapters.FirstOrDefault((ChapterExtraData n) => n.ExtraTextureChapter == chapterId || n.SectionId == 9003);
		if (chapterExtraData != null && chapterExtraData.IsChangeBG())
		{
			int num = _sectionData.BackGroundId;
			if (chapterExtraData.IsUseOtherSectionBG())
			{
				num = chapterExtraData.BGSectionId;
				for (int num2 = 0; num2 < _BGTexture.Length; num2++)
				{
					_BGTexture[num2].mainTexture = Toolbox.ResourcesManager.LoadObject(GetBackGroundPath(num, num2, isFetch: true)) as Texture;
				}
			}
			foreach (int item in chapterExtraData.ExtraTextureIndex)
			{
				_BGTexture[item].mainTexture = Toolbox.ResourcesManager.LoadObject(GetExtraBackGroundPath(num, item, chapterExtraData.BGSuffix, isFetch: true)) as Texture;
			}
			_isNormalBgSet = false;
		}
		else if (!_isNormalBgSet)
		{
			for (int num3 = 0; num3 < _BGTexture.Length; num3++)
			{
				_BGTexture[num3].mainTexture = Toolbox.ResourcesManager.LoadObject(GetBackGroundPath(_sectionData.BackGroundId, num3, isFetch: true)) as Texture;
			}
			_isNormalBgSet = true;
		}
	}

	public List<int> GetChaptersWithDifferentBackgroundFrom(int chapterId)
	{
		ChapterExtraData chapterExtra = _extraChapters.FirstOrDefault((ChapterExtraData n) => n.ExtraTextureChapter == chapterId);
		IEnumerable<ChapterExtraData> source = ((chapterExtra == null || !chapterExtra.IsUseOtherSectionBG()) ? _extraChapters.Where((ChapterExtraData c) => c.IsUseOtherSectionBG() && c.BGSectionId != _sectionData.BackGroundId) : _extraChapters.Where((ChapterExtraData c) => c.BGSectionId != chapterExtra.BGSectionId));
		return source.Select((ChapterExtraData s) => s.ExtraTextureChapter).ToList();
	}

	public void SetExtraEffect(int chapterId, bool isFirstClear = false)
	{
		if (_extraChapters.Count == 0)
		{
			return;
		}
		List<ChapterExtraData> list = new List<ChapterExtraData>();
		list = ((BeforeChapterId >= chapterId) ? _extraChapters.FindAll((ChapterExtraData n) => n.ExtraTextureChapter != 0 && BeforeChapterId > n.ExtraTextureChapter && n.ExtraTextureChapter >= chapterId) : _extraChapters.FindAll((ChapterExtraData n) => n.ExtraTextureChapter != 0 && BeforeChapterId <= n.ExtraTextureChapter && n.ExtraTextureChapter < chapterId));
		BeforeChapterId = chapterId;
		TransitionChapterExtraData = list.FirstOrDefault((ChapterExtraData n) => n.ExtraEffect != null);
		if (isFirstClear)
		{
			return;
		}
		if (list.Count() == 0 || TransitionChapterExtraData == null)
		{
			_changeChapterFirstCall = false;
			return;
		}
		TransitionChapterExtraData = list.FirstOrDefault((ChapterExtraData n) => n.ExtraEffect != null);
		if (!_changeChapterFirstCall && TransitionChapterExtraData != null && TransitionChapterExtraData.ExtraEffect != null)
		{
			TransitionChapterExtraData.ExtraEffect.SetActive(value: false);
			TransitionChapterExtraData.ExtraEffect.SetActive(value: true);
			GameMgr.GetIns().GetSoundMgr().PlaySe(TransitionChapterExtraData.SeType);
		}
		_changeChapterFirstCall = false;
	}

	public IEnumerator SetClearEffect()
	{
		ChapterExtraData clearChapterExtraData = _extraChapters.FirstOrDefault((ChapterExtraData n) => n.ExtraTextureChapter == BeforeChapterId);
		if (clearChapterExtraData != null && clearChapterExtraData.FirstClearEffect != null)
		{
			yield return new WaitForSeconds(clearChapterExtraData.FirstClearEffectDelayTime);
			clearChapterExtraData.FirstClearEffect.SetActive(value: false);
			clearChapterExtraData.FirstClearEffect.SetActive(value: true);
			GameMgr.GetIns().GetSoundMgr().PlaySe(clearChapterExtraData.FirstClearSeType);
		}
	}

	public void SetupEnd()
	{
		if (_bgEffectControl != null)
		{
			_bgEffectControl.SetupEnd();
		}
	}

	public void OnDragBG(GameObject obj, Vector2 delta)
	{
		_BGDragSecResetUpToTime = 0.5f;
		_BGDragSec += Time.deltaTime;
		_BGDragSec = Mathf.Min(_BGDragSec, 0.25f);
		float t = Mathf.Clamp(_BGDragSec / 0.25f, 0f, 1f);
		_BGDragDelta = Vector2.Lerp(Vector2.zero, delta, t);
	}

	public void UpdateBGDrag()
	{
		if (_BGDragSecResetUpToTime > 0f)
		{
			_BGDragSecResetUpToTime -= Time.deltaTime;
			if (_BGDragSecResetUpToTime < 0f)
			{
				_BGDragSecResetUpToTime = 0f;
				_BGDragSec = 0f;
			}
		}
		if (!Mathf.Approximately(_BGDragDelta.x, 0f) || !Mathf.Approximately(_BGDragDelta.y, 0f))
		{
			_BGDragDelta.x *= 0.875f;
			_BGDragDelta.y *= 0.875f;
			Vector3 localPosition = _BGRoot.transform.localPosition;
			localPosition.x += _BGDragDelta.x;
			localPosition.y += _BGDragDelta.y;
			UpdateMovableRange();
			localPosition.x = Mathf.Clamp(localPosition.x, _minMovablePos.x, _maxMovablePos.x);
			localPosition.y = Mathf.Clamp(localPosition.y, _minMovablePos.y, _maxMovablePos.y);
			_BGRoot.transform.localPosition = localPosition;
		}
	}

	public void SetBGDragEnable(bool enable)
	{
		_isBGDragEnable = enable;
		_BGDragCollision.SetActive(enable);
		_BGDragDelta = Vector2.zero;
	}

	private void UpdateMovableRange()
	{
		float num = (float)Screen.width / (float)Screen.height;
		Vector3 localScale = _BGRoot.transform.localScale;
		Vector3 localPosition = _BGRoot.transform.parent.transform.localPosition;
		if (!(localScale == _currentBGScale) || num != _currentAspectRatio || !(localPosition == _currentParentPos))
		{
			float num2 = UIManager.GetInstance().UIManagerRoot.manualHeight;
			if (1.7777778f > num)
			{
				num2 *= 1.7777778f / num;
			}
			float num3 = num2 * num * 0.5f;
			float num4 = num2 * 0.5f;
			float num5 = 2048f * localScale.x;
			float num6 = 2560f * localScale.x;
			float num7 = 1536f * localScale.y;
			float num8 = 1536f * localScale.y;
			_minMovablePos.x = 0f - num6 + num3 + 5f - localPosition.x;
			_maxMovablePos.x = num5 - num3 - 5f - localPosition.x;
			_minMovablePos.y = 0f - num7 + num4 + 5f - localPosition.y;
			_maxMovablePos.y = num8 - num4 - 5f - localPosition.y;
			_currentAspectRatio = num;
			_currentBGScale = localScale;
			_currentParentPos = localPosition;
		}
	}
}
