using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AOT;
using AnyThinkAds.ThirdParty.LitJson;
using System;

public class ATUnityCBridge {
	public delegate void CCallBack(string wrapperClass, string msg);
	
    #if UNITY_IOS || UNITY_IPHONE
	[DllImport("__Internal")]
    extern static bool at_message_from_unity(string msg, CCallBack callback);

    [DllImport("__Internal")]
    extern static int at_get_message_for_unity(string msg, CCallBack callback);

    [DllImport("__Internal")]
    extern static string at_get_string_message_for_unity(string msg, CCallBack callback);
    #endif

    [MonoPInvokeCallback(typeof(CCallBack))]
    static public void MessageFromC(string wrapperClass, string msg) {
        Debug.Log("Unity: ATUnityCBridge::MessageFromC(" + wrapperClass + "," + msg + ")");
        JsonData jsonData = JsonMapper.ToObject(msg);
        if (wrapperClass.Equals("ATRewardedVideoWrapper")) {
            Debug.Log("Unity: ATUnityCBridge::MessageFromC(), hit rv");
            ATRewardedVideoWrapper.InvokeCallback(jsonData);
        } else if (wrapperClass.Equals("ATNativeAdWrapper")) {
            ATNativeAdWrapper.InvokeCallback(jsonData);
        } else if (wrapperClass.Equals("ATInterstitialAdWrapper")) {
            ATInterstitialAdWrapper.InvokeCallback(jsonData);
        } else if (wrapperClass.Equals("ATBannerAdWrapper")) {
            ATBannerAdWrapper.InvokeCallback(jsonData);
        } else if (wrapperClass.Equals("ATNativeBannerAdWrapper")) {
            ATNativeBannerAdWrapper.InvokeCallback(jsonData);
        } else if (wrapperClass.Equals("ATSplashAdWrapper")) {
            ATSplashAdWrapper.InvokeCallback(jsonData);
        }
    }

    static public bool SendMessageToC(string className, string selector, object[] arguments) {
        return SendMessageToC(className, selector, arguments, false);
    }

    static public int GetMessageFromC(string className, string selector, object[] arguments) {
        Debug.Log("Unity: ATUnityCBridge::GetMessageFromC()");
        Dictionary<string, object> msgDict = new Dictionary<string, object>();
        msgDict.Add("class", className);
        msgDict.Add("selector", selector);
        msgDict.Add("arguments", arguments);
        #if UNITY_IOS || UNITY_IPHONE
        return at_get_message_for_unity(JsonMapper.ToJson(msgDict), null);
        #else
        return 0;
        #endif
    }

    static public string GetStringMessageFromC(string className, string selector, object[] arguments) {
        Debug.Log("Unity: ATUnityCBridge::GetStringMessageFromC()");
        Dictionary<string, object> msgDict = new Dictionary<string, object>();
        msgDict.Add("class", className);
        msgDict.Add("selector", selector);
        msgDict.Add("arguments", arguments);
        #if UNITY_IOS || UNITY_IPHONE
        return at_get_string_message_for_unity(JsonMapper.ToJson(msgDict), null);
        #else 
        return "";
        #endif
    }

    static public bool SendMessageToC(string className, string selector, object[] arguments, bool carryCallback) {
        Debug.Log("Unity: ATUnityCBridge::SendMessageToC()");
        Dictionary<string, object> msgDict = new Dictionary<string, object>();
    	msgDict.Add("class", className);
    	msgDict.Add("selector", selector);
    	msgDict.Add("arguments", arguments);
        CCallBack callback = null;
        if (carryCallback) callback = MessageFromC;
        #if UNITY_IOS || UNITY_IPHONE
        return at_message_from_unity(JsonMapper.ToJson(msgDict), callback);
        #else
        return false;
        #endif
    }

#if UNITY_IOS || UNITY_IPHONE
    [DllImport("__Internal")]
    extern static bool at_message_from_unity(string msg, Func<string, int> callback);
#endif
    static public void SendMessageToCWithCallBack(string className, string selector, object[] arguments, Func<string, int> callback)
    {
        Debug.Log("Unity: ATUnityCBridge::SendMessageToCWithCallBack()");
        Dictionary<string, object> msgDict = new Dictionary<string, object>();
        msgDict.Add("class", className);
        msgDict.Add("selector", selector);
        msgDict.Add("arguments", arguments);
#if UNITY_IOS || UNITY_IPHONE
        at_message_from_unity(JsonMapper.ToJson(msgDict), callback);
#endif
    }
}
