using Cute;
using UnityEngine;

namespace Wizard;

public class ChatLogPlateLayoutMember : ChatLogPlateLayoutBase
{
	private readonly Vector2 BASE_SIZE_WITHOUT_CONTENT_DISPLAY = new Vector2(100f, 29f);

	private readonly Vector2 MIN_SIZE_SPRITE_FRAME = new Vector2(48f, 39f);

	[SerializeField]
	private UISprite _spriteFrame;

	[SerializeField]
	private UITexture _textureEmblem;

	[SerializeField]
	private UILabel _labelUserName;

	[SerializeField]
	private UILabel _labelSendTime;

	[SerializeField]
	private ChatLogPartsNotice _noticeUI;

	public override Vector2 GetPlateSize(ChatMessageInfo messageInfo)
	{
		Vector2 result = GetContentSize(messageInfo) + BASE_SIZE_WITHOUT_CONTENT_DISPLAY;
		result.y = Mathf.Max(result.y, _textureEmblem.height);
		return result;
	}

	protected override void OnClearDisplay()
	{
		_spriteFrame.gameObject.SetActive(value: false);
		_textureEmblem.enabled = false;
		_labelUserName.gameObject.SetActive(value: false);
		_labelSendTime.gameObject.SetActive(value: false);
		_noticeUI.gameObject.SetActive(value: false);
	}

	protected override void OnSetDisplayBeforeLoad(ChatMessageInfo messageInfo)
	{
		SetUserDisplay(messageInfo);
	}

	protected override void OnSetDisplayAfterLoad(ChatMessageInfo messageInfo)
	{
		SetUserEmblem(messageInfo.UserInfo.EmblemId);
		SetDisplayFrame(messageInfo);
	}

	private void SetDisplayFrame(ChatMessageInfo messageInfo)
	{
		_spriteFrame.gameObject.SetActive(value: true);
		Vector2 contentSize = GetContentSize(messageInfo);
		_spriteFrame.width = Mathf.Max((int)MIN_SIZE_SPRITE_FRAME.x, (int)contentSize.x);
		_spriteFrame.height = Mathf.Max((int)MIN_SIZE_SPRITE_FRAME.y, (int)contentSize.y);
	}

	private void SetUserDisplay(ChatMessageInfo messageInfo)
	{
		_labelUserName.text = messageInfo.UserInfo.Name;
		_labelUserName.gameObject.SetActive(value: true);
		_labelSendTime.text = messageInfo.GetCreateTimeText();
		_labelSendTime.gameObject.SetActive(value: true);
	}

	private void SetUserEmblem(long emblemId)
	{
		string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(emblemId.ToString(), ResourcesManager.AssetLoadPathType.Emblem_M, isfetch: true);
		_textureEmblem.enabled = true;
		_textureEmblem.mainTexture = Toolbox.ResourcesManager.LoadObject(assetTypePath) as Texture;
	}
}
