using System;
using System.Runtime.InteropServices;
using System.Text;


namespace GcDevicePc.Common
{
    class FileTransfer
    {
        [DllImport(@"WtFileSender.dll", EntryPoint = "fnWtClientInit", CharSet = CharSet.Unicode)]
        private static extern uint fnWtClientInit(byte[] Ip, ushort wTcpPort);

        [DllImport(@"WtFileSender.dll", EntryPoint = "fnWtFileSeverInit")]
        private static extern uint fnWtFileSeverInit(ushort wTcpPort);

        [DllImport(@"WtFileSender.dll", EntryPoint = "fnWtFileClose")]
        private static extern void fnWtClientClose(uint handle);

        [DllImport(@"WtFileSender.dll", EntryPoint = "fnWtClientCopytoServer")]
        private static extern int fnWtClientCopytoServer(uint handle, byte[] lpszFileName, byte[] lpszFilePathName, byte[] lpszRemoteFilePath);

        [DllImport(@"WtFileSender.dll", EntryPoint = "fnWtClientCopyfromServer")]
        private static extern int fnWtClientCopyfromServer(uint handle, byte[] lpszFileName, byte[] lpszFilePathName, byte[] lpszRemoteFilePath);

        [DllImport(@"WtFileSender.dll", EntryPoint = "fnWtCloseCEPro")]
        private static extern int fnWtCloseCEPro(uint handle, byte[] lpszFileName);

        [DllImport(@"WtFileSender.dll", EntryPoint = "fnWtRunPro")]
        private static extern int fnWtRunPro(uint handle, byte[] lpszFileName, byte[] lpszRemoteFilePath);


        private uint hFileServerHandle;

        public void WtClientInit(String RemoteIP, ushort wport)
        {
            try
            {
                byte[] fIP = Encoding.Unicode.GetBytes(RemoteIP);
                this.hFileServerHandle = fnWtClientInit(fIP, wport);
            }
            catch
            {

            }
        }

        public void WtClientClose()
        {
            try
            {
                fnWtClientClose(this.hFileServerHandle);
            }
            catch
            {

            }

        }

        public int WtClientCopytoServer(String filename, String filepath, String RemoteFilePath)
        {
            int bret;
            byte[] bfilename = Encoding.Unicode.GetBytes(filename);
            byte[] bfilepath = Encoding.Unicode.GetBytes(filepath);
            byte[] bRemoteFilePath = Encoding.Unicode.GetBytes(RemoteFilePath);
            bret = fnWtClientCopytoServer(this.hFileServerHandle, bfilename, bfilepath, bRemoteFilePath);
            return bret;
        }

        public int WtClientCopyfromServer(String filename, String filepath, String RemoteFilePath)
        {
            int bret;
            byte[] bfilename = Encoding.Unicode.GetBytes(filename);
            byte[] bfilepath = Encoding.Unicode.GetBytes(filepath);
            byte[] bRemoteFilePath = Encoding.Unicode.GetBytes(RemoteFilePath);
            bret = fnWtClientCopyfromServer(this.hFileServerHandle, bfilename, bfilepath, bRemoteFilePath);
            return bret;
        }

        public int WtHMICloseRro(String filename)
        {
            int bret;
            byte[] bfilename = Encoding.Unicode.GetBytes(filename);
            bret = fnWtCloseCEPro(this.hFileServerHandle, bfilename);
            return bret;
        }

        public int WtHMIRunPro(String filename, String RemoteFilePath)
        {
            int bret;
            byte[] bfilename = Encoding.Unicode.GetBytes(filename);
            byte[] bRemoteFilePath = Encoding.Unicode.GetBytes(RemoteFilePath);
            bret = fnWtRunPro(this.hFileServerHandle, bfilename, bRemoteFilePath);
            return bret;
        }
    }
}
