import java.io.*;
import java.util.Objects;

public class GeneralCode {
    private static final String GPIO_PATH = "/sys/class/gpio/";

    /**
    check is the OS is Linux or not
     */
    public static  boolean OsIsLinux(){
        boolean result = false;
        String OsResult  = System.getProperty("os.name").toLowerCase();
        if (OsResult.contains("linux")) {
            result = true;
        }
        return  result;
    }

    /**
     * checking if pin is already exported in /sys/class/gpio
     * @param Pin --> pin that you want to check
     * @return true if pin exported
     */
    public static boolean GpioIsExported(int Pin){
        String fileName = "/sys/class/gpio/gpio"+Pin;
        File file = new File(fileName);
        return file.exists();
    }

    /**
     * Export file, is a must before set In or Out.
     * @param Pin that you want to set
     * @return true if success
     */
    public static boolean ExportGpio(int Pin){
        return writeToFile(GPIO_PATH + "export", String.valueOf(Pin));
    }

    public static boolean UnExportGpio(int Pin){
        return  writeToFile(GPIO_PATH+"unexport",String.valueOf(Pin));
    }

    /**
     *
     * @param Pin pin that you want to set
     * @param direction true if out, false if in
     * @return True if success
     */
    public static boolean SetGpioDirection(int Pin, boolean direction){
        String val;
        if(direction){
            val = "out";
        }else {val="in";}
        return writeToFile(GPIO_PATH+"gpio"+Pin+"/direction",val);
    }

    /**
     * Set value to file,
     * @param Pin pin that you want to set value
     * @param Value if true-> HIGH false-> LOW
     * @return true if success
     */
    public static boolean SetGpioValue(int Pin,boolean Value){
        String valNum;
        if (Value){
            valNum="1";
        }else {valNum="0";}
        return writeToFile(GPIO_PATH + "gpio" + Pin + "/value", valNum);
    }

    /**
     * Get value from file
     * @param Pin that you want to get value
     * @return 1/0
     */
    public static int GetGpioValue(int Pin){
        String filePath = GPIO_PATH+"gpio"+Pin+"/value";
        return Integer.parseInt(Objects.requireNonNull(readFromFile(filePath)));
    }

    // Helper method to write to a file
    private static boolean writeToFile(String filePath, String value) {
//        System.out.println("writing value : "+filePath+" " + value);
        boolean result = false;
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(value);
            result = true;
        } catch (IOException e) {
            System.err.println("Error writing to " + filePath + ": " + e.getMessage());
        }
        return result;
    }

    // Helper method to read from a file (if needed)
    private static String readFromFile(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            return reader.readLine();
        } catch (IOException e) {
            System.err.println("Error reading from " + filePath + ": " + e.getMessage());
            return null;
        }
    }
}
