using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace TestCDLL
{
    internal class Program
    {

        // Import the CreateANSODHandle_CS function
        [DllImport(@"C:\Projects\ANLS\ANSLIB\x64\Release\ANSODEngine.dll", EntryPoint = "CreateANSODHandle_CS", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern IntPtr CreateANSODHandle_CS(ref IntPtr handle,
                                                         string licenseKey,
                                                         string modelFilePath,
                                                         string modelFileZipPassword,
                                                         float modelThreshold,
                                                         float modelConfThreshold,
                                                         float modelNMSThreshold,
                                                         int autoDetectEngine,
                                                         int modelType,
                                                         int detectionType);



        // Import the RunInferenceImagePath_CS function
        [DllImport(@"C:\Projects\ANLS\ANSLIB\x64\Release\ANSODEngine.dll", EntryPoint = "RunInferenceImagePath_CS", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern IntPtr RunInferenceImagePath_CS(ref IntPtr handle, string imageFilePath);

        // Helper function to convert IntPtr to a C# string
        public static string PtrToStringAnsi(IntPtr ptr)
        {
            return Marshal.PtrToStringAnsi(ptr);
        }
        [DllImport(@"C:\Projects\ANLS\ANSLIB\x64\Release\ANSODEngine.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int ReleaseANSODHandle(ref IntPtr handle);

        static void Main(string[] args)
        {

            IntPtr handle = IntPtr.Zero;
            IntPtr resultPtr = CreateANSODHandle_CS(ref handle,
                                                "",
                                                "C:\\Programs\\TrainingWorkingStation\\Projects\\CrowdHuman\\Models\\ANS_PeopleHead(CPU)_2.zip",
                                                "",
                                                0.5f,
                                                0.6f,
                                                0.4f,
                                                1,
                                                5,
                                                1);



            string createResult = PtrToStringAnsi(resultPtr);
            Console.WriteLine("CreateANSODHandle_CS Result: " + createResult);

            // Call RunInferenceImagePath_CS to run inference on an image
            IntPtr inferenceResultPtr = RunInferenceImagePath_CS(ref handle, "C:\\Programs\\TrainingWorkingStation\\Projects\\CrowdHuman\\data\\273271,1b9330008da38cd6.jpg");
            string inferenceResult = PtrToStringAnsi(inferenceResultPtr);
            Console.WriteLine("RunInferenceImagePath_CS Result: " + inferenceResult);


            // Call ReleaseANSODHandle to release the ANSOD handle
            int result = ReleaseANSODHandle(ref handle);

            if (result == 0)
            {
                Console.WriteLine("Handle released successfully.");
            }
            else
            {
                Console.WriteLine("Failed to release handle.");
            }
            Console.ReadLine();
        }
    }
}
