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

public class WortiseAdSettings
{
    #if UNITY_ANDROID && !UNITY_EDITOR
    public static AndroidJavaObject activity
    {
        get
        {
            AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            return playerClass.GetStatic<AndroidJavaObject>("currentActivity");
        }
    }
    
    private static AndroidJavaClass adContentRating;
    private static AndroidJavaClass adSettings;
    #endif
    
    public static string AssetKey
    {
        get
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                return adSettings.CallStatic<string>("getAssetKey", activity);
            }
            
            return null;
            #elif UNITY_IOS && !UNITY_EDITOR
            return Wortise_AdSettings_GetAssetKey();
            #else
            return null;
            #endif
        }
    }

    public static bool IsChildDirected
    {
        get
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                return adSettings.CallStatic<bool>("isChildDirected", activity);
            }
            
            return false;
            #elif UNITY_IOS && !UNITY_EDITOR
            return Wortise_AdSettings_GetChildDirected();
            #else
            return false;
            #endif
        }

        set
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                adSettings.CallStatic("setChildDirected", activity, value);
            }
            #elif UNITY_IOS && !UNITY_EDITOR
            Wortise_AdSettings_SetChildDirected(value);
            #endif
        }
    }

    public static bool IsTestEnabled
    {
        get
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            return adSettings.CallStatic<bool>("isTestEnabled");
            #elif UNITY_IOS && !UNITY_EDITOR
            return Wortise_AdSettings_GetTestEnabled();
            #else
            return false;
            #endif
        }

        set
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            adSettings.CallStatic("setTestEnabled", value);
            #elif UNITY_IOS && !UNITY_EDITOR
            Wortise_AdSettings_SetTestEnabled(value);
            #endif
        }
    }

    public static WortiseAdContentRating? MaxAdContentRating
    {
        get
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                AndroidJavaObject obj = adSettings.CallStatic<AndroidJavaObject>("getMaxAdContentRating", activity);

                if (obj == null)
                {
                    return null;
                }

                string name = obj.Call<string>("name");

                WortiseAdContentRating rating;

                Enum.TryParse(name, true, out rating);

                return rating;
            }
            
            return null;
            #elif UNITY_IOS && !UNITY_EDITOR
            string name = Wortise_AdSettings_GetMaxAdContentRating();

            if (name == null) {
                return null;
            }

            WortiseAdContentRating rating;

            Enum.TryParse(name, true, out rating);

            return rating;
            #else
            return null;
            #endif
        }

        set
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                string name = value.ToString();

                AndroidJavaObject obj = adContentRating.CallStatic<AndroidJavaObject>("valueOf", name.ToUpper());

                adSettings.CallStatic("setMaxAdContentRating", activity, obj);
            }
            #elif UNITY_IOS && !UNITY_EDITOR
            string name = value.ToString();

            Wortise_AdSettings_SetMaxAdContentRating(name);
            #endif
        }
    }

    public static string UserId
    {
        get
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                return adSettings.CallStatic<string>("getUserId", activity);
            }
            #endif

            return null;
        }

        set
        {
            #if UNITY_ANDROID && !UNITY_EDITOR
            if (activity != null)
            {
                adSettings.CallStatic("setUserId", activity, value);
            }
            #endif
        }
    }


    static WortiseAdSettings()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        adContentRating = new AndroidJavaClass("com.wortise.ads.AdContentRating");
        adSettings      = new AndroidJavaClass("com.wortise.ads.AdSettings");
        #endif
    }


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

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

    [DllImport("__Internal")]
    private static extern string Wortise_AdSettings_GetMaxAdContentRating();

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

    [DllImport("__Internal")]
    private static extern void Wortise_AdSettings_SetChildDirected(bool value);

    [DllImport("__Internal")]
    private static extern void Wortise_AdSettings_SetMaxAdContentRating(string value);

    [DllImport("__Internal")]
    private static extern void Wortise_AdSettings_SetTestEnabled(bool value);
    #endif
}
