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

public class PaymentPC : PaymentBase
{
	public List<string> ProductIdList;

	public List<string> IdList;

	public Dictionary<string, string> ProductPriceList;

	public Dictionary<string, string> FormatProductPriceList;

	public Dictionary<string, string> ProductNameList;

	public Dictionary<string, string> ProductTextList;

	public Dictionary<string, string> ProductPurchaseLimitList;

	public Dictionary<string, string> ProductPurchaseNumberList;

	public Dictionary<string, string> ProductCsvIdList;

	public Dictionary<string, string> ProductImageNameList;

	public Dictionary<string, bool> ProductIsSpecialShop;

	public Dictionary<string, int> ProductCurrentPurchaseCount;

	public Dictionary<string, int> ProductPurchaseLimitCount;

	public Dictionary<string, string> ProductEndTime;

	public string selectedStoreProductId;

	private static PaymentPC instance;

	private bool isCountTime;

	private float timer;

	protected Callback<MicroTxnAuthorizationResponse_t> m_MicroTxnAuthorizationResponse;

	public PaymentUI paymentUI { get; private set; }

	public event Action ProductListSucceeded;

	public event Action ProductListFailed;

	public event Action<string> FinishFailureEvent;

	public event Action<NetworkTask.ResultCode> purchaseFinishSuccessEvent;

	public event Action<NetworkTask.ResultCode> purchaseFinishHttpErrorEvent;

	public event Action<int> purchaseFinishResultCodeErrorEvent;

	public event Action purchaseRetryResultEvent;

	public event Action ConsumePurchaseSucceeded;

	private void Awake()
	{
		if (paymentUI == null)
		{
			paymentUI = new PaymentUI();
		}
		m_MicroTxnAuthorizationResponse = Callback<MicroTxnAuthorizationResponse_t>.Create(OnMicroTxnAuthorizationResponse);
	}

	private void OnMicroTxnAuthorizationResponse(MicroTxnAuthorizationResponse_t pCallback)
	{
		if (!Convert.ToBoolean(pCallback.m_bAuthorized))
		{
			return;
		}
		PaymentPCFinishTask paymentPCFinishTask = new PaymentPCFinishTask();
		paymentPCFinishTask.SetParameter(selectedStoreProductId, pCallback.m_unAppID.ToString(), pCallback.m_ulOrderID.ToString());
		StartCoroutine(Toolbox.NetworkManager.Connect(paymentPCFinishTask, delegate
		{
			string value = "";
			ProductNameList.TryGetValue(selectedStoreProductId, out value);
			if (!ProductIsSpecialShop[selectedStoreProductId])
			{
				paymentUI.PurchaseFinished(value);
			}
			if (this.ConsumePurchaseSucceeded != null)
			{
				this.ConsumePurchaseSucceeded();
			}
		}, paymentUI.PurchaseFailedPC));
	}

	private void Update()
	{
		if (isCountTime)
		{
			checkTimeOut();
		}
		SteamAPI.RunCallbacks();
	}

	public static PaymentPC GetInstance()
	{
		if (instance == null)
		{
			GameObject obj = new GameObject("PaymentPC");
			UnityEngine.Object.DontDestroyOnLoad(obj);
			instance = obj.AddComponent<PaymentPC>();
		}
		return instance;
	}

	private void OnItemListFailure(NetworkTask.ResultCode code)
	{
		Debug.LogError("OnItemListFailure" + code);
		Debug.LogError("プロダクトIDリストリクエストが失敗しました。やり直してください。");
	}

	private void OnItemListResultCodeError(int code)
	{
		Debug.LogError("OnItemListResultCodeError" + code);
		if (this.ProductListFailed != null)
		{
			this.ProductListFailed();
		}
	}

	private void OnFinishResultCodeError(int resultCode)
	{
		Debug.LogError("OnFinishResultCodeError" + resultCode);
		Debug.LogError("チェック不正です。");
		if (this.purchaseFinishResultCodeErrorEvent != null)
		{
			this.purchaseFinishResultCodeErrorEvent(resultCode);
		}
	}

