﻿using System;
using System.Runtime.InteropServices;
using AOT;
using UnityEngine;

namespace Adverty5
{
    public static class AdvertyUnityPlugin
    {
#if UNITY_IPHONE && !UNITY_EDITOR
        public const string LIBRARY_NAME = "__Internal";
#else
        public const string LIBRARY_NAME = "AdvertyUnityPlugin";
#endif

        [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = APIConfig.ADVERTY_NATIVE_CHAR_SET)]
        public delegate void LoggerDelegate([MarshalAs(UnmanagedType.LPWStr)] string str, int verbosity);

        private static LoggerDelegate loggerDelegate;
        private static int graphicsEventId = 0;

#pragma warning disable CA1401

#if UNITY_ANDROID && !UNITY_EDITOR
        [DllImport(LIBRARY_NAME)]
        public static extern void UnityLoadAdvertyPluginWithActivity(
            IntPtr loggerFuncPtr, IntPtr activity, [MarshalAs(UnmanagedType.LPStr)] string graphicsDeviceVersion);
#else
        [DllImport(LIBRARY_NAME)]
        public static extern void UnityLoadAdvertyPlugin(IntPtr loggerFuncPtr);
#endif

        [DllImport(LIBRARY_NAME)]
        public static extern IntPtr GetRenderUpdate();

        [DllImport(LIBRARY_NAME)]
        private static extern int UnityGetGraphicsEventID();
#pragma warning restore CA1401

        [MonoPInvokeCallback(typeof(LoggerDelegate))]
        public static void LoggerCallBackFunction([MarshalAs(UnmanagedType.LPWStr)] string str, int verbosity)
        {
            switch (verbosity)
            {
            case 2:
                Debug.LogWarning(str);
                break;
            case 3:
            case 4:
                Debug.LogError(str);
                break;
            default:
                Debug.Log(str);
                break;
            }
        }

#if UNITY_ANDROID && !UNITY_EDITOR
        public static void LoadAdvertyPlugin(IntPtr activity)
        {
            string deviceVersion = SystemInfo.graphicsDeviceVersion;
            loggerDelegate = LoggerCallBackFunction;
            UnityLoadAdvertyPluginWithActivity(Marshal.GetFunctionPointerForDelegate(loggerDelegate), activity,
                                               deviceVersion);
        }
#else
        public static void LoadAdvertyPlugin()
        {
            loggerDelegate = LoggerCallBackFunction;
            UnityLoadAdvertyPlugin(Marshal.GetFunctionPointerForDelegate(loggerDelegate));
        }
#endif

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
        private static void LoadPluginInRuntime()
        {
#if UNITY_ANDROID && !UNITY_EDITOR
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            LoadAdvertyPlugin(activity.GetRawObject());
#else
            LoadAdvertyPlugin();
#endif
        }

        public static int GetGraphicsEventID()
        {
            if (graphicsEventId == 0)
            {
                graphicsEventId = UnityGetGraphicsEventID();
            }

            return graphicsEventId;
        }
    }
}
