import { NativeEventEmitter, DeviceEventEmitter, EmitterSubscription, NativeModules, Platform } from 'react-native';
import { MPosControllerBase } from "./MPosControllerBase"
import { APIResponse } from "./constants/APIResponse";
import {
    Label_Status,
    Label_Bitmap_Font,
    Label_Vector_Font,
    Label_Rotation,
    Label_Alignment,
    Label_BarcodeType,
    Label_Barcode_Text,
    Label_MaxiCodeMode,
    Label_PDF417_HRI,
    Label_PDF417_BarcodeOrigin,
    Label_PDF417_ECC,
    Label_DataCompression,
    Label_QRCode_Model,
    Label_QRCode_EccLevel,
    Label_Code49_HRI,
    Label_Code49_StartingMode,
    Label_CodaBlock_Mode,
    Label_CheckDigitSelection,
    Label_MSI_HRI,
    Label_Plessey_HRI,
    Label_RSS_BarcodeType,
    Label_drawBlock_Option,
    Label_Circle_Size,
    Label_DitheringType,
    Label_CompressType,
    Label_CharacterSet,
    Label_InternationalCharset,
    Label_PrintingType,
    Label_MediaType,
    Label_Orientation,
    Label_RFID_Type,
    Label_RFID_DataType
} from "./constants/Constants_Label"

const { MPosControllerLabelPrinterProxy } = NativeModules

export class MPosControllerLabelPrinter implements MPosControllerBase {
    private outputCompletedEventEmitter: NativeEventEmitter = new NativeEventEmitter(MPosControllerLabelPrinterProxy);
    private dataEventEmitter: NativeEventEmitter = new NativeEventEmitter(MPosControllerLabelPrinterProxy);
    
    private outputCompletedEventSubscription?: EmitterSubscription;
    private dataEventSubscription?: EmitterSubscription;

    outputCompletedEvent?: (deviceid: number) => void;
    dataEvent?: (deviceid: number, data: Array<number>) => void;

    private readonly isWindows: boolean;

    constructor() {
        this.isWindows = Platform.OS === 'windows';

        if (Platform.OS === 'ios') {
            this.outputCompletedEventSubscription = this.outputCompletedEventEmitter.addListener("LabelPrinterOutputCompletedEvent", (event) => {
                console.log('MPosControllerLabelPrinter: outputCompletedEvent fired');
                if (!this.outputCompletedEvent) { return; }
                this.outputCompletedEvent(event.deviceid);
            });
            this.dataEventSubscription = this.dataEventEmitter.addListener("LabelPrinterDataEvent", (event) => {
                console.log('MPosControllerLabelPrinter: DataEventPrinter fired');
                if (!this.dataEvent) { return; }
                this.dataEvent(event.deviceid, event.data);
            });
        } else if (Platform.OS === 'windows') {
            // Windows 작업 추가 필요
        } else {
            // Android 작업 추가 필요
        }
    }

