using System;
using System.Collections.Generic;
using System.Linq;
using Wizard.Battle.View.Vfx;

namespace Wizard.Battle.Mulligan;

public abstract class MulliganCtrl
{
	public const int MULLIGAN_CARD_MAX = 3;

	public const int MULLIGAN_FIRST_EXCHANGE_MAX = 6;

	public const int MULLIGAN_CHANGED_NUM_NULL = -1;

	protected BattlePlayerBase _battlePlayer;

	protected MulliganViewBase _mulliganView;

	protected List<BattleCardBase> _firstDrawList = new List<BattleCardBase>(3);

	protected List<BattleCardBase> _stockList = new List<BattleCardBase>(3);

	protected List<int> _mulliganAfterCardIndexList;

	protected int _mulliganChangedNum = -1;

	public List<int> DealIdxList = new List<int>();

	public MulliganCtrl(BattlePlayerBase player)
	{
		_battlePlayer = player;
	}

	public abstract VfxBase StartMulliganVfx(SkillProcessor skillProcessor);

	public abstract VfxBase SubmitMulliganVfx(IList<BattleCardBase> abandonCards);

	protected VfxBase _MulliganCardChange(IList<BattleCardBase> AbandonCards)
	{
		SequentialVfxPlayer sequentialVfxPlayer = SequentialVfxPlayer.Create();
		if (AbandonCards.Count > 0)
		{
			IDictionary<int, BattleCardBase> newList = _MoveNewCardToHand(AbandonCards);
			_ReturnAbandonToDeck(AbandonCards);
			sequentialVfxPlayer.Register(_MulliganSwap(newList, AbandonCards));
		}
		return sequentialVfxPlayer;
	}

	protected void _ReturnAbandonToDeck(IList<BattleCardBase> AbandonCards)
	{
		List<BattleCardBase> list = AbandonCards.Where((BattleCardBase c) => c != null).ToList();
		for (int num = 0; num < list.Count(); num++)
		{
			GetBattlePlayer().AddToDeck(list[num]);
		}
	}

	protected virtual IDictionary<int, BattleCardBase> _MoveNewCardToHand(IList<BattleCardBase> AbandonCards)
	{
		List<BattleCardBase> list = _stockList.Take(AbandonCards.Count).ToList();
		_stockList.RemoveRange(0, AbandonCards.Count);
		BattlePlayerBase player = GetBattlePlayer();
		for (int i = 0; i < AbandonCards.Count; i++)
		{
			player.DeckCardList.Remove(list[i]);
			int index = player.HandCardList.IndexOf(AbandonCards[i]);
			player.HandCardList[index] = list[i];
		}
		return list.ToDictionary((BattleCardBase card) => player.HandCardList.IndexOf(card));
	}

	protected IDictionary<int, BattleCardBase> NetworkMoveNewCardToHand(IList<BattleCardBase> AbandonCards)
	{
		BattlePlayerBase player = GetBattlePlayer();
		List<BattleCardBase> list = new List<BattleCardBase>();
		for (int i = 0; i < _mulliganAfterCardIndexList.Count; i++)
		{
			int num = _mulliganAfterCardIndexList[i];
			if (!DealIdxList.Contains(num))
			{
				list.Add(BattleManagerBase.GetIns().GetBattleCardIdx(player.DeckCardList, num));
			}
		}
		if (AbandonCards.Count != list.Count)
		{
			string text = "";
			for (int j = 0; j < AbandonCards.Count; j++)
			{
				if (j > 0)
				{
					text += ",";
				}
				text += AbandonCards[j].Index;
			}
			string text2 = "";
			for (int k = 0; k < list.Count; k++)
			{
				if (k > 0)
				{
					text2 += ",";
				}
				text2 += list[k].Index;
			}
			throw new Exception($"Card swap failed：AbandonCards【{text}】/DrawCards【{text2}】");
		}
		SortedList<int, BattleCardBase> sortedList = new SortedList<int, BattleCardBase>();
		for (int l = 0; l < AbandonCards.Count; l++)
		{
			int key = DealIdxList.IndexOf(AbandonCards[l].Index);
			sortedList.Add(key, AbandonCards[l]);
		}
		IList<int> keys = sortedList.Keys;
		for (int m = 0; m < keys.Count; m++)
		{
			int key2 = keys[m];
			player.DeckCardList.Remove(list[m]);
			int index = player.HandCardList.IndexOf(sortedList[key2]);
			player.HandCardList[index] = list[m];
		}
		return list.ToDictionary((BattleCardBase card) => player.HandCardList.IndexOf(card));
	}

	protected abstract VfxBase _MulliganSwap(IDictionary<int, BattleCardBase> newList, IList<BattleCardBase> oldList);

