#region Header

// Copyright (c) 2021-2022 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.

#endregion

using UnityEngine;
using UnityEngine.Rendering;
using System;
using System.Runtime.InteropServices;
using System.Text;
using AOT;

namespace BlackBox
{
        public static class PluginAPI
        {
#if UNITY_IOS   
                public const string PLUGIN_NAME = "__Internal";
                public const string PLUGIN_NAME_BLACKBOX_CORE = "__Internal";
#else
                public const string PLUGIN_NAME = "BlackBoxUnityPlugin";
                public const string PLUGIN_NAME_BLACKBOX_CORE = "blackbox-core";
#endif

                #region BlackBoxUnityPlugin.dll
                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern IntPtr blackbox_info_get_version();

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool blackbox_init(string game_name,
                                                        string unity_version,
                                                        string helper_path,
                                                        string issue_reporter_path,
                                                        string crash_path,
                                                        string log_path,
                                                        string blackbox_ini_path,
                                                        int gpu_device_id,
                                                        int gpu_api,
                                                        bool is_editor,
                                                        string helper_log_alternative_path,
                                                        string gpu_name,
                                                        string gpu_version
                                                        );

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool blackbox_start_session();

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool blackbox_end_session();

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool blackbox_update(float tick_time, float cpu, float gpu, bool in_game_session);

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool blackbox_shutdown();

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool blackbox_set_game_has_crashed();

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool blackbox_set_log_enabled(bool is_enabled);

#if UNITY_STANDALONE_WIN
                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint blackbox_update_backbuffer_texture(IntPtr texture_ptr);

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern IntPtr blackbox_get_render_event_func();

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern IntPtr blackbox_get_render_event_dx12_func();
#endif
                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint blackbox_config_set_base_url(string base_url);

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint blackbox_save_local_config(string game_name);

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint blackbox_load_local_config(string game_name);

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern IntPtr blackbox_config_get_issue_reporter_hotkey();

                [DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool blackbox_get_issue_report_directory(string parent_path, out string out_dir, out UIntPtr out_dir_size);


                #endregion

                #region BlackBox-Core.dll

#if UNITY_ANDROID
                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern void bbx_set_cert(string cert);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern void bbx_set_crash_file_location(string location);

#if UNITY_ANDROID && !UNITY_EDITOR
                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool bbx_init_android_device_id(string file_path);
#endif
#else
                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint bbx_config_set_blackbox_helper_alternative_path(string path);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool bbx_get_issue_report_directory(string parent_path, StringBuilder out_dir, int out_dir_size);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool bbx_config_get_use_engine_to_capture_screenshot();

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool bbx_config_get_enable_issue_reporter();

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool bbx_config_get_include_log_in_issue_report();

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern int bbx_capture_screenshot(string parent_path);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern int bbx_launch_issue_reporter(string parent_path);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint bbx_config_set_issue_reporter_hotkey(string issue_reporter_hotkey_);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint bbx_config_set_enable_issue_reporter(bool is_issue_reporter_enabled_);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint bbx_config_set_include_log_in_issue_report(bool include_log_in_issue_report_);
#endif

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint bbx_local_config_set_enable_crash_reporter(string new_local_config_value);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint bbx_local_config_set_enable_basic_profiling(string new_local_config_value);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint bbx_local_config_set_store_dxdiag(string new_local_config_value);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern uint bbx_local_config_set_store_crash_video(string new_local_config_value);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern IntPtr bbx_config_get_device_id();

                [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
                public delegate void GenerateDeviceIDCallback(IntPtr buffer, int bufferSize);


                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern IntPtr bbx_generate_device_id(string engine, GenerateDeviceIDCallback callback);

                [DllImport(PLUGIN_NAME_BLACKBOX_CORE, CallingConvention = CallingConvention.Cdecl)]
                public static extern bool bbx_set_thirdparty_account(string platform, string id, string username, string email);

                #endregion

                #region Logger

                [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
                public delegate void blackbox_log_bridge_delegate(string str);

                [MonoPInvokeCallback(typeof(blackbox_log_bridge_delegate))]
                public static void blackbox_log_callback(string str) { Logger.Log("[BlackBoxNative]", str); }

                [DllImport(PLUGIN_NAME)]
                public static extern void blackbox_set_log_function(IntPtr log_function);

                #endregion
        }
}