    async selectInterface(interfaceType: number, adddress: string): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.selectInterface([interfaceType, adddress]);
            } else {
                return await MPosControllerLabelPrinterProxy.selectInterface(Number(interfaceType), adddress);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async selectCommandMode(commandMode: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.selectCommandMode(Number(commandMode));
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async openService(deviceID: number, timeout: number): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.openService([deviceID, timeout]);
            } else {
                return await MPosControllerLabelPrinterProxy.openService(Number(deviceID), timeout);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async closeService(timeout: number): Promise<number> {
        try {
            this.outputCompletedEventSubscription?.remove();
            this.dataEventSubscription?.remove();

            return await MPosControllerLabelPrinterProxy.closeService(timeout);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async isOpen(): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.isOpen();
        } catch (e) {
            return 0;
        }
    }

    async getDeviceId(): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.getDeviceId();
        } catch (e) {
            return -1;
        }
    }

    async directIO(data: Array<number>): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.directIO(data);
        } catch (e) {
            return -1;
        }
    }

    async setTransaction(mode: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setTransaction(mode);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setReadMode(mode: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setReadMode(mode);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setKeepNetworkConnection(mode: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setKeepNetworkConnection(Number(mode));
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async getSDKVersion(): Promise<string> {
        try {
            return await MPosControllerLabelPrinterProxy.getSDKVersion();
        } catch (e) {
            return "";
        }
    }

    async checkPrinterStatus(): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.checkPrinterStatus();
        } catch (e) {
            return Label_Status.LABEL_STATUS_FAIL;
        }
    }

    async printBuffer(numberOfCopies: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.printBuffer(numberOfCopies);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawTextDeviceFont(
        data: string,
        xPos: number,
        yPos: number,
        fontSelection: string,
        fontWidth: number,
        fontHeight: number,
        rightSpace: number,
        rotation: number,
        reverse: number,
        bold: number,
        rightToLeft: number,
        alignment: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawTextDeviceFont(
                    [data, xPos, yPos, fontSelection, fontWidth, fontHeight, rightSpace, rotation, reverse, bold, rightToLeft, alignment]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawTextDeviceFont(
                    data, xPos, yPos, fontSelection, fontWidth, fontHeight, rightSpace, rotation, reverse, bold, rightToLeft, alignment
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawTextVectorFont(
        data: string,
        xPos: number,
        yPos: number,
        fontSelection: string,
        fontWidth: number,
        fontHeight: number,
        rightSpace: number,
        rotation: number,
        reverse: number,
        bold: number,
        italic: number,
        rightToLeft: number,
        alignment: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawTextVectorFont(
                    [data, xPos, yPos, fontSelection, fontWidth, fontHeight, rightSpace, rotation, reverse, bold, italic, rightToLeft, alignment]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawTextVectorFont(
                    data, xPos, yPos, fontSelection, fontWidth, fontHeight, rightSpace, rotation, reverse, bold, italic, rightToLeft, alignment
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcode1D(
        data: string,
        xPos: number,
        yPos: number,
        barcodeType: number,
        widthNarrow: number,
        widthWide: number,
        height: number,
        hri: number,
        quietZoneWidth: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcode1D(
                    [data, xPos, yPos, barcodeType, widthNarrow, widthWide, height, hri, quietZoneWidth, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcode1D(
                    data, xPos, yPos, barcodeType, widthNarrow, widthWide, height, hri, quietZoneWidth, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeMaxiCode(
        data: string,
        xPos: number,
        yPos: number,
        mode: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeMaxiCode([data, xPos, yPos, mode]);
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeMaxiCode(data, xPos, yPos, mode);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodePDF417(
        data: string,
        xPos: number,
        yPos: number,
        maximumRowCount: number,
        maximumColumnCount: number,
        errorCorrectionLevel: number,
        dataCompressionMethod: number,
        hri: number,
        barcodeOriginPoint: number,
        moduleWidth: number,
        barHeight: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodePDF417(
                    [data, xPos, yPos, maximumRowCount, maximumColumnCount, errorCorrectionLevel, dataCompressionMethod, hri, barcodeOriginPoint, moduleWidth, barHeight, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodePDF417(
                    data, xPos, yPos, maximumRowCount, maximumColumnCount, errorCorrectionLevel, dataCompressionMethod, hri, barcodeOriginPoint, moduleWidth, barHeight, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeQRCode(
        data: string,
        xPos: number,
        yPos: number,
        size: number,
        model: number,
        eccLevel: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeQRCode([data, xPos, yPos, size, model, eccLevel, rotation]);
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeQRCode(data, xPos, yPos, size, model, eccLevel, rotation);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeDataMatrix(
        data: string,
        xPos: number,
        yPos: number,
        size: number,
        reverse: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeDataMatrix([data, xPos, yPos, size, reverse, rotation]);
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeDataMatrix(data, xPos, yPos, size, reverse, rotation);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeAztec(
        data: string,
        xPos: number,
        yPos: number,
        size: number,
        extendedChannel: number,
        eccLevel: number,
        menuSymbol: number,
        numberOfSymbols: number,
        optionalID: string,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeAztec(
                    [data, xPos, yPos, size, extendedChannel, eccLevel, menuSymbol, numberOfSymbols, optionalID, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeAztec(
                    data, xPos, yPos, size, extendedChannel, eccLevel, menuSymbol, numberOfSymbols, optionalID, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeCode49(
        data: string,
        xPos: number,
        yPos: number,
        widthNarrow: number,
        widthWide: number,
        height: number,
        hri: number,
        startingMode: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeCode49(
                    [data, xPos, yPos, widthNarrow, widthWide, height, hri, startingMode, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeCode49(
                    data, xPos, yPos, widthNarrow, widthWide, height, hri, startingMode, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeCodaBlock(
        data: string,
        xPos: number,
        yPos: number,
        widthNarrow: number,
        widthWide: number,
        height: number,
        securityLevel: number,
        dataColumns: number,
        mode: string,
        rowsToEncode: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeCodaBlock(
                    [data, xPos, yPos, widthNarrow, widthWide, height, securityLevel, dataColumns, mode, rowsToEncode]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeCodaBlock(
                    data, xPos, yPos, widthNarrow, widthWide, height, securityLevel, dataColumns, mode, rowsToEncode
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeMicroPDF(
        data: string,
        xPos: number,
        yPos: number,
        moduleWidth: number,
        height: number,
        mode: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeMicroPDF(
                    [data, xPos, yPos, moduleWidth, height, mode, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeMicroPDF(
                    data, xPos, yPos, moduleWidth, height, mode, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeIMB(
        data: string,
        xPos: number,
        yPos: number,
        hri: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeIMB(
                    [data, xPos, yPos, hri, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeIMB(
                    data, xPos, yPos, hri, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeMSI(
        data: string,
        xPos: number,
        yPos: number,
        widthNarrow: number,
        widthWide: number,
        height: number,
        checkDigit: number,
        printCheckDigit: number,
        hri: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeMSI(
                    [data, xPos, yPos, widthNarrow, widthWide, height, checkDigit, printCheckDigit, hri, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeMSI(
                    data, xPos, yPos, widthNarrow, widthWide, height, checkDigit, printCheckDigit, hri, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodePlessey(
        data: string,
        xPos: number,
        yPos: number,
        widthNarrow: number,
        widthWide: number,
        height: number,
        printCheckDigit: number,
        hri: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodePlessey(
                    [data, xPos, yPos, widthNarrow, widthWide, height, printCheckDigit, hri, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodePlessey(
                    data, xPos, yPos, widthNarrow, widthWide, height, printCheckDigit, hri, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeTLC39(
        data: string,
        xPos: number,
        yPos: number,
        narrowWidth: number,
        widthWide: number,
        height: number,
        rowHeightMicroPDF417: number,
        narrowWidthOfMicroPDF417: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeTLC39(
                    [data, xPos, yPos, narrowWidth, widthWide, height, rowHeightMicroPDF417, narrowWidthOfMicroPDF417, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeTLC39(
                    data, xPos, yPos, narrowWidth, widthWide, height, rowHeightMicroPDF417, narrowWidthOfMicroPDF417, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBarcodeRSS(
        data: string,
        xPos: number,
        yPos: number,
        barcodeType: number,
        magnification: number,
        separatorHeight: number,
        height: number,
        segmentWidth: number,
        rotation: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBarcodeRSS(
                    [data, xPos, yPos, barcodeType, magnification, separatorHeight, height, segmentWidth, rotation]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBarcodeRSS(
                    data, xPos, yPos, barcodeType, magnification, separatorHeight, height, segmentWidth, rotation
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBlock(
        startPosX: number,
        startPosY: number,
        endPosX: number,
        endPosY: number,
        option: string,
        thickness: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBlock(
                    [startPosX, startPosY, endPosX, endPosY, option, thickness]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBlock(
                    startPosX, startPosY, endPosX, endPosY, option, thickness
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawCircle(
        startPosX: number,
        startPosY: number,
        size: number,
        multiplier: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawCircle(
                    [startPosX, startPosY, size, multiplier]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawCircle(
                    startPosX, startPosY, size, multiplier
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawImageFile(
        fileName: string,
        xPos: number,
        yPos: number,
        width: number,
        threshold: number,
        ditheringType: number,
        compressType: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawImageFile(
                    [fileName, xPos, yPos, width, threshold, ditheringType, compressType]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawImageFile(
                    fileName, xPos, yPos, width, threshold, ditheringType, compressType
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async drawBase64Image(
        base64String: string,
        xPos: number,
        yPos: number,
        width: number,
        threshold: number,
        ditheringType: number,
        compressType: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.drawBase64Image(
                    [base64String, xPos, yPos, width, threshold, ditheringType, compressType]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.drawBase64Image(
                    base64String, xPos, yPos, width, threshold, ditheringType, compressType
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setCharacterset(
        chararacterset: number,
        internationalChararacterset: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.setCharacterset([chararacterset, internationalChararacterset]);
            } else {
                return await MPosControllerLabelPrinterProxy.setCharacterset(chararacterset, internationalChararacterset);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setPrintingType(
        printingType: string
    ): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setPrintingType(printingType);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setMargin(
        horizontalMargin: number,
        verticalMargin: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.setMargin([horizontalMargin, verticalMargin]);
            } else {
                return await MPosControllerLabelPrinterProxy.setMargin(horizontalMargin, verticalMargin);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setLength(
        labelLength: number,
        gapLength: number,
        mediaType: string,
        offsetLength: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.setLength(
                    [labelLength, gapLength, mediaType, offsetLength]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.setLength(
                    labelLength, gapLength, mediaType, offsetLength
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setWidth(labelWidth: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setWidth(labelWidth);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setSpeed(speed: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setSpeed(speed);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setDensity(density: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setDensity(density);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setOrientation(orientation: string): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setOrientation(orientation);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setOffset(length: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setOffset(length);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setCuttingPosition(position: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setCuttingPosition(position);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setAutoCutter(
        enableAutoCutter: number,
        cuttingPeriod: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.setAutoCutter([enableAutoCutter, cuttingPeriod]);
            } else {
                return await MPosControllerLabelPrinterProxy.setAutoCutter(enableAutoCutter, cuttingPeriod);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setRewinder(enable: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setRewinder(enable);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setupRFID(
        rfidType: number,
        numberOfRetries: number,
        numberOfLabel: number,
        radioPower: number
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.setupRFID(
                    [rfidType, numberOfRetries, numberOfLabel, radioPower]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.setupRFID(
                    rfidType, numberOfRetries, numberOfLabel, radioPower
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setEPCDataStructure(
        totalSize: number,
        fieldSizes: string
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.setEPCDataStructure([totalSize, fieldSizes]);
            } else {
                return await MPosControllerLabelPrinterProxy.setEPCDataStructure(totalSize, fieldSizes);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setRFIDPosition(transPosition: number): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.setRFIDPosition(transPosition);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async calibrateRFID(): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.calibrateRFID();
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async writeRFID(
        dataType: number,
        startingBlockNumber: number,
        dataLength: number,
        data: string
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.writeRFID(
                    [dataType, startingBlockNumber, dataLength, data]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.writeRFID(
                    dataType, startingBlockNumber, dataLength, data
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setRFIDPassword(
        oldAccessPwd: string,
        oldKillPwd: string,
        newAccessPwd: string,
        newKillPwd: string
    ): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerLabelPrinterProxy.setRFIDPassword(
                    [oldAccessPwd, oldKillPwd, newAccessPwd, newKillPwd]
                );
            } else {
                return await MPosControllerLabelPrinterProxy.setRFIDPassword(
                    oldAccessPwd, oldKillPwd, newAccessPwd, newKillPwd
                );
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async lockRFID(): Promise<number> {
        try {
            return await MPosControllerLabelPrinterProxy.lockRFID();
        }
        catch (e) { return APIResponse.RES_FAIL; }
    }

    async getFirmwareVersion(): Promise<string> {
        try {
            return await MPosControllerLabelPrinterProxy.getFirmwareVersion();
        } catch (e) {
            return "";
        }
    }

    async getModelName(): Promise<string> {
        try {
            return await MPosControllerLabelPrinterProxy.getModelName();
        } catch (e) {
            return "";
        }
    }

    async getOccuredData(): Promise<Array<number>|null> {
        try {
            return await MPosControllerLabelPrinterProxy.getOccuredData();
        } catch (e) {
            return Promise.resolve(null);
        }
     }
}