using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Wizard.RoomMatch;

namespace Wizard;

public class TournamentWatch : MonoBehaviour
{
	[SerializeField]
	private UIButton _watchButton;

	[SerializeField]
	private UIPanel _panel;

	[SerializeField]
	private BoxCollider _collider;

	private Transform _cellTransform0;

	private Transform _cellTransform1;

	private BattleParameter _battleParameter;

	public TournamentWatchData Data { get; private set; }

	public void Setup(TournamentWatchData data, List<TournamentCell> roundCells, GatheringInfo info)
	{
		Data = data;
		_cellTransform0 = roundCells.FirstOrDefault((TournamentCell c) => c.Data.ViewerId == data.ViewerId0).transform;
		_cellTransform1 = roundCells.FirstOrDefault((TournamentCell c) => c.Data.ViewerId == data.ViewerId1).transform;
		_watchButton.onClick.Add(new EventDelegate(Watch));
		_battleParameter = info.Rule.BattleParameterInstance;
	}

	public void SetEnable(bool isEnabled)
	{
		_collider.enabled = isEnabled;
	}

	public void SetAlpha(float alpha)
	{
		_panel.alpha = alpha;
	}

	public void UpdatePosition()
	{
		base.transform.localPosition = (_cellTransform0.localPosition + _cellTransform1.localPosition) * 0.5f;
	}

	public void Watch()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		RoomConnectController.InitializeParameter initializeParameter = new RoomConnectController.InitializeParameter(RoomConnectController.PositionMode.WATCHER, _battleParameter, Data.RoomId.ToString());
		initializeParameter.IsGathering = true;
		UIManager.GetInstance().StartCoroutine(GatheringUtility.JoinRoom(initializeParameter, ""));
	}
}
