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

public class CardMake : MonoBehaviour
{

	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 = 0; // Pre-Phase-5b: headless has no user inventory
		DestructDict = new Dictionary<string, string>();
		DestructDict.Add(cardId.ToString(), "1," + possessionCardNum);
		DestructCard();
	}

	private void DestructCard()
	{
		// Pre-Phase-5b: fired CardDestructTask via NetworkManager. Headless has no wire
		// task service; short-circuit straight to the success callback so the UI event
		// chain (OnRequestFinishDestruct → TriggerUpdateUserDeck → OnClose) still fires.
		OnRequestFinishDestruct(default(NetworkTask.ResultCode));
	}

	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 = 0; // Pre-Phase-5b: headless has no user inventory
		_craftDict.Add(cardId.ToString(), $"{1},{possessionCardNum}");
		CraftCard();
	}

	private void CraftCard()
	{
		// Pre-Phase-5b: fired CardCreateTask via NetworkManager. See DestructCard.
		OnRequestFinishCraft(default(NetworkTask.ResultCode));
	}

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

	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 = 0; // Pre-Phase-5b: headless has no user inventory
		return $"{num},{possessionCardNum}";
	}
}
