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

const { MPosControllerHIDProxy } = NativeModules

export class MPosControllerHID implements MPosControllerBase {
    private dataEventEmitter: NativeEventEmitter = new NativeEventEmitter(MPosControllerHIDProxy);

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

    private readonly isWindows: boolean;

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

        if (Platform.OS === 'ios') {
            this.dataEventEmitter.addListener("HIDDataEvent", (event) => {
                console.log('MPosControllerHIDProxy: dataEvent fired');
                if (!this.dataEvent) { return; }
                this.dataEvent(event.deviceid, event.data);
            })
        } else if (Platform.OS === 'windows') {
            DeviceEventEmitter.addListener("HIDDataEvent", (event) => {
                console.log('MPosControllerHIDProxy: 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("HIDDataEvent", (event) => {
                console.log('MPosControllerHIDProxy: 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 MPosControllerHIDProxy.selectInterface([interfaceType, adddress]);
            } else {
                return await MPosControllerHIDProxy.selectInterface(Number(interfaceType), adddress);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

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

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

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

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

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

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

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

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

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

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

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