﻿using System;
using System.Collections;
using System.Collections.Generic;
#if UNITY_IOS && !UNITY_EDITOR
using System.Runtime.InteropServices;
#endif
using UnityEngine;

public class WortiseConsentManager
{
    #if UNITY_ANDROID && !UNITY_EDITOR
    private static AndroidJavaObject activity
    {
        get
        {
            return WortiseSdk.activity;
        }
    }
    
    private static AndroidJavaObject consentManager;
    #endif

    #if UNITY_IOS && !UNITY_EDITOR
    private static int lastRequestId = 1;

    internal static Dictionary<int, Action<bool>> requestCallbacks = new Dictionary<int, Action<bool>>();
    #endif
    
    public static bool CanCollectData
    {
        get
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                return consentManager.CallStatic<bool>("canCollectData", activity);
            }
            
            return false;
            #elif UNITY_IOS && !UNITY_EDITOR
            return Wortise_ConsentManager_CanCollectData();
            #else
            return false;
            #endif
        }
    }

    public static bool CanRequestPersonalizedAds
    {
        get
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                return consentManager.CallStatic<bool>("canRequestPersonalizedAds", activity);
            }

            return false;
            #elif UNITY_IOS && !UNITY_EDITOR
            return Wortise_ConsentManager_CanRequestPersonalizedAds();
            #else
            return false;
            #endif
        }
    }

    public static bool Exists
    {
        get
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                return consentManager.CallStatic<bool>("exists", activity);
            }
            
            return false;
            #elif UNITY_IOS && !UNITY_EDITOR
            return Wortise_ConsentManager_Exists();
            #else
            return false;
            #endif
        }
    }
    
    
    static WortiseConsentManager()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        consentManager = new AndroidJavaClass("com.wortise.ads.consent.ConsentManager");
        #endif
    }
    
    public static void Request(Action<bool> callback = null)
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        if (activity != null)
        {
            consentManager.CallStatic("request", activity, new ConsentRequestListener(callback));
        }
        #elif UNITY_IOS && !UNITY_EDITOR
        int id = lastRequestId++;

        requestCallbacks.Add(id, callback);

        Wortise_ConsentManager_Request(id);
        #endif
    }
    
    public static void RequestIfRequired(Action<bool> callback = null)
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        if (activity != null)
        {
            consentManager.CallStatic("requestIfRequired", activity, new ConsentRequestListener(callback));
        }
        #elif UNITY_IOS && !UNITY_EDITOR
        int id = lastRequestId++;

        requestCallbacks.Add(id, callback);

        Wortise_ConsentManager_RequestIfRequired(id);
        #endif
    }


    #if UNITY_ANDROID && !UNITY_EDITOR
    private class ConsentRequestListener : AndroidJavaProxy
    {
        private Action<bool> callback;


        public ConsentRequestListener(Action<bool> callback) : base("com.wortise.ads.unity.ConsentRequestListener")
        {
            this.callback = callback;
        }

        public AndroidJavaObject invoke(bool shown)
        {
            WortiseSdk.RaiseAction(() =>
            {
                callback?.Invoke(shown);
            });

            return null;
        }
    }
    #endif

    #if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")]
    private static extern bool Wortise_ConsentManager_CanCollectData();

    [DllImport("__Internal")]
    private static extern bool  Wortise_ConsentManager_CanRequestPersonalizedAds();

    [DllImport("__Internal")]
    private static extern bool Wortise_ConsentManager_Exists();

    [DllImport("__Internal")]
    private static extern void Wortise_ConsentManager_Request(int id);

    [DllImport("__Internal")]
    private static extern void Wortise_ConsentManager_RequestIfRequired(int id);
    #endif
}
