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

const { MPosControllerBCDProxy } = NativeModules

export class MPosControllerBCD implements MPosControllerBase {
    private readonly isWindows: boolean;

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

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

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

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

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

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

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

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

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

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

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

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