using System;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.ReactNative.Managed;
using WinRT.BxlComponent.UwpWrapper;
using System.Threading;


namespace ReactNativeRnBixolonBgateLibs
{
    [ReactModule]
    public sealed partial class MPosControllerPrinterProxy 
    { 
        private MPosDevicePrinterImpl _printerDev;
        public MPosControllerPrinterProxy()
        {
            _printerDev = new MPosDevicePrinterImpl();
        }
        [ReactMethod("selectInterface")]
        public void selectInterface(string[] array, IReactPromise<int> promise)
        {
            promise.Resolve(_printerDev.selectInterface(
                Convert.ToInt32(array[0]), 
                array[1])
                );
            return;
        }

        [ReactMethod("openService")]
        public async void openService(uint[] array, IReactPromise<int> promise) 
        {
            promise.Resolve(await _printerDev.openService(
                array[0], 
                array[1]
                ));
            return;
        }
        [ReactMethod("closeService")]
        public async void closeService(uint closeTimeout/*second*/, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.closeService(closeTimeout));
            return;
        }
        [ReactMethod("selectCommandMode")]
        public void selectCommandMode(int commandMode, IReactPromise<int> promise)
        {
            promise.Resolve(_printerDev.selectCommandMode(commandMode));
            return;
        }
        [ReactMethod("isOpen")]
        public void isOpen(IReactPromise<int> promise)
        {
            promise.Resolve(Convert.ToInt32(_printerDev.isOpen()));
            return;
        }
        [ReactMethod("getDeviceId")]
        public void getDeviceId(IReactPromise<int> promise)
        {
            promise.Resolve(Convert.ToInt32(_printerDev.getDeviceId()));
            return;
        }
        [ReactMethod("directIO")]
        public async void directIO([ReadOnlyArray()] byte[] data, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.directIO(data));
            return;
        }
        [ReactMethod("cutPaper")]
        public async void cutPaper(int cutType, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.cutPaper(cutType));
            return;
        }
        [ReactMethod("printText")]
        public async void  printText(string[] array, IReactPromise<int> promise)
        {

            MPosWrapperConstant attribute = null;

            if (array != null)
            {
                attribute = new MPosWrapperConstant();
                attribute.FontType  = Convert.ToInt32            (array[1] );
                attribute.Width     = Convert.ToInt32            (array[2] );
                attribute.Height    = Convert.ToInt32            (array[3] );
                attribute.Bold      = Convert.ToBoolean(int.Parse(array[4]));
                attribute.Underline = Convert.ToInt32            (array[5] );
                attribute.Reverse   = Convert.ToBoolean(int.Parse(array[5]));
                attribute.Alignment = Convert.ToInt32            (array[7] );
                
            }

            promise.Resolve(await _printerDev.printText(array[0], attribute));
            return;
        }
        [ReactMethod("print1DBarcode")]
        public async void print1DBarcode(string[] array, IReactPromise<int> promise) 
        {
            promise.Resolve(await _printerDev.print1DBarcode(
                array[0], 
                Convert.ToInt32(array[1]), 
                Convert.ToInt32(array[2]), 
                Convert.ToInt32(array[3]), 
                Convert.ToInt32(array[4]), 
                Convert.ToInt32(array[5])
                ));
            return;
        }
        [ReactMethod("SetCharacterSet")]
        public async void setCharacterset(int characterset, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.setCharacterset(characterset));
            return;
        }
        [ReactMethod("setInternationalCharacterset")]
        public async void setInternationalCharacterset(int internationalCharacterset, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.setInternationalCharacterset(internationalCharacterset));
            return;
        }
        [ReactMethod("setPagemode")]
        public async void setPagemode(int pageMode, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.setPagemode(pageMode));
            return;
        }
        [ReactMethod("SetPageModePrintArea")]
        public int SetPageModePrintArea(string[] array, IReactPromise<int> promise) 
        {
            return _printerDev.setPagemodePrintArea(Convert.ToInt32(
                array[0]), 
                Convert.ToInt32(array[1]), 
                Convert.ToInt32(array[2]), 
                Convert.ToInt32(array[3])
                );
        }
        [ReactMethod("setPagemodeDirection")]
        public int setPagemodeDirection(int direction, IReactPromise<int> promise)
        {
            return _printerDev.setPagemodeDirection(direction);
        }
        [ReactMethod("setPagemodePosition")]
        public int setPagemodePosition(string[] array, IReactPromise<int> promise)
        {
            return _printerDev.setPagemodePosition(Convert.ToInt32(
                array[0]), 
                Convert.ToInt32(array[1])
                );
        }
        [ReactMethod("printBitmapFile")]
        public async void printBitmapFile(string[] array , IReactPromise<int> promise) 
        {
            promise.Resolve(await _printerDev.printBitmapFile(
                array[0], 
                Convert.ToInt32(array[1]), 
                Convert.ToInt32(array[2]), 
                Convert.ToInt32(array[3]), 
                Convert.ToBoolean(int.Parse(array[4])), 
                Convert.ToBoolean(int.Parse(array[5]))
                ));
            return;
        }
        [ReactMethod("printBase64Image")]
        public async void printBitmapWithBase64(string[] array, IReactPromise<int> promise) 
        {
            promise.Resolve(await _printerDev.printBitmapWithBase64(
                array[0], 
                Convert.ToInt32(array[1]), 
                Convert.ToInt32(array[2]), 
                Convert.ToInt32(array[3]), 
                Convert.ToBoolean(int.Parse(array[4])), 
                Convert.ToBoolean(int.Parse(array[5]))
                ));
            return;
        }
        [ReactMethod("printQRCode")]
        public async void printQRCode(string[] array , IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.printQRCode(
                array[0], Convert.ToInt32(array[1]), 
                Convert.ToInt32(array[2]), 
                Convert.ToInt32(array[3]), 
                Convert.ToInt32(array[4])
                ));
            return;
        }
        [ReactMethod("printPDF417")] //Use array due to more than 7 parameters.`
        public async void printPDF417(string[] array, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.printPDF417(
                array[0],                   //string data
                Convert.ToInt32(array[1]),  //int    Symbology
                Convert.ToInt32(array[2]),  //int    Alignment
                Convert.ToInt32(array[3]),  //int    ColumnNumber
                Convert.ToInt32(array[4]),  //int    RowNumber
                Convert.ToInt32(array[5]),  //int    ModuleWidth
                Convert.ToInt32(array[6]),  //int    ModuleHeight
                Convert.ToInt32(array[7]))  //int    EccLevel
                );
            return;
        }
        [ReactMethod("printDataMatrix")]
        public async void printDataMatrix(string[] array, IReactPromise<int> promise) 
        {
            promise.Resolve(await _printerDev.printDataMatrix(
                array[0], 
                Convert.ToInt32(array[1]), 
                Convert.ToInt32(array[2])
                ));
            return;
        }
        [ReactMethod("checkPrinterStatus")]
        public async void checkPrinterStatus(IReactPromise<int> promise)
        {
            promise.Resolve((int)await _printerDev.checkPrinterStatus());
            return;
        }
        [ReactMethod("asbEnable")]
        public async void asbEnable(bool enable, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.asbEnable(enable));
            return;
        }
        [ReactMethod("openDrawer")]
        public async void openDrawer(int pinNumber, IReactPromise<int> promise)
        {
            if (pinNumber == 0)
                promise.Resolve(await _printerDev.directIO(new byte[] { 0x1B, 0x70, 0x00, 0x19, 0xFF }));
            promise.Resolve(await _printerDev.directIO(new byte[] { 0x1B, 0x70, 0x01, 0x19, 0xFF }));

            return;
        }
        [ReactMethod("printBox")]
        public async void printBox(int[] array, IReactPromise<int> promise) 
        {
            promise.Resolve(await _printerDev.printBox(
                array[0], 
                array[1], 
                array[2], 
                array[3], 
                array[4]
                ));
            return;
        }
        [ReactMethod("printLine")]
        public async void printLine(int[] array, IReactPromise<int> promise)
        {
            promise.Resolve(await _printerDev.printLine(
                array[0], 
                array[1], 
                array[2], 
                array[3], 
                array[4]
                ));
            return;
        }
        [ReactMethod("getModelName")]
        public async void getModelName(IReactPromise<string> promise)
        {
            promise.Resolve(await _printerDev.getModelName());
            return;
        }
        [ReactMethod("getFirmwareVersion")]
        public async void getFirmwareVersion(IReactPromise<string> promise)
        {
            promise.Resolve(await _printerDev.getFirmwareVersion());
            return;
        }
    }
}
