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

namespace Wizard;

public class TournamentRound : MonoBehaviour
{
	[SerializeField]
	private UILabel _nameLabel;

	[SerializeField]
	private UIScrollView _scrollView;

	[SerializeField]
	private GameObject _originalCell;

	[SerializeField]
	private GameObject _originalWatch;

	[NonSerialized]
	public List<TournamentCell> Cells;

	[NonSerialized]
	public List<TournamentWatch> Watchs;

	public TournamentRoundData Data { get; private set; }

	public UIScrollView ScrollView => _scrollView;

	public UIPanel ScrollBarPanel { private get; set; }

	public BoxCollider ScrollBarCollider { private get; set; }

	private void Start()
	{
		_originalCell.SetActive(value: false);
	}

	public void Setup(TournamentRoundData data, GatheringInfo info)
	{
		Data = data;
		_nameLabel.text = GetName(data);
		Cells = new List<TournamentCell>(data.Cells.Count);
		_originalCell.SetActive(value: true);
		foreach (TournamentCellData cell in data.Cells)
		{
			TournamentCell component = NGUITools.AddChild(_scrollView.gameObject, _originalCell).GetComponent<TournamentCell>();
			component.Setup(cell);
			Cells.Add(component);
		}
		_originalCell.SetActive(value: false);
		Watchs = new List<TournamentWatch>(data.Watchs.Count);
		_originalWatch.SetActive(value: true);
		foreach (TournamentWatchData watch in data.Watchs)
		{
			TournamentWatch component2 = NGUITools.AddChild(_scrollView.gameObject, _originalWatch).GetComponent<TournamentWatch>();
			component2.Setup(watch, Cells, info);
			Watchs.Add(component2);
		}
		_originalWatch.SetActive(value: false);
		SetCellEnable(isEnabled: false);
	}

	private string GetName(TournamentRoundData data)
	{
		return string.Format(Wizard.Data.SystemText.Get("Gathering_Tournament_0003"), data.RoundNo);
	}

	public void SetCellEnable(bool isEnabled)
	{
		foreach (TournamentCell cell in Cells)
		{
			cell.gameObject.SetActive(isEnabled);
		}
	}

	public void SetScrollBarEnable(bool isEnabled)
	{
		ScrollBarPanel.alpha = (isEnabled ? 1f : 0f);
		ScrollBarCollider.enabled = isEnabled;
	}

	public void SetWatchAlpha(float alpha)
	{
		if (Watchs == null)
		{
			return;
		}
		foreach (TournamentWatch watch in Watchs)
		{
			watch.SetAlpha(alpha);
		}
	}

	public void SetWatchEnable(bool isEnabled)
	{
		if (Watchs == null)
		{
			return;
		}
		foreach (TournamentWatch watch in Watchs)
		{
			watch.SetEnable(isEnabled);
		}
	}

	public void UpdateWatchPosition()
	{
		if (Watchs == null)
		{
			return;
		}
		foreach (TournamentWatch watch in Watchs)
		{
			watch.UpdatePosition();
		}
	}
}
