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

namespace Wizard;

public class StorySectionSummaryDialog : MonoBehaviour
{
	private const int FRAME_DEFAULT_HEIGHT = 87;

	private const int FRAME_OFFSET_HEIGHT_PER_LINE_COUNT = 28;

	private const int GRID_DEFAULT_CELL_HIEGHT = 28;

	private const string SUMMARY_TITLE_KEY_FORMAT = "story_section_summary_title_{0:D2}";

	private const string SUMMARY_TEXT_KEY_FORMAT = "story_section_summary_text_{0:D2}";

	private const string SUMMARTY_BG_TEXTURE_FORMAT = "bg_story_section_summary_{0:D2}";

	[SerializeField]
	private UITexture _textureBackGround;

	[SerializeField]
	private UISprite _spriteSummaryFrame;

	[SerializeField]
	private UISprite _spriteSummaryFramePanel;

	[SerializeField]
	private UILabel _labelSectionSummary;

	[SerializeField]
	private UIGrid _gridSummaryDetailLines;

	[SerializeField]
	private UISprite _spriteSummaryDetailLine;

	private string _bgTexturePath;

	private int SummaryDefaultFontSize => _labelSectionSummary.fontSize;

	public static void Create(int sectionId)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("UI/layoutParts/Dialog/StorySectionSummaryDialog")) as GameObject;
		dialogBase.SetObj(gameObject);
		gameObject.GetComponent<StorySectionSummaryDialog>().Initialize(dialogBase, sectionId);
	}

	private void Initialize(DialogBase dialog, int sectionId)
	{
		string storySectionTitleText = Data.Master.GetStorySectionTitleText($"story_section_summary_title_{sectionId:D2}");
		dialog.SetTitleLabel(storySectionTitleText);
		dialog.SetSize(DialogBase.Size.XL);
		string storySectionTitleText2 = Data.Master.GetStorySectionTitleText($"story_section_summary_text_{sectionId:D2}");
		SetSummaryText(storySectionTitleText2);
		SetFramePanelAtlas();
		UIManager.GetInstance().createInSceneCenterLoading();
		string bgTextureName = $"bg_story_section_summary_{sectionId:D2}";
		StartCoroutine(LoadBgTexture(bgTextureName, delegate
		{
			SetBackGroundTexture(bgTextureName);
			DialogBase dialogBase = dialog;
			dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
			{
				UnloadTexture();
			});
			UIManager.GetInstance().closeInSceneCenterLoading();
		}));
	}

	private IEnumerator LoadBgTexture(string bgTextureName, Action onFinish)
	{
		_bgTexturePath = Toolbox.ResourcesManager.GetAssetTypePath(bgTextureName, ResourcesManager.AssetLoadPathType.UiStorySectionSummary);
		yield return StartCoroutine(Toolbox.ResourcesManager.LoadAssetAsync(_bgTexturePath, null));
		onFinish.Call();
	}

	private void UnloadTexture()
	{
		Toolbox.ResourcesManager.RemoveAsset(_bgTexturePath);
		_bgTexturePath = null;
	}

	private void SetBackGroundTexture(string bgTextureName)
	{
		Texture mainTexture = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(bgTextureName, ResourcesManager.AssetLoadPathType.UiStorySectionSummary, isfetch: true)) as Texture;
		_textureBackGround.mainTexture = mainTexture;
	}

	private void SetFramePanelAtlas()
	{
		UIManager.GetInstance().AttachAtlas(_spriteSummaryFramePanel.gameObject, isTargetChildren: false);
	}

	private void SetSummaryText(string summaryText)
	{
		_labelSectionSummary.SetWrapText(summaryText);
		int textLineCount = Global.GetTextLineCount(_labelSectionSummary.text);
		int fontSizeDelta = SummaryDefaultFontSize - _labelSectionSummary.finalFontSize;
		SetSummaryLayout(textLineCount, fontSizeDelta);
	}

	private void SetSummaryLayout(int lineCount, int fontSizeDelta)
	{
		ResizeSummaryFrame(lineCount, fontSizeDelta);
		GenerateDetailLineGrid(lineCount, fontSizeDelta);
	}

	private void ResizeSummaryFrame(int lineCount, int fontSizeDelta)
	{
		int num = (28 - fontSizeDelta) * lineCount;
		_spriteSummaryFrame.height = 87 + num;
	}

	private void GenerateDetailLineGrid(int lineCount, int fontSizeDelta)
	{
		for (int i = 1; i < lineCount; i++)
		{
			NGUITools.AddChild(_gridSummaryDetailLines.gameObject, _spriteSummaryDetailLine.gameObject);
		}
		_gridSummaryDetailLines.cellHeight = 28 - fontSizeDelta;
		UIUtil.AddPositionY(_gridSummaryDetailLines.transform, fontSizeDelta);
		_gridSummaryDetailLines.Reposition();
	}
}
