import Gson.GsonFormatter;

import java.nio.file.Files;
import java.nio.file.Path;

public class ConfigStructure {
    String MySQLHost = "localhost";
    int MySQLPort = 3306;
    String MySQLAdminUser = "adminfis";
    String MySQLAdminPassword = "adminfis";
    String MySQLDatabase = "minifis";
    Users[] users = new Users[]{new Users("teknisi","bandara","admin"), new Users("user","user123456","user")};
    String WebHost = "0.0.0.0";
    int WebPort = 7000;
    int SocketIOPort = 7001;

    public static ConfigStructure LoadFromFile(Path path) {
        if (path != null ) {
            try {
                String content = Files.readString(path);
                return GsonFormatter.fromJson(content, ConfigStructure.class);
            } catch (Exception e) {
                {
                    System.out.println("Error loading Config from file " + path+", Exception: "+e.getMessage());
                }
            }
        }
        // default values
        return new ConfigStructure();
    }

    /**
     * Save this config to file
     * @param path path to save the file
     * @return true if success
     */
    public boolean SaveToFile(Path path){
        if (path!=null ){
            try{
                String content = GsonFormatter.toJson(this);
                Files.writeString(path, content);
                return true;
            } catch (Exception e){
                System.out.println("Error saving Config to file " + path+", Exception: "+e.getMessage());
            }
        } else System.out.println("SaveToFile failed, Path is empty");
        return false;
    }


}

class Users{
    String username;
    String password;
    String role;
    public Users(String username, String password, String role){
        this.username = username;
        this.password = password;
        this.role = role;
    }
}
