using System.Collections.Generic;
using UnityEngine;

namespace Wizard;

public abstract class ChatLogPlateLayoutBase : MonoBehaviour
{
	protected ChatConnectController _chatConnectController;

	[SerializeField]
	private List<ChatLogContentBase> _listLogContent;

	private Dictionary<ChatMessageInfo.eMessageType, ChatLogContentBase> _contentList;

	public abstract Vector2 GetPlateSize(ChatMessageInfo messageInfo);

	protected abstract void OnClearDisplay();

	protected abstract void OnSetDisplayBeforeLoad(ChatMessageInfo messageInfo);

	protected abstract void OnSetDisplayAfterLoad(ChatMessageInfo messageInfo);

	public void Init(IChatSettings chatSettings, ChatConnectController chatConnectController, ChatLogUI.PartsForPlate partsForPlate)
	{
		_chatConnectController = chatConnectController;
		InitContentList(chatSettings, partsForPlate);
	}

	public void ClearDisplay()
	{
		ClearAllContentDisplay();
		OnClearDisplay();
	}

	public void SetDisplayBeforeLoad(ChatMessageInfo messageInfo)
	{
		OnSetDisplayBeforeLoad(messageInfo);
	}

	public void SetDisplayAfterLoad(ChatMessageInfo messageInfo)
	{
		SetContentDisplay(messageInfo);
		OnSetDisplayAfterLoad(messageInfo);
	}

	protected Vector2 GetContentSize(ChatMessageInfo messageInfo)
	{
		return GetContent(messageInfo.MessageType).GetSize(messageInfo);
	}

	private void SetContentDisplay(ChatMessageInfo messageInfo)
	{
		ChatLogContentBase content = GetContent(messageInfo.MessageType);
		content.gameObject.SetActive(value: true);
		content.SetData(messageInfo);
	}

	private void InitContentList(IChatSettings chatSettings, ChatLogUI.PartsForPlate partsForPlate)
	{
		_contentList = new Dictionary<ChatMessageInfo.eMessageType, ChatLogContentBase>();
		foreach (ChatLogContentBase item in _listLogContent)
		{
			foreach (ChatMessageInfo.eMessageType item2 in item.ListMessagetType)
			{
				_contentList[item2] = item;
			}
		}
		foreach (KeyValuePair<ChatMessageInfo.eMessageType, ChatLogContentBase> content in _contentList)
		{
			content.Value.Init(chatSettings, _chatConnectController, partsForPlate);
		}
	}

	private ChatLogContentBase GetContent(ChatMessageInfo.eMessageType messageType)
	{
		_contentList.TryGetValue(messageType, out var value);
		return value;
	}

	private void ClearAllContentDisplay()
	{
		foreach (KeyValuePair<ChatMessageInfo.eMessageType, ChatLogContentBase> content in _contentList)
		{
			ChatLogContentBase value = content.Value;
			if (value != null)
			{
				value.gameObject.SetActive(value: false);
			}
		}
	}
}
