package kr.co.icontrols.ilp300_700;

import android.util.Log;

import kr.co.icontrols.v40ioctl.V40IOInterface;

public class IOInterface {

    String TAG = "IOInterface";

    private V40IOInterface mV40IOIF;

    public IOInterface() {
        Log.d(TAG, " ======= [IOInterface] =======");
        mV40IOIF = new V40IOInterface();
    }

    public int WpdInterfaceOpen() {
        return mV40IOIF.WpdInterfaceOpen();
    }

    public int WpdInterfaceClose() {
        return mV40IOIF.WpdInterfaceClose();
    }

    public int WpdInterfaceControl(int cmd, int arg) {
        return mV40IOIF.WpdInterfaceControl(cmd, arg);
    }

    public int SetMacAddr(String mac) {
        return mV40IOIF.SetMacAddr(mac);
    }

    public int IpAddr(String ip) {
        return mV40IOIF.IpAddr(ip);
    }

    public int GatewayIp(String gw) {
        return mV40IOIF.GatewayIp(gw);
    }

    public int NetMask(String sn) {
        return mV40IOIF.NetMask(sn);
    }

    public int NetSet() {
        return mV40IOIF.NetSet();
    }

    public int UsbUpdate() {
        return mV40IOIF.UsbUpdate();
    }

    public int LiveUpdate() {
        return mV40IOIF.LiveUpdate();
    }

    public int LcdWakeUp() {
        return mV40IOIF.LcdWakeUp();
    }

    public void NetDns1(String dns) {
        mV40IOIF.NetDns1(dns);
    }

    public void NetDns2(String dns) {
        mV40IOIF.NetDns2(dns);
    }

    public void volumectl(String vol) {
        Log.d(TAG, "[volumectl] vol [" + vol + "]");
        mV40IOIF.volumectl(vol);
    }

    public int setVolumeCtl(int vol) {
        Log.d(TAG, "[setVolume] vol [" + vol + "]");
        return mV40IOIF.setVolume(vol);
    }

    public int getVolumeCtl() {
        return mV40IOIF.getVolume();
    }

    public void Reboot(int sec) {
        mV40IOIF.Reboot(sec);
    }

    private boolean bWallpadJNI = false;

    public void ctrlWallpadJNI(boolean bOpen) {
        try {
            if (bOpen) {
                if (!bWallpadJNI) {
                    WpdInterfaceOpen();
                    bWallpadJNI = true;
                }
            } else {
                WpdInterfaceClose();
                bWallpadJNI = false;
            }
        } catch (Exception e) {
            bWallpadJNI = false;
            Log.e(TAG, "[Exception] ctrlWallpadJNI(bOpen)");
            e.printStackTrace();
        }
    }

    public void setNetworkInterface(String strIP, String strNetmask, String strGateway, String strDNS, String strMAC) {
        try {
            Log.i(TAG, "[setNetworkInterface] strIP [" + strIP + "], strNetmask [" + strNetmask + "], strGateway [" + strGateway + "], strDNS [" + strDNS + "], strMAC [" + strMAC + "]");
            IpAddr(strIP);
            NetMask(strNetmask);
            GatewayIp(strGateway);
            SetMacAddr(strMAC);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setNetworkInterface(String strIP, String strNetmask, String strGateway, String strDNS, String strMAC)");
            e.printStackTrace();
        }
    }

    public void setDns(String dns1, String dns2) {
        NetDns1(dns1);
        NetDns2(dns2);
        Log.i(TAG, "V40 SET DNS IP#1: " + dns1 + " / DNS IP#2: " + dns2);
    }

