using Cute;
using UnityEngine;

namespace Wizard;

public class TournamentCell : MonoBehaviour
{
	[SerializeField]
	private UITexture _emblemTexture;

	[SerializeField]
	private UILabel _nameLabel;

	[SerializeField]
	private GameObject _activePlate;

	[SerializeField]
	private GameObject _winPlate;

	[SerializeField]
	private GameObject _losePlate;

	[SerializeField]
	private GameObject _loseByDefaultPlate;

	[SerializeField]
	private GameObject _championPlate;

	[SerializeField]
	private GameObject _youMark;

	[SerializeField]
	private UISprite _lineDownSprite;

	[SerializeField]
	private UISprite _lineUpSprite;

	[SerializeField]
	private Transform _leftConnectorTransform;

	[SerializeField]
	private Transform _rightConnectorTransform;

	[SerializeField]
	private UILabel _isFinalMatchResetLabel;

	private Transform _lineEndTransform;

	private const string DEFAULT_LINE_SPRITE_NAME = "Line_tournament";

	private const string ACTIVE_LINE_SPRITE_NAME = "Line_tournament_active";

	private const string PRE_EXTRA_LINE_SPRITE_NAME = "Line_tournament_Final";

	private const string WIN_PRE_EXTRA_LINE_SPRITE_NAME = "Line_win_tournament_Final";

	private const string WIN_LINE_SPRITE_NAME = "Line_win_tournament";

	public TournamentCellData Data { get; private set; }

	public Transform LeftConnectorTransform => _leftConnectorTransform;

	public Transform RightConnectorTransform => _rightConnectorTransform;

	public void Setup(TournamentCellData data)
	{
		Data = data;
		_nameLabel.gameObject.SetActive(data.Name != string.Empty);
		_nameLabel.text = data.Name;
		_emblemTexture.gameObject.SetActive(data.EmblemId > 0);
		_emblemTexture.mainTexture = Toolbox.ResourcesManager.LoadObject(Data.GetEmblemPath()) as Texture;
		_isFinalMatchResetLabel.gameObject.SetActive(data.IsFinalMatchReset);
		SetupState();
		SetupLineVisible();
		SetYouMarkVisible(isVisible: false);
	}

	private void SetupState()
	{
		TournamentCellData.CellState state = Data.State;
		_activePlate.SetActive(state == TournamentCellData.CellState.Active);
		_winPlate.SetActive(state == TournamentCellData.CellState.Win);
		_losePlate.SetActive(state == TournamentCellData.CellState.Lose);
		_loseByDefaultPlate.SetActive(state == TournamentCellData.CellState.LoseByDefault);
		_championPlate.SetActive(Data.IsChampion);
		switch (state)
		{
		case TournamentCellData.CellState.Blank:
		case TournamentCellData.CellState.LoseByDefault:
			_emblemTexture.gameObject.SetActive(value: false);
			_nameLabel.gameObject.SetActive(value: false);
			break;
		case TournamentCellData.CellState.Lose:
		{
			UIManager.SetObjectToGrey(base.gameObject, b: true);
			UISprite lineSprite = GetLineSprite();
			if (lineSprite != null)
			{
				lineSprite.color = Color.white;
			}
			break;
		}
		}
	}

	private void SetupLineVisible()
	{
		UISprite lineSprite = GetLineSprite();
		_lineDownSprite.gameObject.SetActive(lineSprite == _lineDownSprite);
		_lineUpSprite.gameObject.SetActive(lineSprite == _lineUpSprite);
		if (!(lineSprite == null))
		{
			string spriteName = "Line_tournament";
			if (Data.State == TournamentCellData.CellState.Active)
			{
				spriteName = "Line_tournament_active";
			}
			else if (Data.IsPreExtra)
			{
				spriteName = ((Data.State == TournamentCellData.CellState.Win) ? "Line_win_tournament_Final" : "Line_tournament_Final");
			}
			else if (Data.State == TournamentCellData.CellState.Win)
			{
				spriteName = "Line_win_tournament";
			}
			lineSprite.spriteName = spriteName;
		}
	}

	private UISprite GetLineSprite()
	{
		if (Data.IsTerminal)
		{
			return null;
		}
		if (Data.Line != TournamentCellData.LineType.Down)
		{
			return _lineUpSprite;
		}
		return _lineDownSprite;
	}

	public void SetLineEndTransform(Transform lineEndTransform)
	{
		_lineEndTransform = lineEndTransform;
	}

	public void UpdateLine()
	{
		UISprite lineSprite = GetLineSprite();
		if (!(lineSprite == null))
		{
			Vector3 localPosition = _rightConnectorTransform.localPosition;
			Vector3 vector;
			if (Data.IsPreExtra)
			{
				vector = (localPosition + base.transform.InverseTransformPoint(_lineEndTransform.position)) * 0.5f;
				vector.x += 48f;
			}
			else
			{
				vector = base.transform.InverseTransformPoint(_lineEndTransform.position);
			}
			lineSprite.transform.localPosition = (localPosition + vector) * 0.5f;
			lineSprite.width = (int)(vector.x - localPosition.x);
			lineSprite.height = (int)(Mathf.Abs(vector.y - localPosition.y) + 14f);
		}
	}

	public void SetYouMarkVisible(bool isVisible)
	{
		_youMark.SetActive(isVisible);
		if (isVisible)
		{
			_youMark.GetComponent<UILabel>().color = LabelDefine.TEXT_COLOR_NORMAL;
		}
	}
}
