using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using QC.MultiplexManager;

namespace MultiplexManagerInterface;

public class MultiplexManagerManaged
{
	[return: MarshalAs(UnmanagedType.U1)]
	public unsafe bool SwitchPath(int inputParameterSource, sbyte* multiplexManagerConfigFile, int dutNumber, sbyte* requestedRFPorts, sbyte* requestedBands)
	{
		IntPtr ptr = new IntPtr(requestedBands);
		IntPtr ptr2 = new IntPtr(requestedRFPorts);
		IntPtr ptr3 = new IntPtr(multiplexManagerConfigFile);
		return MultiplexManagerStatic.SwitchPath((InputParameterSource)inputParameterSource, Marshal.PtrToStringAnsi(ptr3), dutNumber, Marshal.PtrToStringAnsi(ptr2), Marshal.PtrToStringAnsi(ptr));
	}

	[return: MarshalAs(UnmanagedType.U1)]
	public unsafe bool SwitchPathByAntennaNumbers(int inputParameterSource, sbyte* multiplexManagerConfigFile, int dutNumber, sbyte* requestedTxAntNumbers, sbyte* requestedRxAntNumbers, sbyte* technology_code)
	{
		IntPtr ptr = new IntPtr(technology_code);
		IntPtr ptr2 = new IntPtr(requestedRxAntNumbers);
		IntPtr ptr3 = new IntPtr(requestedTxAntNumbers);
		IntPtr ptr4 = new IntPtr(multiplexManagerConfigFile);
		return MultiplexManagerStatic.SwitchPathByAntennaNumbers((InputParameterSource)inputParameterSource, Marshal.PtrToStringAnsi(ptr4), dutNumber, Marshal.PtrToStringAnsi(ptr3), Marshal.PtrToStringAnsi(ptr2), Marshal.PtrToStringAnsi(ptr));
	}

	[return: MarshalAs(UnmanagedType.U1)]
	public unsafe bool SetPortsPathsGlobalVariable(int inputParameterSource, sbyte* multiplexManagerConfigFile, int dutNumber)
	{
		IntPtr ptr = new IntPtr(multiplexManagerConfigFile);
		return MultiplexManagerStatic.SetPorsPathsGlobalVariable((InputParameterSource)inputParameterSource, Marshal.PtrToStringAnsi(ptr), dutNumber);
	}

	public unsafe void SetMultiPortGlobalVariable(sbyte* multiplexManagerConfigFile, int dutNumber)
	{
		IntPtr ptr = new IntPtr(multiplexManagerConfigFile);
		MultiplexManagerStatic.SetMultiPortGlobalVariable(Marshal.PtrToStringAnsi(ptr), dutNumber);
	}

	public unsafe void GetGlobalVariable(sbyte* name, sbyte* valueOut, int valueSize)
	{
		IntPtr ptr = new IntPtr(name);
		string globalVariable = MultiplexManagerStatic.GetGlobalVariable(Marshal.PtrToStringAnsi(ptr));
		if (globalVariable != null)
		{
			sbyte* ptr2 = (sbyte*)(void*)Marshal.StringToHGlobalAnsi(globalVariable);
			global::_003CModule_003E.strcpy_s(valueOut, (uint)valueSize, ptr2);
			IntPtr hglobal = new IntPtr(ptr2);
			Marshal.FreeHGlobal(hglobal);
		}
		else if (valueOut != null)
		{
			*valueOut = 0;
		}
	}

	public unsafe void SetGlobalVariable(sbyte* name, sbyte* value)
	{
		IntPtr ptr = new IntPtr(value);
		IntPtr ptr2 = new IntPtr(name);
		MultiplexManagerStatic.SetGlobalVariable(Marshal.PtrToStringAnsi(ptr2), Marshal.PtrToStringAnsi(ptr));
	}

	[return: MarshalAs(UnmanagedType.U1)]
	public unsafe bool LockResource(int inputParameterSource, sbyte* multiplexManagerConfigFile, int dutNumber, int Lock_Timeout)
	{
		IntPtr ptr = new IntPtr(multiplexManagerConfigFile);
		return MultiplexManagerStatic.LockResource((InputParameterSource)inputParameterSource, Marshal.PtrToStringAnsi(ptr), dutNumber, Lock_Timeout);
	}

	[return: MarshalAs(UnmanagedType.U1)]
	public unsafe bool UnlockResource(int inputParameterSource, sbyte* multiplexManagerConfigFile, int dutNumber)
	{
		IntPtr ptr = new IntPtr(multiplexManagerConfigFile);
		return MultiplexManagerStatic.UnlockResource((InputParameterSource)inputParameterSource, Marshal.PtrToStringAnsi(ptr), dutNumber);
	}

	public MultiplexManagerManaged()
	{
		AppDomain.CurrentDomain.AssemblyResolve += ResolvePath;
	}

	private static Assembly ResolvePath(object sender, ResolveEventArgs args)
	{
		AssemblyName assemblyName = new AssemblyName(args.Name);
		string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), assemblyName.Name + ".dll");
		if (!File.Exists(text))
		{
			return null;
		}
		return Assembly.LoadFrom(text);
	}
}
