using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;

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

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

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

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

       
        private uint hFileServerHandle;

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

            }
             
        }

        public void WtClientInit(String RemoteIP, ushort wport)
        {
            try
            {
                byte[] fIP = Encoding.Unicode.GetBytes(RemoteIP);
                this.hFileServerHandle = fnWtClientInit(fIP, wport);
            }
            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;
        }

    }
}
