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

namespace Wizard;

public class GatheringChat : MonoBehaviour
{
	[SerializeField]
	private GameObject _rootObj;

	[SerializeField]
	private Chat _chatPrefab;

	private Chat _chat;

	[SerializeField]
	private GatheringChatSettings _chatSettings;

	[SerializeField]
	private GatheringChatDeckList _gatheringDeckListUI;

	[SerializeField]
	private GatheringChatAutoJoinRoomMatch _autoJoinRoomMatchUI;

	[SerializeField]
	private ChatSendDeckUI _sendDeckUI;

	[SerializeField]
	private ChatSendReplayUI _sendReplayUI;

	[SerializeField]
	private GatheringChatSendRoomMatchUI _sendRoomMatchUI;

	[SerializeField]
	private ChatShareDeckUI _shareDeckUI;

	[SerializeField]
	private GatheringChatInterruptOrLeave _interruptOrLeaveUI;

	[SerializeField]
	private NotificatonAnimation _notificationAnimationPrefab;

	[SerializeField]
	private GameObject _notificationParent;

	private DialogBase _confirmStateChangeDialog;

	public void OpenCategory()
	{
		ClearActionUI();
		List<IChatActionUI> chatActionUIList = new List<IChatActionUI> { _gatheringDeckListUI, _autoJoinRoomMatchUI, _sendDeckUI, _sendReplayUI, _sendRoomMatchUI, _shareDeckUI, _interruptOrLeaveUI };
		GatheringGetSelfInfoTask task = new GatheringGetSelfInfoTask(isDependGatheringInfo: true);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			_chatSettings.SetGatheringInfo(task.Info, task.EntrySetting);
			_chat = NGUITools.AddChild(_rootObj, _chatPrefab.gameObject).GetComponent<Chat>();
			_chat.OnAddNewChatMessage = OnAddNewChatMessage;
			_chat.OnMissionCleared = ShowMissionClearedNotifications;
			_chat.gameObject.transform.localPosition = new Vector3(2000f, 0f, 0f);
			_chat.Init(_chatSettings, chatActionUIList, task.ChatStampList, NetworkDefine.MAINTENANCE_TYPE.CHAT_GATHERING, delegate(ChatInfo chatInfo)
			{
				ChangeActionUI(_chatSettings.GatheringInfo, chatInfo);
				_chat.gameObject.transform.localPosition = Vector3.zero;
				bool b = _chatSettings.GatheringInfo.Rule.BattleParameterInstance.DeckFormat == Format.Avatar;
				UIManager.SetObjectToGrey(_shareDeckUI.gameObject, b);
				UIManager.SetObjectToGrey(_sendDeckUI.gameObject, b);
			});
		}));
	}

	public void ReadyCloseCategory()
	{
		_chat.ReadyClose();
	}

	public void CloseCategory()
	{
		_chat.ExecuteClose();
	}

	private void ChangeActionUI(GatheringInfo gatheringInfo, ChatInfo chatInfo)
	{
		ClearActionUI();
		bool canBattleJoin = gatheringInfo.CanBattleJoin;
		switch (gatheringInfo.State)
		{
		case GatheringInfo.eState.BEFORE_BATTLE:
			_gatheringDeckListUI.gameObject.SetActive(canBattleJoin);
			break;
		case GatheringInfo.eState.ACTIVE_BATTLE:
			_gatheringDeckListUI.gameObject.SetActive(canBattleJoin);
			if (!gatheringInfo.Rule.IsTournament)
			{
				_sendRoomMatchUI.gameObject.SetActive(canBattleJoin);
			}
			UpdateActiveAutoJoinRoomMatchUI(chatInfo);
			break;
		case GatheringInfo.eState.AFTER_BATTLE:
			_sendDeckUI.gameObject.SetActive(value: true);
			_sendReplayUI.gameObject.SetActive(value: true);
			_shareDeckUI.gameObject.SetActive(value: true);
			_interruptOrLeaveUI.gameObject.SetActive(value: true);
			break;
		}
	}

	private void ClearActionUI()
	{
		_gatheringDeckListUI.gameObject.SetActive(value: false);
		_autoJoinRoomMatchUI.gameObject.SetActive(value: false);
		_sendRoomMatchUI.gameObject.SetActive(value: false);
		_sendDeckUI.gameObject.SetActive(value: false);
		_sendReplayUI.gameObject.SetActive(value: false);
		_shareDeckUI.gameObject.SetActive(value: false);
		_interruptOrLeaveUI.gameObject.SetActive(value: false);
	}

	private void OnAddNewChatMessage(ChatInfo chatInfo)
	{
		UpdateActiveAutoJoinRoomMatchUI(chatInfo);
		UpdateStateByChatMessage(chatInfo.MessageList);
	}

	private void UpdateActiveAutoJoinRoomMatchUI(ChatInfo chatInfo)
	{
		if (_chatSettings.GatheringInfo.State != GatheringInfo.eState.ACTIVE_BATTLE)
		{
			_autoJoinRoomMatchUI.gameObject.SetActive(value: false);
			return;
		}
		if (!_chatSettings.GatheringInfo.CanBattleJoin)
		{
			_autoJoinRoomMatchUI.gameObject.SetActive(value: false);
			return;
		}
		_autoJoinRoomMatchUI.gameObject.SetActive(value: true);
		if (_chatSettings.GatheringInfo.Rule.Type == GatheringRule.eType.TOURNAMENT)
		{
			_autoJoinRoomMatchUI.SetViewTournamentRoomMatch(chatInfo.GatheringMatchedRoom);
		}
	}

	private void UpdateStateByChatMessage(List<ChatMessageInfo> chatMessageList)
	{
		GatheringInfo.eState state = _chatSettings.GatheringInfo.State;
		GatheringInfo.eState eState = state;
		if (state == GatheringInfo.eState.AFTER_BATTLE)
		{
			return;
		}
		using (List<ChatMessageInfo>.Enumerator enumerator = chatMessageList.GetEnumerator())
		{
			while (enumerator.MoveNext())
			{
				switch (enumerator.Current.MessageType)
				{
				case ChatMessageInfo.eMessageType.GATHERING_BATTLE_END:
					eState = GatheringInfo.eState.AFTER_BATTLE;
					goto end_IL_003e;
				case ChatMessageInfo.eMessageType.GATHERING_BATTLE_START:
					eState = GatheringInfo.eState.ACTIVE_BATTLE;
					break;
				}
				continue;
				end_IL_003e:
				break;
			}
		}
		if (eState != state)
		{
			CreateConfirmStateChangeDialog(eState);
		}
	}

	private void ChangeViewSceneGoToGatheringActionMenu()
	{
		UIManager.ChangeViewSceneParam changeViewSceneParam = new UIManager.ChangeViewSceneParam();
		changeViewSceneParam.MyPageMenuIndex = 3;
		changeViewSceneParam.OnFinishChangeView = delegate
		{
			MyPageMenu.Instance.GoToGatheringActionMenu();
		};
		UIManager.GetInstance().ChangeViewScene(UIManager.ViewScene.MyPage, changeViewSceneParam);
	}

	private void CreateConfirmStateChangeDialog(GatheringInfo.eState state)
	{
		if (state != GatheringInfo.eState.BEFORE_BATTLE)
		{
			if (_confirmStateChangeDialog != null)
			{
				_confirmStateChangeDialog.OnCloseStart = null;
				_confirmStateChangeDialog.Close();
			}
			_confirmStateChangeDialog = UIManager.GetInstance().CreateDialogClose();
			string text = string.Empty;
			switch (state)
			{
			case GatheringInfo.eState.ACTIVE_BATTLE:
				text = Data.SystemText.Get("Gathering_Chat_0009");
				break;
			case GatheringInfo.eState.AFTER_BATTLE:
				text = Data.SystemText.Get("Gathering_Chat_0010");
				break;
			}
			_confirmStateChangeDialog.SetText(text);
			_confirmStateChangeDialog.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
			_confirmStateChangeDialog.OnCloseStart = delegate
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_CANCEL_TRANS);
				ChangeViewSceneGoToGatheringActionMenu();
				UIManager.GetInstance().dialogAllClear();
			};
			_confirmStateChangeDialog.SetPanelDepth(4000);
		}
	}

	public static bool IsMaintenance(List<NetworkDefine.MAINTENANCE_TYPE> maintenanceCodeList, GatheringRule rule)
	{
		if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_ALL_MAINTENANCE) || maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_BATTLE_MAINTENANCE))
		{
			return true;
		}
		switch (rule.BattleParameterInstance.DeckFormat)
		{
		case Format.Rotation:
			if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_ROTATION))
			{
				return true;
			}
			break;
		case Format.Unlimited:
			if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_UNLIMITED))
			{
				return true;
			}
			break;
		case Format.PreRotation:
			if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_PRE_ROTATION))
			{
				return true;
			}
			break;
		}
		switch (rule.BattleParameterInstance.Rule)
		{
		case RoomConnectController.BattleRule.Bo1:
			if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_RULE_BO1))
			{
				return true;
			}
			break;
		case RoomConnectController.BattleRule.Bo3:
			if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_RULE_BO3))
			{
				return true;
			}
			break;
		case RoomConnectController.BattleRule.Bo5:
			if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_RULE_BO5))
			{
				return true;
			}
			break;
		case RoomConnectController.BattleRule.Bo3Ban:
			if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_RULE_BO3_BAN1))
			{
				return true;
			}
			break;
		case RoomConnectController.BattleRule.Bo5Ban:
			if (maintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_RULE_BO5_BAN1))
			{
				return true;
			}
			break;
		}
		return false;
	}

	public static void ResetLatestReadChatMessageId()
	{
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.READ_LATEST_GATHERING_CHAT_MESSAGE_ID, -1);
	}

	private void ShowMissionClearedNotifications(List<string> messages)
	{
		List<NotificatonAnimation.Param> list = new List<NotificatonAnimation.Param>();
		for (int i = 0; i < messages.Count; i++)
		{
			list.Add(new NotificatonAnimation.Param(NotificatonAnimation.Param.Type.MissionCleared, messages[i]));
		}
		NotificatonAnimation component = NGUITools.AddChild(_notificationParent, _notificationAnimationPrefab.gameObject).GetComponent<NotificatonAnimation>();
		StartCoroutine(component.Exec(list));
	}
}
