using System;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;

namespace Wizard;

public class ChatShareDeckUI : MonoBehaviour, IChatActionUI
{
	[SerializeField]
	private UIButton _btnDeckLogList;

	private IChatApiSettings _chatApiSettings;

	private ChatConnectController _chatConnectCtr;

	private ChatLogUI _chatLogUI;

	private DialogBase _dialogDeckList;

	private Dictionary<Format, List<ChatMessageInfo.DeckLogData>> _deckLogDataListFormatDict = new Dictionary<Format, List<ChatMessageInfo.DeckLogData>>();

	public void Init(IChatSettings chatSettings, ChatConnectController chatConnectCtr, ChatLogUI chatLogUI, Action actionAddNewChatLogAfterSendChat)
	{
		_chatApiSettings = chatSettings.ApiSettings;
		_chatConnectCtr = chatConnectCtr;
		_chatLogUI = chatLogUI;
		_btnDeckLogList.onClick.Clear();
		_btnDeckLogList.onClick.Add(new EventDelegate(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			ShowDeckLogList();
		}));
	}

	private void ShowDeckLogList()
	{
		ChatDeckLogTask task = new ChatDeckLogTask(_chatApiSettings.ApiChatDeckLog);
		_chatConnectCtr.StartConnectCommon(task, delegate
		{
			Format defaultFormat = Format.Rotation;
			if (task.DeckLogListRotation.Count <= 0)
			{
				if (task.DeckLogListUnlimited.Count > 0)
				{
					defaultFormat = Format.Unlimited;
				}
				else if (Prerelease.Status != Prerelease.eStatus.NONE && task.DeckLogListPreRotation.Count > 0)
				{
					defaultFormat = Format.PreRotation;
				}
				else if (Data.Crossover.IsWithinAnyPeriod && task.DeckLogListCrossover.Count > 0)
				{
					defaultFormat = Format.Crossover;
				}
				else if (Data.MyRotationAllInfo.IsMyRotationEnable && task.DeckLogListMyRotation != null && task.DeckLogListMyRotation.Count > 0)
				{
					defaultFormat = Format.MyRotation;
				}
			}
			UpdateDeckLogListAndCreateDialog(task.DeckLogListRotation, task.DeckLogListUnlimited, task.DeckLogListPreRotation, task.DeckLogListCrossover, task.DeckLogListMyRotation, defaultFormat);
		});
	}

	private void CreateDialogDeckList(Format format)
	{
		DeckData deckData = null;
		List<DeckGroup> list = new List<DeckGroup>();
		foreach (KeyValuePair<Format, List<ChatMessageInfo.DeckLogData>> item in _deckLogDataListFormatDict)
		{
			if (item.Value.Count <= 0)
			{
				continue;
			}
			List<DeckData> list2 = ChatMessageInfo.DeckLogData.ConvertDeckDataList(item.Value);
			if (deckData == null)
			{
				deckData = list2.FirstOrDefault((DeckData deck) => deck.IsUsable());
			}
			int num = list2.Count % 9;
			if (num > 0)
			{
				int num2 = 9 - num;
				for (int num3 = 0; num3 < num2; num3++)
				{
					list2.Add(new DeckData(item.Key));
				}
			}
			list.Add(new DeckGroup(list2, item.Key, DeckAttributeType.CustomDeck));
		}
		DeckGroupListData deckGroupListData = new DeckGroupListData(list);
		deckGroupListData.ForceVisiblePreRotation(Prerelease.Status != Prerelease.eStatus.NONE);
		DeckSelectUIDialog.eFormatChangeUIType formatChangeUIType = DeckSelectUIDialog.eFormatChangeUIType.NormalFormatOnly;
		if (Prerelease.Status != Prerelease.eStatus.NONE)
		{
			formatChangeUIType = DeckSelectUIDialog.eFormatChangeUIType.WithPrerotation;
		}
		else if (DeckListUtility.DeckGroupDataBaseClone().Any((DeckGroup deckgroup) => deckgroup.DeckFormat == Format.Crossover))
		{
			formatChangeUIType = DeckSelectUIDialog.eFormatChangeUIType.WithCrossover;
		}
		else if (Data.MyRotationAllInfo.IsMyRotationEnable)
		{
			formatChangeUIType = DeckSelectUIDialog.eFormatChangeUIType.WithMyRotation;
		}
		DeckSelectUI.InitOptions initOptions = new DeckSelectUI.InitOptions
		{
			PrimaryFirstDisplayDeck = deckData
		};
		if (CustomPreference.GetTextLanguage() == Global.LANG_TYPE.Jpn.ToString())
		{
			initOptions.LongTextTitlePosition = 34;
		}
		DeckSelectUIDialog deckSelectUIDialog = DeckSelectUIDialog.Create(Data.SystemText.Get("Guild_Chat_0013"), deckGroupListData, format, formatChangeUIType, isVisibleCreateNew: false, delegate(DialogBase dialogDeckList, DeckData deck)
		{
			ShowDeckViewFromDeckLog(deck);
		}, initOptions);
		_dialogDeckList = deckSelectUIDialog.Dialog;
	}

	private void ShowDeckViewFromDeckLog(DeckData deck)
	{
		ChatMessageInfo.DeckLogData deckLogDataByDeckId = ChatMessageInfo.DeckLogData.GetDeckLogDataByDeckId(_deckLogDataListFormatDict[deck.Format], deck.GetDeckID());
		_chatLogUI.ShowDeckViewByDeckLog(deckLogDataByDeckId, delegate
		{
			if (_dialogDeckList != null)
			{
				_dialogDeckList.SetDisp(inDisp: false);
				_dialogDeckList.SetActive(inActive: false);
			}
		}, delegate
		{
			if (_dialogDeckList != null)
			{
				_dialogDeckList.SetActive(inActive: true);
				_dialogDeckList.SetDisp(inDisp: true);
			}
		}, delegate(Format format, Dictionary<Format, List<ChatMessageInfo.DeckLogData>> dictDeckLogList)
		{
			if (_dialogDeckList != null)
			{
				_dialogDeckList.SetActive(inActive: true);
				_dialogDeckList.Close();
				_deckLogDataListFormatDict = dictDeckLogList;
				CreateDialogDeckList(format);
			}
		});
	}

	private void UpdateDeckLogListAndCreateDialog(List<ChatMessageInfo.DeckLogData> deckLogListRotation, List<ChatMessageInfo.DeckLogData> deckLogListUnlimited, List<ChatMessageInfo.DeckLogData> deckLogListPreRotation, List<ChatMessageInfo.DeckLogData> deckLogListCrossover, List<ChatMessageInfo.DeckLogData> deckLogListMyRotation, Format defaultFormat)
	{
		_deckLogDataListFormatDict[Format.Rotation] = deckLogListRotation;
		_deckLogDataListFormatDict[Format.Unlimited] = deckLogListUnlimited;
		_deckLogDataListFormatDict[Format.PreRotation] = deckLogListPreRotation;
		if (deckLogListCrossover != null)
		{
			_deckLogDataListFormatDict[Format.Crossover] = deckLogListCrossover;
		}
		if (deckLogListMyRotation != null)
		{
			_deckLogDataListFormatDict[Format.MyRotation] = deckLogListMyRotation;
		}
		CreateDialogDeckList(defaultFormat);
	}
}