	protected VfxBase _CardSwapAndMoveToStaticPositionVfx(IDictionary<int, BattleCardBase> newList, IList<BattleCardBase> oldList, bool isCardHolderPlayer, bool isHideMulliganTitle)
	{
		List<int> list = newList.Keys.ToList();
		List<BattleCardBase> list2 = newList.Values.ToList();
		SequentialVfxPlayer sequentialVfxPlayer = SequentialVfxPlayer.Create();
		sequentialVfxPlayer.Register(InstantVfx.Create(delegate
		{
			_mulliganView.HideMulliganUIAbandonZone();
			if (isHideMulliganTitle)
			{
				_mulliganView.HideMulliganTitle();
			}
		}));
		sequentialVfxPlayer.Register(new PlayerMulliganSwapVfx(list2, list, oldList, isCardHolderPlayer));
		int count = newList.Count;
		ParallelVfxPlayer parallelVfxPlayer = ParallelVfxPlayer.Create();
		for (int num = 0; num < count; num++)
		{
			BattleCardBase card = list2[num];
			int posIndex = list[num];
			parallelVfxPlayer.Register(_mulliganView.MoveCardToStaticPosition(card, posIndex, isAbandon: false));
		}
		parallelVfxPlayer.Register(InstantVfx.Create(delegate
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_HAND_MOVE_RIGHT);
		}));
		sequentialVfxPlayer.Register(parallelVfxPlayer);
		return sequentialVfxPlayer;
	}

	public void GetAbandonCardList(BattleManagerBase battleMgr, ref List<BattleCardBase> retCardList, ref List<int> retPosList)
	{
		IEnumerable<int> source = DealIdxList.Where((int index) => !_mulliganAfterCardIndexList.Contains(index));
		retPosList = source.Select((int index) => DealIdxList.IndexOf(index)).ToList();
		retCardList = source.Select((int index) => battleMgr.GetBattleCardIdx(GetBattlePlayer().AllCards.ToList(), index)).ToList();
	}

	public SequentialVfxPlayer MoveCardToStaticPosition(BattleCardBase card, int posIndex, bool isAbandon)
	{
		return _mulliganView.MoveCardToStaticPosition(card, posIndex, isAbandon);
	}

	public VfxBase MoveMulliganUIOutWhenSubmitMulligan()
	{
		return _mulliganView.MoveMulliganUIOutWhenSubmitMulligan();
	}

	public VfxBase DrawFirstMulliganCard()
	{
		SkillProcessor skillProcessor = new SkillProcessor();
		return GetBattlePlayer().DrawCards(_firstDrawList, skillProcessor, isOpen: false, isMulligan: true).Vfx;
	}

	protected List<int> _LotMulliganCardIndex(int maxNum)
	{
		List<int> list = new List<int>(6);
		List<int> list2 = Enumerable.Range(1, maxNum).ToList();
		if (BattleManagerBase.IsRandomDraw || GameMgr.GetIns().IsNetworkBattle)
		{
			for (int i = 0; i < 6; i++)
			{
				int index = BattleManagerBase.GetIns().StableRandom(list2.Count);
				list.Add(list2[index]);
				list2.Remove(list2[index]);
			}
		}
		else
		{
			for (int j = 0; j < 6; j++)
			{
				list.Add(list2[j]);
			}
		}
		return list;
	}

	protected void _CreateMulliganCardList(List<int> indexList)
	{
		List<BattleCardBase> deckCardList = GetBattlePlayer().DeckCardList;
		for (int i = 0; i < 3; i++)
		{
			BattleCardBase battleCardIdx = BattleManagerBase.GetIns().GetBattleCardIdx(deckCardList, indexList[i]);
			BattleCardBase battleCardIdx2 = BattleManagerBase.GetIns().GetBattleCardIdx(deckCardList, indexList[i + 3]);
			_firstDrawList.Add(battleCardIdx);
			_stockList.Add(battleCardIdx2);
		}
	}

	public void CreateMulliganDealList(List<int> indexList)
	{
		List<BattleCardBase> deckCardList = GetBattlePlayer().DeckCardList;
		for (int i = 0; i < 3; i++)
		{
			BattleCardBase battleCardIdx = BattleManagerBase.GetIns().GetBattleCardIdx(deckCardList, indexList[i]);
			_firstDrawList.Add(battleCardIdx);
		}
	}

	public List<BattleCardBase> GetFirstDrawList()
	{
		return _firstDrawList;
	}

	public List<BattleCardBase> GetStockList()
	{
		return _stockList;
	}

	public List<int> GetMulliganAfterCardIndexList()
	{
		return _mulliganAfterCardIndexList;
	}

	public void SetMulliganAfterCardIndexList(List<int> indexList)
	{
		_mulliganAfterCardIndexList = indexList;
	}

	public int GetChangedNum()
	{
		return _mulliganChangedNum;
	}

	public BattlePlayerBase GetBattlePlayer()
	{
		return _battlePlayer;
	}

	public MulliganInfoControl GetMulliganInfo()
	{
		return _mulliganView.MulliganInfo;
	}
}
