using System;
using Cute;
using UnityEngine;

namespace Wizard;

public class GatheringJoiningCategorySelect : MonoBehaviour
{
	public enum Category
	{
		CHAT,
		INFO,
		MEMBER_LIST,
		INVITE,
		RANKING,
		TOURNAMENT
	}

	[SerializeField]
	private UIButton _chatButton;

	[SerializeField]
	private UIButton _infoButton;

	[SerializeField]
	private UIButton _memberListButton;

	[SerializeField]
	private UIButton _inviteButton;

	[SerializeField]
	private UIButton _rankingButton;

	[SerializeField]
	private GameObject _selectCursor;

	[SerializeField]
	private UISprite _bottomSprite;

	[SerializeField]
	private GameObject _bgSpriteInvite;

	[SerializeField]
	private GameObject _bgSpriteMemberList;

	[SerializeField]
	private GameObject _bgSpriteRanking;

	[SerializeField]
	private UILabel[] _categoryLabel;

	[SerializeField]
	private UIGrid _grid;

	public Action<Category> OnSelect;

	private bool _isTournament;

	private void Start()
	{
		_categoryLabel[0].color = LabelDefine.TEXT_COLOR_NORMAL;
		SetCursor(_chatButton);
		_chatButton.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			SetCursor(_chatButton);
			OnSelectCategory(Category.CHAT);
		}));
		_infoButton.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			SetCursor(_infoButton);
			OnSelectCategory(Category.INFO);
		}));
		_memberListButton.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			SetCursor(_memberListButton);
			OnSelectCategory(Category.MEMBER_LIST);
		}));
		_inviteButton.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			SetCursor(_inviteButton);
			OnSelectCategory(Category.INVITE);
		}));
		_rankingButton.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			SetCursor(_rankingButton);
			Category category = (_isTournament ? Category.TOURNAMENT : Category.RANKING);
			OnSelectCategory(category);
		}));
	}

	private void OnSelectCategory(Category category)
	{
		int num = (int)((category == Category.TOURNAMENT) ? Category.RANKING : category);
		for (int i = 0; i < _categoryLabel.Length; i++)
		{
			if (i == num)
			{
				_categoryLabel[i].color = LabelDefine.TEXT_COLOR_NORMAL;
			}
			else
			{
				_categoryLabel[i].color = LabelDefine.TEXT_COLOR_B0B0B0;
			}
		}
		OnSelect.Call(category);
	}

	private void SetCursor(UIButton parent)
	{
		_selectCursor.transform.parent = parent.transform;
		_selectCursor.transform.localPosition = Vector3.zero;
	}

	public void OnReceiveSelfInfo(GatheringInfo info)
	{
		if (info.State == GatheringInfo.eState.BEFORE_BATTLE)
		{
			_inviteButton.gameObject.SetActive(info.Role == GatheringRule.eRole.OWNER);
			_rankingButton.gameObject.SetActive(value: false);
		}
		else
		{
			_inviteButton.gameObject.SetActive(value: false);
			_rankingButton.gameObject.SetActive(value: true);
		}
		_grid.Reposition();
		GameObject gameObject = null;
		gameObject = ((info.Role != GatheringRule.eRole.OWNER) ? ((info.State == GatheringInfo.eState.BEFORE_BATTLE) ? _bgSpriteMemberList : _bgSpriteRanking) : ((info.State == GatheringInfo.eState.BEFORE_BATTLE) ? _bgSpriteInvite : _bgSpriteRanking));
		_bottomSprite.topAnchor.target = gameObject.transform;
		_bottomSprite.bottomAnchor.target = gameObject.transform;
		_bottomSprite.ResetAnchors();
		_bottomSprite.UpdateAnchors();
		if (info.Rule != null)
		{
			_isTournament = info.Rule.Type == GatheringRule.eType.TOURNAMENT;
		}
	}
}
