﻿using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
#if UNITY_ANDROID || UNITY_IOS || UNITY_STANDALONE || UNITY_EDITOR
using UnityEngine;
#endif

namespace ChatSDK
{
    // ValueCallback<T>
    internal delegate void OnSuccessResultV2(IntPtr header, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] IntPtr[] data, DataType dType, int size, int callbackId);
    internal delegate void OnSuccessResult([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]IntPtr[] data, DataType dType, int size, int callbackId);
    internal delegate void OnErrorV2(int code, string desc, int callbackId);
    internal delegate void OnProgressV2(int progress, int callbackId);

    
    internal delegate void OnSuccess(int callbackId);

    /**
	* The callback for a method execution failure.
	*
	* @param code	   The error code.
	* @param desc      The error description.
	*/   
    public delegate void OnError(int code, string desc);

    /**
	* The callback for the method execution progress.
	*
	* @param progress   The execution progress value that ranges from 0 to 100 in percentage.
	* 
	*/
    public delegate void OnProgress(int progress);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
	/**
	    * The class of callbacks without a return value.
		* 
	    */
    public class CallBack   
    {
		/**
	    * The success callback.
		* 
	    */
        public Action Success;
		/**
	    * The error callback.
		* 
	    */    
        public OnError Error;  
		/**
	    * The progress callback.
		* 
	    */
        public OnProgress Progress; 

        internal string callbackId;

        /**
	    * The result callback constructor.
	    *
	    * @param onSuccess      The success callback.
	    * @param onProgress     The progress callback.
	    * @param onError        The error callback.
	    * 
	    */
        public CallBack(Action onSuccess = null, OnProgress onProgress = null, OnError onError = null)
        {
            Success = onSuccess;
            Error = onError;
            Progress = onProgress;
            callbackId = CallbackManager.Instance().CurrentId.ToString();
            CallbackManager.Instance().AddCallback(CallbackManager.Instance().CurrentId, this);
        }

        internal void ClearCallback()
        {
            Error(0, null);
            CallbackManager.Instance().RemoveCallback(int.Parse(callbackId));
        }

        ~CallBack()
        {
            Debug.Log($"CallBack ${callbackId} finalized!");
        }
    }
    /**
	* The class of callbacks with a return value.
	*/
    public class ValueCallBack<T> : CallBack
    {
	  /**
	   * The success callback with a return value.
	   */
        public Action<T> OnSuccessValue;

        /**
	    * The constructor for the class of callbacks with a return value.
	    *
	    * @param onSuccess      The success callback.
	    * @param onError        The error callback.
	    * 
	    */
        public ValueCallBack(Action<T> onSuccess = null, OnError onError = null)
        {
            OnSuccessValue = onSuccess;
            Error = onError;
            callbackId = CallbackManager.Instance().CurrentId.ToString();
            CallbackManager.Instance().AddValueCallback<T>(CallbackManager.Instance().CurrentId, this);
        }
    }
}
