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

namespace BIXOLON
{
    public class MPosControllerBCD
    {
        int nBgtId;
        int ndeviceId;

        public Int32 selectInterface(int interfaceType, string address)
        {
            nBgtId = MPOSBCDLib.selectInterface(interfaceType, address);
            return nBgtId;
        }

        public Int32 selectCommandMode(int commandMode)
        {
            return MPOSBCDLib.selectCommandMode(nBgtId, commandMode);
        }

        public Int32 setTimeout(int timeout)
        {
            return MPOSBCDLib.setTimeout(nBgtId, timeout);
        }

        public Int32 openService()
        {
            Int32 ret = MPOSBCDLib.openService(nBgtId);
            if (ret == MPosConst.RES_SUCCESS || ret == MPosConst.RES_ALREADY_OPEN)
                ndeviceId = MPOSBCDLib.getDeviceId(nBgtId);

            return ret;
        }

        public Int32 openServiceById(int openDeviceId, int timeout)
        {
            Int32 ret = MPOSBCDLib.openServiceById(nBgtId, openDeviceId, timeout);
            if (ret == MPosConst.RES_SUCCESS || ret == MPosConst.RES_ALREADY_OPEN)
                ndeviceId = MPOSBCDLib.getDeviceId(nBgtId);

            return ret;
        }

        public Int32 openUSBSerial(int positionNumber)
        {
            Int32 ret = MPOSBCDLib.openUSBSerial(nBgtId, positionNumber);
            if (ret == MPosConst.RES_SUCCESS || ret == MPosConst.RES_ALREADY_OPEN)
                ndeviceId = MPOSBCDLib.getDeviceId(nBgtId);

            return ret;
        }

        public Int32 closeService(int closeTimeout)
        {
            return MPOSBCDLib.closeService(nBgtId, closeTimeout);
        }

        public Int32 lsOpen()
        {
            return MPOSBCDLib.lsOpen(nBgtId);
        }

        public Int32 getDeviceId()
        {
            return MPOSBCDLib.getDeviceId(nBgtId);
        }

        public Int32 directIO(byte[] data, int dataLength)
        {
            return MPOSBCDLib.directIO(nBgtId, data, dataLength);
        }

        public Int32 setTransaction(int transactionMode)
        {
            return MPOSBCDLib.setTransaction(nBgtId, transactionMode);
        }

        public Int32 setReadMode(int readMode)
        {
            return MPOSBCDLib.setReadMode(nBgtId, readMode);
        }

        public Int32 convertHID(int newDeviceType)
        {
            return MPOSBCDLib.convertHID(nBgtId, newDeviceType);
        }

        public Int32 setStatusUpdateEvent(MPOSBCDLib.fnStatusUpdateEventCallback StatusUpdateEventCallback)
        {
            return MPOSBCDLib.setStatusUpdateEvent(nBgtId, StatusUpdateEventCallback);
        }

        public Int32 setDataEvent(MPOSBCDLib.fnDataEventCallback DataEventCallback)
        {
            return MPOSBCDLib.setDataEvent(nBgtId, DataEventCallback);
        }

        public Int32 clearScreen()
        {
            return MPOSBCDLib.clearScreen(nBgtId);
        }

        public Int32 displayString(string data, int codePage, int internationalCharacterSet, int line)
        {
            return MPOSBCDLib.displayString(nBgtId, data, codePage, internationalCharacterSet, line);
        }

        public Int32 displayStringAtLine(int line, string data)
        {
            return MPOSBCDLib.displayStringAtLine(nBgtId, line, data);
        }

        public Int32 setTextEncoding(int textEncoding)
        {
            return MPOSBCDLib.setTextEncoding(nBgtId, textEncoding);
        }

        public Int32 setCharacterset(int codepage)
        {
            return MPOSBCDLib.setCharacterset(nBgtId, codepage);
        }

