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

const { MPosControllerConfigProxy } = NativeModules

export class MPosControllerConfig 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 MPosControllerConfigProxy.selectInterface([interfaceType, adddress]);
            } else {
                return await MPosControllerConfigProxy.selectInterface(Number(interfaceType), adddress);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

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

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

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

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

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

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

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

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

    async searchDevices(): Promise<Array<number>> {
        try {
            return await MPosControllerConfigProxy.searchDevices();
        }
        catch (e) {
            return new Array();
        }
    }

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

    async getUSBDevice(deviceId: number): Promise<string> {
        try {
            return await MPosControllerConfigProxy.getUSBDevice(deviceId);
        } catch (e) {
            return "";
        }
    }

    async getCustomDevices(deviceType: number): Promise<Array<string>> {
        try {
            return await MPosControllerConfigProxy.getCustomDevices(deviceType);
        } catch (e) {
            return [];
        }
    }

    async addCustomDevice(deviceType: number, vid: string, pid: string): Promise<number> {
        try {
            return await MPosControllerConfigProxy.addCustomDevice(deviceType, vid, pid);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async deleteCustomDevice(deviceType: number, vid: string, pid: string): Promise<number> {
        try {
            return await MPosControllerConfigProxy.deleteCustomDevice(deviceType, vid, pid);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async reInitCustomDeviceType(deviceType: number): Promise<number> {
        try {
            return await MPosControllerConfigProxy.reInitCustomDeviceType(deviceType);
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async getSerialConfig(deviceId: number): Promise<Array<number>> {
        try {
            return await MPosControllerConfigProxy.getSerialConfig(deviceId);
        } catch (e) {
            return [];
        }
    }

    async getSerialConfiguration(deviceId: number): Promise<Array<number>> {
        try {
            return await MPosControllerConfigProxy.getSerialConfiguration(deviceId);
        } catch (e) {
            return [];
        }
    }

    async setSerialConfig(deviceId: number,
        baudRate: number,
        dataBit: number,
        stopBit: number,
        parityBit: number): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerConfigProxy.setSerialConfig([deviceId, baudRate, dataBit, stopBit, parityBit]);
            } else {
                return await MPosControllerConfigProxy.setSerialConfig(deviceId, baudRate, dataBit, stopBit, parityBit);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

    async setSerialConfiguration(deviceId: number,
        baudRate: number,
        dataBit: number,
        stopBit: number,
        parityBit: number,
        flowControl: number): Promise<number> {
        try {
            if (this.isWindows) {
                return await MPosControllerConfigProxy.setSerialConfiguration([deviceId, baudRate, dataBit, stopBit, parityBit, flowControl]);
            } else {
                return await MPosControllerConfigProxy.setSerialConfiguration(deviceId, baudRate, dataBit, stopBit, parityBit, flowControl);
            }
        } catch (e) {
            return APIResponse.RES_FAIL;
        }
    }

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

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