package com.phlx.anchorcollect.rfid;

import android.os.SystemClock;

import com.phlx.anchorcollect.Configs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 上电操作
 */
class DeviceControl {


    void PowerOnDevice() {

        switch (Configs.cpuName) {

            case "MT6735":
                try {
                    //手持机 RM_7088_MT2
                    WriteFile("/sys/class/misc/mtgpio/pin", "-wdout94 1");
                    Thread.sleep(300);

                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
                break;

            case "MT6762V/CB":
                try {
                    //pad RM_WIOT61c_S1
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 24 1");
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "mode 24 0");
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "dir 24 1");

                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 55 1");
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "mode 55 0");
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "dir 55 1");

                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 173 1");
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "mode 173 0");
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "dir 173 1");
                    SystemClock.sleep(500);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

            case "MT6763V/B":
                try {
                    //pad RM_SD100T_MT1
                    WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "mode75 0");
                    WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out75 1");
                    WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out55 1");
                    Thread.sleep(300);
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
                break;
        }

        //RM_SD100T_MT1
//            WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out55 1");

    }

    void PowerOffDevice() {


        switch (Configs.cpuName) {

            case "MT6735":
                try {
                    //手持机 RM_7088_MT2
                    WriteFile("/sys/class/misc/mtgpio/pin", "-wdout94 0");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

            case "MT6762V/CB":
                try {
                    //RM_WIOT61c_S1
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 24 0");
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 55 0");
                    WriteFile("/sys/devices/platform/pinctrl/mt_gpio", "out 173 0");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

            case "MT6763V/B":
                try {
                    //pad RM_SD100T_MT1
                    WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out75 0");
                    WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out55 0");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;


            //RM_SD100T_MT1
//            WriteFile("/sys/bus/platform/drivers/mediatek-pinctrl/10005000.pinctrl/mt_gpio", "out55 0");

        }
    }

    private void WriteFile(String path, String value) throws IOException {
        File DeviceName = new File(path);
        if (DeviceName.exists()) {
            BufferedWriter CtrlFile = new BufferedWriter(new FileWriter(DeviceName, false));
            CtrlFile.write(value);
            CtrlFile.flush();
            CtrlFile.close();
        }
    }
}