using System;
#if UNITY_WEBGL && !UNITY_EDITOR
using System.Runtime.InteropServices;
#endif
using UnityEngine;
using AOT;

namespace Arkadium
{
	[Serializable]
	public class Details {
		public string GameKey;
		public string GameVersion;
		public string NestVersion;
		public string SdkVersion;
	}

	public interface IArkadiumHost
	{
		void GetQueryParameter(string param, Action<string> cb);
		void GetDetails(Action<Details> cb);
		void OpenPurchaseForm(Action<bool> cb);
		void OpenPurchaseForm(string currencySku, Action<bool> cb);
	}

	public class ArkadiumHost : IArkadiumHost
	{
#if UNITY_WEBGL && !UNITY_EDITOR
		[DllImport("__Internal")]
		private static extern void _GetQueryParameter(string param, Action<string> cb);
		[DllImport("__Internal")]
		private static extern void _GetDetails(Action<string> cb);
		[DllImport("__Internal")]
		private static extern void _OpenPurchaseForm(string currencySku, Action<int> cb);
#else
		private void _GetQueryParameter(string param, Action<string> cb) { cb?.Invoke(param + "-value"); }
		private void _GetDetails(Action<string> cb) { cb?.Invoke("{ \"GameKey\": \"game-key\", \"GameVersion\": \"game-version\", \"NestVersion\": \"nest-version\", \"SdkVersion\": \"sdk-version\", }"); }
		private void _OpenPurchaseForm(string currencySku, Action<int> cb) {
			cb?.Invoke(1);
		}
#endif

		private static Action<string> _getQueryParameterCb;
		private static Action<Details> _getDetailsCb;
		private static Action<bool> _openPurchaseFormCb;

		
		public void GetQueryParameter(string param, Action<string> cb)
		{
			_getQueryParameterCb = cb;
			_GetQueryParameter(param, GetQueryParameterCb);
		}
		public void GetDetails(Action<Details> cb)
		{
			_getDetailsCb = cb;
			_GetDetails(GetDetailsCb);
		}
		public void OpenPurchaseForm(Action<bool> cb) {
			_openPurchaseFormCb = cb;
			_OpenPurchaseForm("default", OpenPurchaseFormCb);
		}
		public void OpenPurchaseForm(string currencySku, Action<bool> cb) {
			_openPurchaseFormCb = cb;
			_OpenPurchaseForm(currencySku, OpenPurchaseFormCb);
		}

		[MonoPInvokeCallback(typeof(Action<string>))]
		private static void GetQueryParameterCb(string result) {
			ArkadiumSDK.Instance.Log($"Query parameter returned {result}");
			_getQueryParameterCb?.Invoke(result);
			_getQueryParameterCb = null;
		}

		[MonoPInvokeCallback(typeof(Action<string>))]
		private static void GetDetailsCb(string detailsSerialized) {
			ArkadiumSDK.Instance.Log($"get details returned {detailsSerialized}");

			if (string.IsNullOrEmpty(detailsSerialized)) {
				_getDetailsCb?.Invoke(null);
				_getDetailsCb = null;
				return;
			}
			try {
				var details = JsonUtility.FromJson<Details>(detailsSerialized);
				_getDetailsCb?.Invoke(details);
				_getDetailsCb = null;
			} catch (Exception) {}
		}

		[MonoPInvokeCallback(typeof(Action<int>))]
		public static void OpenPurchaseFormCb(int result) {
			ArkadiumSDK.Instance.Log($"Purchase request returned {result}");
			_openPurchaseFormCb?.Invoke(result == 1);
			_openPurchaseFormCb = null;
		}
	}
}
