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

public class CardMake : MonoBehaviour
{
	public const int CAN_CREATE_MAX = 3;

	public const string FORMAT_CARD_CRAFT_PARAM = "{0},{1}";

	private IDictionary<string, string> DestructDict;

	private IDictionary<string, string> _craftDict;

	public Action OnCardSell;

	public Action<int> OnCardSellId;

	public Action OnCardBuy;

	public Action OnClose;

	private Action _onFinishCardDestruct;

	public void StartCardDestruct(int cardId)
	{
		if (DestructDict == null)
		{
			DestructDict = new Dictionary<string, string>();
		}
		else
		{
			DestructDict.Clear();
		}
		int possessionCardNum = GameMgr.GetIns().GetDataMgr().GetPossessionCardNum(cardId, isIncludingSpotCard: false);
		DestructDict = new Dictionary<string, string>();
		DestructDict.Add(cardId.ToString(), "1," + possessionCardNum);
		DestructCard();
	}

	private void DestructCard()
	{
		CardDestructTask cardDestructTask = GameMgr.GetIns().GetCardDestructTask();
		cardDestructTask.SetParameter(DestructDict);
		StartCoroutine(Toolbox.NetworkManager.Connect(cardDestructTask, OnRequestFinishDestruct, OnError, OnError));
	}

	private void OnRequestFinishDestruct(NetworkTask.ResultCode error)
	{
		TriggerUpdateUserDeck();
		if (_onFinishCardDestruct != null)
		{
			_onFinishCardDestruct();
		}
	}

	private void TriggerUpdateUserDeck()
	{
		List<string> list = new List<string>(DestructDict.Keys);
		for (int i = 0; i < list.Count; i++)
		{
			int obj = int.Parse(list[i]);
			if (OnCardSellId != null)
			{
				OnCardSellId(obj);
			}
		}
		if (OnCardSell != null)
		{
			OnCardSell();
		}
		if (OnClose != null)
		{
			OnClose();
		}
		list = null;
	}

	public void StartCardCraft(int cardId)
	{
		if (_craftDict == null)
		{
			_craftDict = new Dictionary<string, string>();
		}
		else
		{
			_craftDict.Clear();
		}
		int possessionCardNum = GameMgr.GetIns().GetDataMgr().GetPossessionCardNum(cardId, isIncludingSpotCard: false);
		_craftDict.Add(cardId.ToString(), $"{1},{possessionCardNum}");
		CraftCard();
	}

	private void CraftCard()
	{
		CardCreateTask cardCreateTask = GameMgr.GetIns().GetCardCreateTask();
		cardCreateTask.SetParameter(_craftDict);
		StartCoroutine(Toolbox.NetworkManager.Connect(cardCreateTask, OnRequestFinishCraft, OnError, OnError));
	}

	private void OnRequestFinishCraft(NetworkTask.ResultCode error)
	{
		if (OnCardBuy != null)
		{
			OnCardBuy();
		}
		if (OnClose != null)
		{
			OnClose();
		}
	}

	public void StartDestructAll(IDictionary<string, string> destructDict, Action onFinishCallBack = null)
	{
		_onFinishCardDestruct = onFinishCallBack;
		DestructDict = destructDict;
		if (DestructDict.Count > 0)
		{
			DestructCard();
		}
	}

	public void StartDestructAll(IDictionary<int, int> destructDict, Action callback = null)
	{
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		foreach (KeyValuePair<int, int> item in destructDict)
		{
			string value = CreateRequestParam(item.Key, item.Value);
			dictionary.Add(item.Key.ToString(), value);
		}
		StartDestructAll(dictionary, callback);
	}

	public void StartCraftAll(IDictionary<int, int> craftDict)
	{
		if (_craftDict == null)
		{
			_craftDict = new Dictionary<string, string>();
		}
		else
		{
			_craftDict.Clear();
		}
		foreach (KeyValuePair<int, int> item in craftDict)
		{
			if (item.Value > 0)
			{
				string value = CreateRequestParam(item.Key, item.Value);
				_craftDict.Add(item.Key.ToString(), value);
			}
		}
		if (_craftDict.Count > 0)
		{
			CraftCard();
		}
	}

	private string CreateRequestParam(int cardId, int num)
	{
		int possessionCardNum = GameMgr.GetIns().GetDataMgr().GetPossessionCardNum(cardId, isIncludingSpotCard: false);
		return $"{num},{possessionCardNum}";
	}

	private void OnError(NetworkTask.ResultCode code)
	{
		OnClose.Call();
	}

	private void OnError(int code)
	{
		OnClose.Call();
	}
}
