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

namespace Wizard;

public class ChatLogPlate : UIBase
{
	private const int MARGIN_UNREAD_MESSAGE = 26;

	[SerializeField]
	private Transform _layoutRoot;

	[SerializeField]
	private ChatLogPlateLayoutMember _layoutOwn;

	[SerializeField]
	private ChatLogPlateLayoutMember _layoutOtherMember;

	[SerializeField]
	private ChatLogPlateLayoutNoMember _layoutNoMember;

	[SerializeField]
	private GameObject _unreadMessageUI;

	private ChatMessageInfo _messageInfo;

	private Func<List<string>, bool> _funcIsLoadedResources;

	public int GetHeightSize(ChatMessageInfo messageInfo, bool isUnreadMessage)
	{
		int num = (int)GetDisplayByMessageInfo(messageInfo).GetPlateSize(messageInfo).y;
		if (isUnreadMessage)
		{
			num += 26;
		}
		return num;
	}

	public void Init(IChatSettings chatSettings, ChatConnectController chatConnectController, ChatLogUI.PartsForPlate partsForPlate, Func<List<string>, bool> funcIsLoadedResources)
	{
		_funcIsLoadedResources = funcIsLoadedResources;
		_layoutOwn.Init(chatSettings, chatConnectController, partsForPlate);
		_layoutOtherMember.Init(chatSettings, chatConnectController, partsForPlate);
		_layoutNoMember.Init(chatSettings, chatConnectController, partsForPlate);
	}

	public void SetData(ChatMessageInfo messageInfo, bool isUnreadMessage)
	{
		ClearDisplay();
		if (messageInfo != null)
		{
			_messageInfo = messageInfo;
			UpdateUnreadMessage(isUnreadMessage);
			UpdateDisplay();
		}
	}

	private void ClearDisplay()
	{
		_layoutRoot.localPosition = Vector3.zero;
		_unreadMessageUI.gameObject.SetActive(value: false);
		_layoutOwn.ClearDisplay();
		_layoutOwn.gameObject.SetActive(value: false);
		_layoutOtherMember.ClearDisplay();
		_layoutOtherMember.gameObject.SetActive(value: false);
		_layoutNoMember.ClearDisplay();
		_layoutNoMember.gameObject.SetActive(value: false);
	}

	private ChatLogPlateLayoutBase GetDisplayByMessageInfo(ChatMessageInfo info)
	{
		ChatLogPlateLayoutBase result = null;
		switch (info.TalkerType)
		{
		case ChatMessageInfo.eTalkerType.NO_MEMBER:
			result = _layoutNoMember;
			break;
		case ChatMessageInfo.eTalkerType.OTHER:
			result = _layoutOtherMember;
			break;
		case ChatMessageInfo.eTalkerType.OWN:
			result = _layoutOwn;
			break;
		}
		return result;
	}

	private void UpdateUnreadMessage(bool isUnreadMessage)
	{
		_unreadMessageUI.gameObject.SetActive(isUnreadMessage);
		if (isUnreadMessage)
		{
			_layoutRoot.localPosition = Vector3.down * 26f;
		}
		else
		{
			_layoutRoot.localPosition = Vector3.zero;
		}
	}

	private void UpdateDisplay()
	{
		ChatLogPlateLayoutBase logDisplay = GetDisplayByMessageInfo(_messageInfo);
		logDisplay.gameObject.SetActive(value: true);
		logDisplay.SetDisplayBeforeLoad(_messageInfo);
		StartCoroutine(CheckResouceLoaded(_messageInfo.MessageId, _messageInfo.GetResourcePathList(), delegate
		{
			logDisplay.SetDisplayAfterLoad(_messageInfo);
		}));
	}

	private IEnumerator CheckResouceLoaded(int messageId, List<string> checkResourceList, Action onLoadEndCallback)
	{
		while (true)
		{
			if (_messageInfo.MessageId != messageId)
			{
				yield break;
			}
			if (_funcIsLoadedResources.Call(checkResourceList))
			{
				break;
			}
			yield return null;
		}
		onLoadEndCallback.Call();
	}
}