    /**
     * Remote ADB connection enable
     * IP : Lobbyphone IP
     * Port : 15000
     */
    public void enableADBRemoteConnection(String strPort) {
        try {
            Log.d(TAG, "[enableADBRemoteConnection] ===== START =====");
            ctrlWallpadJNI(true);
            mV40IOIF.ADBenable(strPort);
            ctrlWallpadJNI(false);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] enableADBRemoteConnection()");
            e.printStackTrace();
        }
    }

    /**
     * Remote ADB connection disable
     */
    public void disableADBRemoteConnection() {
        try {
            Log.d(TAG, "[disableADBRemoteConnection] ===== START =====");
            ctrlWallpadJNI(true);
            mV40IOIF.ADBdisable();
            ctrlWallpadJNI(false);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] disableADBRemoteConnection()");
            e.printStackTrace();
        }
    }

    /**
     * Remote ADB connection state check
     */
    public int checkADBRemoteConnectionState() {
        int nResult = -99;
        try {
            Log.d(TAG, "[checkADBRemoteConnectionState] ===== START =====");
            ctrlWallpadJNI(true);
            nResult = mV40IOIF.ADBstate();
            ctrlWallpadJNI(false);
            Log.i(TAG, "[checkADBRemoteConnectionState] nResult [" + nResult + "]");
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkADBRemoteConnectionState()");
            e.printStackTrace();
            return nResult;
        }
    }

    private int IOCTL_FINGERPRINT_MODULE_INTERRUPT = 0xFF30;
    private int IOCTL_POWER_FINGERPRINT_MODULE = 0xFF31;
    private int IOCTL_DEVICE_DIVISION = 0xFF32;
    private int IOCTL_CSI_CAMERA_POWEREN = 0xFF33;
    private int IOCTL_DEVICE_MODEL_TYPE_GET = 0xFF34;
    private int IOCTL_ANALOG_CAMERA_POWEREN = 0xFF35;
    private int IOCTL_EXTERNAL_SENSOR = 0xFF36;   // 외부기기로부터 센서값 입력
    private int IOCTL_FIRE_DETECTION = 0xFF37;
    private int IOCTL_DOOR_OPEN = 0xFF38;
    private int IOCTL_DOOR_SWITCH = 0xFF39;
    private int IOCTL_POWER_KEYPAD = 0xFF41;
    private int IOCTL_POWER_CARDREADER = 0xFF41;
    private int IOCTL_CAMERA_BRIGHTNESS_SET = 0xF901;
    private int IOCTL_CAMERA_CONTRAST_SET = 0xF903;
    private int IOCTL_FIRE_DETECTION_ACK = 0xFF3C;
    private int IOCTL_APP_LIVE_SIGNAL = 0xFF3F;

    /**
     * 로비폰 모델타입
     * @return 0: 4.3'(현산향) / 1: 7'(대외향)
     */
    public int getDeviceModelType() {
        try {
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_DEVICE_MODEL_TYPE_GET, 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[getDeviceModelType] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getDeviceModelType()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * Keypad power control
     * @return 0: 성공 / -1: 실패
     */
    public int ctrlKeypadPower(boolean bOn) {
        try {
            Log.d(TAG, "[ctrlKeypadPower] bOn = " + bOn);
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_POWER_KEYPAD, (bOn) ? 1 : 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[ctrlKeypadPower] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] ctrlKeypadPower()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * Cardreader power control
     * @return 0: 성공 / -1: 실패
     */
    public int ctrlCardreaderPower(boolean bOn) {
        try {
            Log.d(TAG, "[ctrlCardreaderPower] bOn = " + bOn);
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_POWER_CARDREADER, (bOn) ? 1 : 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[ctrlCardreaderPower] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] ctrlCardreaderPower()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 지문모듈 전원
     * @return 0: 성공 / -1: 실패
     */
    public int ctrlFingerprintModulePower(boolean bOn) {
        try {
            Log.d(TAG, "[ctrlFingerprintModulePower] bOn = " + bOn);
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_POWER_FINGERPRINT_MODULE, (bOn) ? 1 : 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[ctrlFingerprintModulePower] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] ctrlFingerprintModulePower(boolean bOn)");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * Analog 카메라 전원
     * @return 0: Off / 1: On
     */
    public int setAnalogCameraPower(boolean bOn) {
        try {
            Log.d(TAG, "[setAnalogCameraPower] bOn = " + bOn);
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_ANALOG_CAMERA_POWEREN, (bOn) ? 1 : 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[setAnalogCameraPower] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setAnalogCameraPower()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 카메라 밝기 조절
     * @return 0: 성공 / -1: 실패
     */
    public int ctrlCameraBrightness(int nValue) {
        try {
            Log.d(TAG, "[ctrlCameraBrightness] nValue = " + nValue);
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_CAMERA_BRIGHTNESS_SET, nValue);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[ctrlCameraBrightness] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] ctrlCameraBrightness()");
            e.printStackTrace();
            return -1;
        }
    }

    public int ctrlCameraContrast(int nValue) {
        try {
            Log.d(TAG, "[ctrlCameraContrast] nValue = " + nValue);
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_CAMERA_CONTRAST_SET, nValue);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[ctrlCameraContrast] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] ctrlCameraContrast()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 문열림 제어
     * @return 0: 성공 / -1: 실패
     */
    public int ctrlDoorOpen(boolean bOpen) {
        try {
            Log.d(TAG, "[ctrlDoorOpen] bOpen = " + bOpen);
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_DOOR_OPEN, (bOpen) ? 0 : 1);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[ctrlDoorOpen] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] ctrlDoorOpen()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * CSI 카메라 전원
     * @return 0: Off / 1: On
     */
    public int setCSICameraPower(boolean bOn) {
        try {
            Log.d(TAG, "[setCSICameraPower] bOn = " + bOn);
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_CSI_CAMERA_POWEREN, (bOn) ? 1 : 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[setCSICameraPower] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setCSICameraPower()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * EM Lock/Motion Sensor
     * @return 0: Normal / 1: Detect
     */
    public int getDoorswitchSensorStatus() {
        try {
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_DOOR_SWITCH, 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[getDoorswitchSensorStatus] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getDoorswitchSensorStatus()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * Fire Sensor
     * @return 0: Normal / 1: Detect
     */
    public int getFireSensorStatus() {
        try {
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_DOOR_SWITCH, 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[getFireSensorStatus] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getFireSensorStatus()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * External Sensor
     * @return 0: Normal / 1: Detect
     */
    public int getExternalSensorStatus() {
        try {
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_EXTERNAL_SENSOR, 0);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[getExternalSensorStatus] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getExternalSensorStatus()");
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * External Sensor
     * @return 0: Normal / 1: Detect
     */
    public int sendAppLiveSignal() {
        try {
            ctrlWallpadJNI(true);
            int nResult = WpdInterfaceControl(IOCTL_APP_LIVE_SIGNAL, 1);
            ctrlWallpadJNI(false);
            Log.d(TAG, "[sendAppLiveSignal] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] sendAppLiveSignal()");
            e.printStackTrace();
            return -1;
        }
    }



}