        public Int32 setInternationalCharacterset(int intlCharset)
        {
            return MPOSBCDLib.setInternationalCharacterset(nBgtId, intlCharset);
        }

        public Int32 storeImageFile(string filename, int width, int imageNumber)
        {
            return MPOSBCDLib.storeImageFile(nBgtId, filename, width, imageNumber);
        }

        public Int32 displayImage(int imageNumber, int xPos, int yPos)
        {
            return MPOSBCDLib.displayImage(nBgtId, imageNumber, xPos, yPos);
        }

        public Int32 clearImage(int imageNumber)
        {
            return MPOSBCDLib.clearImage(nBgtId, imageNumber);
        }

        public Int32 showCursor(int cursor)
        {
            return MPOSBCDLib.showCursor(nBgtId, cursor);
        }
        
    }
    
    public class MPOSBCDLib
    {
        private const string MPOS_BCD_LIB_NAME = "MPosControllerBCD.dll";

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "selectInterface")]
        public extern static Int32 selectInterface(int interfaceType, [MarshalAs(UnmanagedType.LPWStr)] string address);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "selectCommandMode")]
        public extern static Int32 selectCommandMode(int bgtId, int commandMode);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "setTimeout")]
        public extern static Int32 setTimeout(int bgtId, int timeout);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "openService")]
        public extern static Int32 openService(int bgtId);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "openServiceById")]
        public extern static Int32 openServiceById(int bgtId, int openDeviceId, int timeout);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "openUSBSerial")]
        public extern static Int32 openUSBSerial(int bgtId, int positionNumber);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "closeService")]
        public extern static Int32 closeService(int bgtId, int closeTimeout);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "isOpen")]
        public extern static Int32 lsOpen(int bgtId);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "getDeviceId")]
        public extern static Int32 getDeviceId(int bgtId);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "directIO")]
        public extern static Int32 directIO(int bgtId, byte[] data, int dataLength);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "setTransaction")]
        public extern static Int32 setTransaction(int bgtId, int transactionMode);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "setReadMode")]
        public extern static Int32 setReadMode(int bgtId, int readMode);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "convertHID")]
        public extern static Int32 convertHID(int bgtId, int newDeviceType);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "setStatusUpdateEvent")]
        public extern static Int32 setStatusUpdateEvent(int bgtId, fnStatusUpdateEventCallback StatusUpdateEventCallback);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "setDataEvent")]
        public extern static Int32 setDataEvent(int bgtId, fnDataEventCallback DataEventCallback);

        public delegate void fnStatusUpdateEventCallback(int status);
        public delegate void fnDataEventCallback([MarshalAs(UnmanagedType.LPWStr)]StringBuilder data);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "clearScreen")]
        public extern static Int32 clearScreen(int bgtId);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "displayString")]
        public extern static Int32 displayString(int bgtId, [MarshalAs(UnmanagedType.LPWStr)] string data, int codePage, int internationalCharacterSet, int line);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "displayStringAtLine")]
        public extern static Int32 displayStringAtLine(int bgtId, int line, [MarshalAs(UnmanagedType.LPWStr)] string data);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "setTextEncoding")]
        public extern static Int32 setTextEncoding(int bgtId, int textEncoding);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "setCharacterset")]
        public extern static Int32 setCharacterset(int bgtId, int codepage);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "setInternationalCharacterset")]
        public extern static Int32 setInternationalCharacterset(int bgtId, int intlCharset);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "storeImageFile")]
        public extern static Int32 storeImageFile(int bgtId, [MarshalAs(UnmanagedType.LPWStr)] string filename, int width, int imageNumber);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "displayImage")]
        public extern static Int32 displayImage(int bgtId, int imageNumber, int xPos, int yPos);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "clearImage")]
        public extern static Int32 clearImage(int bgtId, int imageNumber);

        [DllImport(MPOS_BCD_LIB_NAME, EntryPoint = "showCursor")]
        public extern static Int32 showCursor(int bgtId, int cursor);

    }
}

