package com.tcl.factory.utils;

import android.os.RemoteException;
import android.os.SystemProperties;
import android.util.Log;

import com.tcl.factory.def.DEFS;

import vendor.tcl.gpio.V1_0.IGpio;

public class GpioHelper {
    private static final String TAG = "api";
    private IGpio myGpioHelper = null;

    public GpioHelper() {
        try {
            myGpioHelper = IGpio.getService(true);
        } catch (RemoteException e) {
            e.printStackTrace();
            Log.i(TAG, "bind gpio service error " + e.toString());
        }
    }

    public boolean setGPIO(int index, int value) {
        try {
            if (index == 482) {
                switch (value) {
                    case DEFS.COMMON_ON:
                        SystemProperties.set(DEFS.GPIO_FUNCTION, "true");
                        break;
                    case DEFS.COMMON_OFF:
                        SystemProperties.set(DEFS.GPIO_FUNCTION, "false");
                        break;
                    default:
                        SystemProperties.set(DEFS.GPIO_FUNCTION, "true");
                        break;
                }
            }
            return myGpioHelper.setGPIO(index, value);
        } catch (RemoteException e) {
            e.printStackTrace();
            Log.i(TAG, "bind gpio service error " + e.toString());
        }
        return false;
    }

    public void initGPIO() {
        int value = 0;
        if (getGPIO()) {
            value = 1;
        }
        setGPIO(482, value);
    }

    public boolean getGPIO() {
        return SystemProperties.getBoolean(DEFS.GPIO_FUNCTION, true);
    }
}
