import { NativeEventEmitter, DeviceEventEmitter, NativeModules, Platform } from 'react-native';
import { MPosControllerBase } from "./MPosControllerBase"
import { APIResponse } from "./constants/APIResponse";

const { MPosControllerScannerProxy } = NativeModules

export class MPosControllerScanner implements MPosControllerBase {
    private dataEventEmitter: NativeEventEmitter = new NativeEventEmitter(MPosControllerScannerProxy);

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

    private readonly isWindows: boolean;

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

        if (Platform.OS === 'ios') {
            this.dataEventEmitter.addListener("ScannerDataEvent", (event) => {
                console.log(`MPosControllerScannerProxy ${event.deviceid}: dataEvent fired`);
                if (!this.dataEvent) { return; }
                this.dataEvent(event.deviceid, event.data);
            })
        } else if (Platform.OS === 'windows') {
            DeviceEventEmitter.addListener("ScannerDataEvent", (event) => {
                console.log('MPosControllerScannerProxy: dataEvent fired');

                const [deviceid, data] = event;
                event.deviceid = deviceid;
                event.data = data;

                if (!this.dataEvent) { return; }
                this.dataEvent(event.deviceid, event.data);
            })
        } else {
            DeviceEventEmitter.addListener("ScannerDataEvent", (event) => {
                console.log('MPosControllerScannerProxy: dataEvent fired');
                if (!this.dataEvent) { return; }
                this.dataEvent(event.deviceid, event.data);
            })
        }
    }

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

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

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

    async closeService(timeout: number): Promise<number> {
        try {
            return await MPosControllerScannerProxy.closeService(timeout);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

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

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

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

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

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

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

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

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