package com.casic.electric.detector.utils;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MT6761Gpio {

    private final String filePath = "/sys/devices/platform/1000b000.pinctrl/mt_gpio";
    private String pin;

    public void setGpioHigh() {
        if (pin.isEmpty()) {
            return;
        }
        setGpioMode(pin, "0");
        setGpioDir(pin, "1");
        setGpioOut(pin, "1");
    }

    public void setGpioLow() {
        if (pin.isEmpty()) {
            return;
        }
        setGpioMode(pin, "0");
        setGpioDir(pin, "1");
        setGpioOut(pin, "0");
    }

    public void setGpioInput() {
        if (pin.isEmpty()) {
            return;
        }
        setGpioMode(pin, "0");
        setGpioDir(pin, "0");
    }

    public void setNotPen() {
        setGpioPen(pin, "0");
    }

    public void setPullUp() {
        setGpioPen(pin, "1");
        setGpioPsel(pin, "1");
    }

    public void setPullDown() {
        setGpioPen(pin, "1");
        setGpioPsel(pin, "0");
    }

    public void setCtrlPin(String gpio) {
        pin = gpio;
    }

    public String readGpioTitle() {
        return read(filePath, "PIN:");
    }

    public String readGpioSingle() {
        int gpio = 0;
        if (pin.isEmpty()) {
            return "";
        }
        try {
            gpio = Integer.parseInt(pin);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        String str = String.format("%3d", gpio).replace(" ", "0");
        return read(filePath, pin + ":");
    }

    public String readGpioAll() {
        return read_all(filePath);
    }

    private void setGpioMode(String pin, String value) {
        nm_write("mode " + pin + " " + value);
    }

    private void setGpioDir(String pin, String value) {
        nm_write("dir " + pin + " " + value);
    }

    private void setGpioOut(String pin, String value) {
        nm_write("out " + pin + " " + value);
    }

    private void setGpioPen(String pin, String value) {   //1:浣胯兘涓婁笅鎷�   0:绂佹涓婁笅鎷�
        nm_write("pullen " + pin + " " + value);
    }

    private void setGpioPsel(String pin, String value) {  //1:璁剧疆涓婃媺    0:璁剧疆涓嬫媺
        nm_write("pullsel " + pin + " " + value);
    }

    private void nm_write(String value) {
        if (value.isEmpty()) {
            return;
        }
        FileWriter fw;
        try {
            fw = new FileWriter(filePath);
            fw.write(value);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public String read(String sys_path, String value) {
        if (value.isEmpty()) {
            return null;
        }
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("cat " + sys_path + "\n"); // 姝ゅ杩涜璇绘搷浣�
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            String line;
            while (null != (line = br.readLine())) {
                if (line.contains(value)) {
                    return line;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public String read_all(String sys_path) {
        StringBuilder value = new StringBuilder();
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("cat " + sys_path + "\n"); // 姝ゅ杩涜璇绘搷浣�
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;

            while (null != (line = br.readLine())) {
                value.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return value.toString();
    }
}