	public override void purchaceStart(string ProductId, bool isFromAlert = false)
	{
		if (!isFromAlert)
		{
			IsAlertAgree = false;
		}
		bool isAlertOn = PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.PURCHASE_ALERT);
		PaymentPCStartTask task = new PaymentPCStartTask();
		task.SetParameter(ProductId, IsAlertAgree, isAlertOn);
		task.SkipAllCuteResultCodeCheckErrorPopup();
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			PurchasePC(ProductId, task);
		}, delegate
		{
		}, delegate(int code)
		{
			if (code == 329)
			{
				if (isAlertOn)
				{
					SystemText systemText = Data.SystemText;
					DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
					dialogBase.SetSize(DialogBase.Size.S);
					dialogBase.SetTitleLabel(systemText.Get("ErrorHeader_0329"));
					dialogBase.SetText(systemText.Get("Error_0329"));
					dialogBase.SetButtonText(systemText.Get("Shop_0082"));
					dialogBase.SetFadeButtonEnabled(flag: false);
					dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
					dialogBase.SetPanelDepth(6000);
					dialogBase.SetPanelSortingOrder(2);
					dialogBase.SetButtonDelegate(new EventDelegate(delegate
					{
						CallPaymentStartFromAlert(ProductId);
					}));
				}
				else
				{
					CallPaymentStartFromAlert(ProductId);
				}
			}
			else
			{
				Toolbox.NetworkManager.NetworkUI.OpenCloseOnlyErrorPopUp(code);
			}
		}));
		selectedStoreProductId = ProductId;
	}

	private void PurchasePC(string ProductId, PaymentPCStartTask task)
	{
		RefundWarningDialog.Start(task.NeedRefundWarningType, delegate
		{
			SteamMicroTxnInitTask steamMicroTxnInitTask = new SteamMicroTxnInitTask();
			steamMicroTxnInitTask.SetParameter(ProductId);
			StartCoroutine(Toolbox.NetworkManager.Connect(steamMicroTxnInitTask));
		});
	}

	public void purchaceFinish(string ProductId)
	{
		PaymentPCFinishTask paymentPCFinishTask = new PaymentPCFinishTask();
		paymentPCFinishTask.SetParameter(ProductId);
		paymentPCFinishTask.SkipCuteTimeOutPopup();
		StartCoroutine(Toolbox.NetworkManager.Connect(paymentPCFinishTask, delegate
		{
			string value = "";
			ProductNameList.TryGetValue(ProductId, out value);
			if (!ProductIsSpecialShop[ProductId])
			{
				paymentUI.PurchaseFinished(value);
			}
			if (this.ConsumePurchaseSucceeded != null)
			{
				this.ConsumePurchaseSucceeded();
			}
		}, paymentUI.PurchaseFailedPC));
	}

	public void initialize()
	{
		this.ProductListSucceeded = null;
		this.ProductListFailed = null;
		this.FinishFailureEvent = null;
		this.purchaseFinishSuccessEvent = null;
		this.purchaseFinishHttpErrorEvent = null;
		this.purchaseFinishResultCodeErrorEvent = null;
		this.purchaseRetryResultEvent = null;
		this.ConsumePurchaseSucceeded = null;
		ProductIdList = new List<string>();
		IdList = new List<string>();
		ProductNameList = new Dictionary<string, string>();
		ProductPriceList = new Dictionary<string, string>();
		FormatProductPriceList = new Dictionary<string, string>();
		ProductTextList = new Dictionary<string, string>();
		ProductPurchaseLimitList = new Dictionary<string, string>();
		ProductPurchaseNumberList = new Dictionary<string, string>();
		ProductCsvIdList = new Dictionary<string, string>();
		ProductImageNameList = new Dictionary<string, string>();
		ProductIsSpecialShop = new Dictionary<string, bool>();
		ProductCurrentPurchaseCount = new Dictionary<string, int>();
		ProductPurchaseLimitCount = new Dictionary<string, int>();
		ProductEndTime = new Dictionary<string, string>();
		PaymentPCItemListTask task = new PaymentPCItemListTask();
		StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			if (this.ProductListSucceeded != null)
			{
				this.ProductListSucceeded();
			}
		}, OnItemListFailure, OnItemListResultCodeError));
	}

	public void finalize()
	{
		this.ProductListSucceeded = null;
		this.ProductListFailed = null;
		this.FinishFailureEvent = null;
		this.purchaseFinishSuccessEvent = null;
		this.purchaseFinishHttpErrorEvent = null;
		this.purchaseFinishResultCodeErrorEvent = null;
		this.purchaseRetryResultEvent = null;
		this.ConsumePurchaseSucceeded = null;
		Payment.finalize();
	}

	public void StartTimeCount()
	{
		isCountTime = true;
	}

	public void StopTimeCount()
	{
		isCountTime = false;
	}

	private void checkTimeOut()
	{
		timer += Time.deltaTime;
		if (timer >= 30f)
		{
			timer = 0f;
			paymentUI.StopLoading();
			paymentUI.PurchaseTimeOut();
		}
	}
}
