﻿using System;
using System.Runtime.InteropServices;

namespace Adverty5
{
    public static class APIConfig
    {
        public const CharSet ADVERTY_NATIVE_CHAR_SET = CharSet.Unicode;
#if UNITY_IPHONE && !UNITY_EDITOR
        public const string LIBRARY_NAME = "__Internal";
#else
        public const string LIBRARY_NAME = "AdvertyUnityPlugin";
#endif
    }

    public enum AdPlacementTextureSize
    {
        /// <summary>
        /// 256x256
        /// </summary>
        Small = 0,
        /// <summary>
        /// 512x512
        /// </summary>
        Medium = 1,
        /// <summary>
        /// 1024x1024
        /// </summary>
        Large = 2,
        /// <summary>
        /// 2048x2048
        /// </summary>
        Huge = 3
    }

    [StructLayout(LayoutKind.Sequential, CharSet = APIConfig.ADVERTY_NATIVE_CHAR_SET)]
    public struct LaunchData
    {
        public string apiKey;
        public bool sandboxMode;
        public UserData userData;
        public SessionCallbacks callbacks;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct AdPlacementRegisterData
    {
        public IntPtr textureBuffer;
        public int width;
        public int height;
        public IntPtr senderObj;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct AdPlacementTextureData
    {
        public IntPtr texture;
        public int width;
        public int height;
        public int format;
        public AdPlacementTextureSize maxSize;
    };

    [StructLayout(LayoutKind.Sequential, CharSet = APIConfig.ADVERTY_NATIVE_CHAR_SET)]
    public struct AdPlacementDescription
    {
        public IntPtr senderObj;
        public bool allowVideo;
        public int width;
        public int height;
        public AdPlacementTextureData textureData;
    }

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void RegisterAdPlacementCallback(AdPlacementRegisterData data);
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void RegisterFailureCallback(IntPtr senderObj);
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void AdPlacementActivatedCallback(IntPtr senderObj);
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void PaidCallback(double price);
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void ViewabilityCheckCallback(IntPtr senderObj);
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void BrowserOpenCallback();
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void BrowserClosedCallback();

    [StructLayout(LayoutKind.Sequential, CharSet = APIConfig.ADVERTY_NATIVE_CHAR_SET)]
    public struct AdPlacementCallbacks
    {
        public RegisterAdPlacementCallback onRegisterSuccess;
        public RegisterFailureCallback onRegisterFailure;
        public AdPlacementActivatedCallback onAdPlacementActivated;
        public ViewabilityCheckCallback onViewabilityCheck;
    }

    public enum Gender
    {
        Undefined,
        Male,
        Female,
        Other,
        Unknown
    }

    [StructLayout(LayoutKind.Sequential, CharSet = APIConfig.ADVERTY_NATIVE_CHAR_SET)]
    public struct UserData
    {
        public Gender gender;
        public int yearOfBirth;
        public bool COPPA;
        public string consentString;
        public string gppConsentString;
        public int gppSid;
        public bool consented;

        public UserData(Gender gender = Gender.Unknown, int yearOfBirth = -1, bool COPPA = false, bool consented = true,
                        string consentString = "", string gppConsentString = "", int gppSid = 0)
        {
            this.gender = gender;
            this.yearOfBirth = yearOfBirth;
            this.COPPA = COPPA;
            this.consentString = consentString;
            this.gppConsentString = gppConsentString;
            this.gppSid = gppSid;
            this.consented = consented;
        }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = APIConfig.ADVERTY_NATIVE_CHAR_SET)]
    public struct SessionCallbacks
    {
        public PaidCallback OnPaidCallback;
        public BrowserOpenCallback OnBrowserOpenCallback;
        public BrowserClosedCallback OnBrowserClosedCallback;
    }
}
