package Config;

import id.co.gtc.erhacam.Detectors;
import lombok.Getter;
import org.tinylog.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.util.Properties;
import static Config.SomeCodes.*;

@SuppressWarnings("unused")
@Getter
public class ConfigFile {
    private  String AudioPhase1;
    private String AudioPhase2;
    private String AudioPhase3;
    private String AudioPhase4;
    private String AudioPhase5;

    private String CameraLeft90;
    private String CameraLeft45;
    private String CameraCenter;
    private String CameraRight45;
    private String CameraRight90;

    private CameraConfig ConfigLeft90 = new CameraConfig();
    private CameraConfig ConfigLeft45 = new CameraConfig();
    private CameraConfig ConfigCenter = new CameraConfig();
    private CameraConfig ConfigRight45 =new CameraConfig();
    private CameraConfig ConfigRight90 = new CameraConfig();

    private String FTPHost;
    private String FTPPort;
    private String FTPUser;
    private String FTPPass;
    private String FTPPath;

    private String PhotoDirectory;

    private @Getter double cascadeScaleFactor;
    private @Getter int cascadeMinNeighbors;
    private @Getter int cascadeMinSize;
    private @Getter int cascadeMaxSize;

    private @Getter boolean MirrorCamera = false;
    private @Getter boolean FlipCamera = false;

    private @Getter double SharpnessThreshold;

    private boolean needsave = false;

    public ConfigFile(){
        String ss = String.format("Current working directory in Java : %s", currentDirectory);
        Logger.info(ss);
        Load();
    }

    public void setMirrorCamera(boolean value){
        if (MirrorCamera != value){
            MirrorCamera = value;
            needsave = true;
        }
    }

    public void setFlipCamera(boolean value){
        if (FlipCamera != value){
            FlipCamera = value;
            needsave = true;
        }
    }

    public void setSharpnessThreshold(double value){
        if (SharpnessThreshold != value){
            SharpnessThreshold = value;
            needsave = true;
        }
    }

    public void setCascadeScaleFactor(double value){
        if (cascadeScaleFactor != value){
            cascadeScaleFactor = value;
            needsave = true;
        }
    }

    public void setCascadeMinNeighbors(int value){
        if (cascadeMinNeighbors != value){
            cascadeMinNeighbors = value;
            needsave = true;
        }
    }

    public void setCascadeMinSize(int value){
        if (cascadeMinSize != value){
            cascadeMinSize = value;
            needsave = true;
        }
    }

    public void setCascadeMaxSize(int value){
        if (cascadeMaxSize != value){
            cascadeMaxSize = value;
            needsave = true;
        }
    }

    public void SetPhotoDirectory(String path){
        if (ValidString(path)){
            if (!path.equals(PhotoDirectory)){
                File ff = new File(path);
                if (ff.isDirectory()){
                    Logger.info("Photo Directory changed from {} to {}", PhotoDirectory, path);
                    PhotoDirectory = path;
                    needsave = true;
                }
            }
        }
    }

    public void SetAudioPhase1(String path){
        if (ValidString(path)){
            if (!path.equals(AudioPhase1)){
                Logger.info("Audio Phase 1 changed from {} to {}", AudioPhase1, path);
                AudioPhase1 = path;
                needsave = true;
            }
        }
    }

    public void SetAudioPhase2(String path){
        if (ValidString(path)){
            if (!path.equals(AudioPhase2)){
                Logger.info("Audio Phase 2 changed from {} to {}", AudioPhase2, path);
                AudioPhase2 = path;
                needsave = true;
            }
        }
    }

    public void SetAudioPhase3(String path){
        if (ValidString(path)){
            if (!path.equals(AudioPhase3)){
                Logger.info("Audio Phase 3 changed from {} to {}", AudioPhase3, path);
                AudioPhase3 = path;
                needsave = true;
            }
        }
    }

    public void SetAudioPhase4(String path){
        if (ValidString(path)){
            if (!path.equals(AudioPhase4)){
                Logger.info("Audio Phase 4 changed from {} to {}", AudioPhase4, path);
                AudioPhase4 = path;
                needsave = true;
            }
        }
    }

    public void SetAudioPhase5(String path){
        if (ValidString(path)){
            if (!path.equals(AudioPhase5)){
                Logger.info("Audio Phase 5 changed from {} to {}", AudioPhase5, path);
                AudioPhase5 = path;
                needsave = true;
            }
        }
    }

    /**
     * Set Camera Path for Left 90 Degree
     * @param path Path to Camera, or empty string to disable
     */
    public void SetCameraLeft90(String path){
        if (path==null) path="";
        if (!path.equals(CameraLeft90)){
            Logger.info("Camera Left 90 Degree changed from {} to {}", CameraLeft90, path);
            CameraLeft90 = path;
            needsave = true;
        }
    }

    /**
     * Set Camera Path for Left 45 Degree
     * @param path Path to Camera, or empty string to disable
     */
    public void SetCameraLeft45(String path){
        if (path==null) path="";
        if (!path.equals(CameraLeft45)){
            Logger.info("Camera Left 45 Degree changed from {} to {}", CameraLeft45, path);
            CameraLeft45 = path;
            needsave = true;
        }
    }

    /**
     * Set Camera Path for Center
     * @param path Path to Camera, or empty string to disable
     */
    public void SetCameraCenter(String path){
        if (path==null) path="";
        if (!path.equals(CameraCenter)){
            Logger.info("Camera Center changed from {} to {}", CameraCenter, path);
            CameraCenter = path;
            needsave = true;
        }
    }

    /**
     * Set Camera Path for Right 45 Degree
     * @param path Path to Camera, or empty string to disable
     */
    public void SetCameraRight45(String path){
        if (path==null) path="";
        if (!path.equals(CameraRight45)){
            Logger.info("Camera Right 45 Degree changed from {} to {}", CameraRight45, path);
            CameraRight45 = path;
            needsave = true;
        }
    }

    /**
     * Set Camera Path for Right 90 Degree
     * @param path Path to Camera, or empty string to disable
     */
    public void SetCameraRight90(String path){
        if (path==null) path="";
        if (!path.equals(CameraRight90)){
            Logger.info("Camera Right 90 Degree changed from {} to {}", CameraRight90, path);
            CameraRight90 = path;
            needsave = true;
        }
    }

    public void SetFTPHost(String host){
        if (ValidString(host)){
            if (!host.equals(FTPHost)){
                Logger.info("FTP Host changed from {} to {}", FTPHost, host);
                FTPHost = host;
                needsave = true;

            }
        }
    }

    public void SetFTPPort(String port){
        if (ValidString(port)){
            if (!port.equals(FTPPort)){
                if (ValidPortNumber(toInt(port))){
                    Logger.info("FTP Port changed from {} to {}", FTPPort, port);
                    FTPPort = port;
                    needsave = true;
                }
            }
        }
    }

    public void SetFTPUser(String user){
        if (ValidString(user)){
            if (!user.equals(FTPUser)){
                Logger.info("FTP User changed from {} to {}", FTPUser, user);
                FTPUser = user;
                needsave = true;
            }
        }
    }

    public void SetFTPPass(String pass){
        if (ValidString(pass)){
            if (!pass.equals(FTPPass)){
                Logger.info("FTP Password changed from {} to {}", FTPPass, pass);
                FTPPass = pass;
                needsave = true;
            }
        }
    }

    public void SetFTPPath(String path){
        if (ValidString(path)){
            if (!path.equals(FTPPath)){
                Logger.info("FTP Path changed from {} to {}", FTPPath, path);
                FTPPath = path;
                needsave = true;
            }
        }
    }

    public void setBrightness(CameraConfigEnum cc, double value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.Brightness != value){

            conf.Brightness = value;
            needsave = true;
        }
    }

    public double getBrightness(CameraConfigEnum cc){
        return switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90.Brightness;
            case CameraConfigLeft45 -> ConfigLeft45.Brightness;
            case CameraConfigCenter -> ConfigCenter.Brightness;
            case CameraConfigRight45 -> ConfigRight45.Brightness;
            case CameraConfigRight90 -> ConfigRight90.Brightness;
        };
    }

    public void setContrast(CameraConfigEnum cc, double value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.Contrast != value){
            conf.Contrast = value;
            needsave = true;
        }
    }

    public double getContrast(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.Contrast;
            case CameraConfigLeft45 -> ConfigLeft45.Contrast;
            case CameraConfigCenter -> ConfigCenter.Contrast;
            case CameraConfigRight45 -> ConfigRight45.Contrast;
            case CameraConfigRight90 -> ConfigRight90.Contrast;
        };
    }

    public void setSaturation(CameraConfigEnum cc, double value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.Saturation != value){
            conf.Saturation = value;
            needsave = true;
        }
    }

    public double getSaturation(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.Saturation;
            case CameraConfigLeft45 -> ConfigLeft45.Saturation;
            case CameraConfigCenter -> ConfigCenter.Saturation;
            case CameraConfigRight45 -> ConfigRight45.Saturation;
            case CameraConfigRight90 -> ConfigRight90.Saturation;
        };
    }

    public void setHue(CameraConfigEnum cc, double value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.Hue != value){
            conf.Hue = value;
            needsave = true;
        }
    }

    public double getHue(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.Hue;
            case CameraConfigLeft45 -> ConfigLeft45.Hue;
            case CameraConfigCenter -> ConfigCenter.Hue;
            case CameraConfigRight45 -> ConfigRight45.Hue;
            case CameraConfigRight90 -> ConfigRight90.Hue;
        };
    }

    public void setGain(CameraConfigEnum cc, double value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.Gain != value){
            conf.Gain = value;
            needsave = true;
        }
    }

    public double getGain(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.Gain;
            case CameraConfigLeft45 -> ConfigLeft45.Gain;
            case CameraConfigCenter -> ConfigCenter.Gain;
            case CameraConfigRight45 -> ConfigRight45.Gain;
            case CameraConfigRight90 -> ConfigRight90.Gain;
        };
    }

    public void setExposure(CameraConfigEnum cc, double value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.Exposure != value){
            conf.Exposure = value;
            needsave = true;
        }
    }

    public double getExposure(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.Exposure;
            case CameraConfigLeft45 -> ConfigLeft45.Exposure;
            case CameraConfigCenter -> ConfigCenter.Exposure;
            case CameraConfigRight45 -> ConfigRight45.Exposure;
            case CameraConfigRight90 -> ConfigRight90.Exposure;
        };
    }

    public void setSharpness(CameraConfigEnum cc, double value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.Sharpness != value){
            conf.Sharpness = value;
            needsave = true;
        }
    }

    public double getSharpness(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.Sharpness;
            case CameraConfigLeft45 -> ConfigLeft45.Sharpness;
            case CameraConfigCenter -> ConfigCenter.Sharpness;
            case CameraConfigRight45 -> ConfigRight45.Sharpness;
            case CameraConfigRight90 -> ConfigRight90.Sharpness;
        };
    }

    public void setGamma(CameraConfigEnum cc, double value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.Gamma != value){
            conf.Gamma = value;
            needsave = true;
        }
    }

    public double getGamma(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.Gamma;
            case CameraConfigLeft45 -> ConfigLeft45.Gamma;
            case CameraConfigCenter -> ConfigCenter.Gamma;
            case CameraConfigRight45 -> ConfigRight45.Gamma;
            case CameraConfigRight90 -> ConfigRight90.Gamma;
        };
    }

    public void setAutoExposure(CameraConfigEnum cc, boolean value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.AutoExposure != value){
            conf.AutoExposure = value;
            needsave = true;
        }
    }

    public boolean getAutoExposure(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.AutoExposure;
            case CameraConfigLeft45 -> ConfigLeft45.AutoExposure;
            case CameraConfigCenter -> ConfigCenter.AutoExposure;
            case CameraConfigRight45 -> ConfigRight45.AutoExposure;
            case CameraConfigRight90 -> ConfigRight90.AutoExposure;
        };
    }

    public void setAutoFocus(CameraConfigEnum cc, boolean value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.AutoFocus != value){
            conf.AutoFocus = value;
            needsave = true;
        }
    }

    public boolean getAutoFocus(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.AutoFocus;
            case CameraConfigLeft45 -> ConfigLeft45.AutoFocus;
            case CameraConfigCenter -> ConfigCenter.AutoFocus;
            case CameraConfigRight45 -> ConfigRight45.AutoFocus;
            case CameraConfigRight90 -> ConfigRight90.AutoFocus;
        };
    }

    public void setAutoWhiteBalance(CameraConfigEnum cc, boolean value){
        CameraConfig conf = switch (cc) {
            case CameraConfigLeft90 -> ConfigLeft90;
            case CameraConfigLeft45 -> ConfigLeft45;
            case CameraConfigCenter -> ConfigCenter;
            case CameraConfigRight45 -> ConfigRight45;
            case CameraConfigRight90 -> ConfigRight90;
        };
        if (conf.AutoWhiteBalance != value){
            conf.AutoWhiteBalance = value;
            needsave = true;
        }
    }

    public boolean getAutoWhiteBalance(CameraConfigEnum cc){
        return switch (cc){
            case CameraConfigLeft90 -> ConfigLeft90.AutoWhiteBalance;
            case CameraConfigLeft45 -> ConfigLeft45.AutoWhiteBalance;
            case CameraConfigCenter -> ConfigCenter.AutoWhiteBalance;
            case CameraConfigRight45 -> ConfigRight45.AutoWhiteBalance;
            case CameraConfigRight90 -> ConfigRight90.AutoWhiteBalance;
        };
    }



    private void Load(){
        File ff = Path.of(currentDirectory, "config.properties").toFile();
        if (ff.isFile()){
            String ss = String.format("Load config file at %s", ff.getPath());
            Logger.info(ss);
            try{
                Properties prop = new Properties();
                FileInputStream FIS = new FileInputStream(ff.getPath());
                prop.load(FIS);
                boolean allcorrect = prop.getProperty("AudioPhase1") != null;
                if (prop.getProperty("AudioPhase2") == null) allcorrect = false;
                if (prop.getProperty("AudioPhase3") == null) allcorrect = false;
                if (prop.getProperty("AudioPhase4") == null) allcorrect = false;
                if (prop.getProperty("AudioPhase5") == null) allcorrect = false;
                if (prop.getProperty("CameraLeft90") == null) allcorrect = false;
                if (prop.getProperty("CameraLeft45") == null) allcorrect = false;
                if (prop.getProperty("CameraCenter") == null) allcorrect = false;
                if (prop.getProperty("CameraRight45") == null) allcorrect = false;
                if (prop.getProperty("CameraRight90") == null) allcorrect = false;
                if (prop.getProperty("FTPHost") == null) allcorrect = false;
                if (prop.getProperty("FTPPort") == null) allcorrect = false;
                if (prop.getProperty("FTPUser") == null) allcorrect = false;
                if (prop.getProperty("FTPPass") == null) allcorrect = false;
                if (prop.getProperty("FTPPath") == null) allcorrect = false;
                if (prop.getProperty("PhotoDirectory") == null) allcorrect = false;
                if (prop.getProperty("MirrorCamera") == null) allcorrect = false;
                if (prop.getProperty("FlipCamera") == null) allcorrect = false;
                if (prop.getProperty("SharpnessThreshold") == null) allcorrect = false;


                if (prop.getProperty(CameraConfigEnum.CameraConfigLeft90.toString()) == null) allcorrect = false;
                if (prop.getProperty(CameraConfigEnum.CameraConfigLeft45.toString()) == null) allcorrect = false;
                if (prop.getProperty(CameraConfigEnum.CameraConfigCenter.toString()) == null) allcorrect = false;
                if (prop.getProperty(CameraConfigEnum.CameraConfigRight45.toString()) == null) allcorrect = false;
                if (prop.getProperty(CameraConfigEnum.CameraConfigRight90.toString()) == null) allcorrect = false;

                if (prop.getProperty("cascadeScaleFactor") == null) allcorrect = false;
                if (prop.getProperty("cascadeMinNeighbors") == null) allcorrect = false;
                if (prop.getProperty("cascadeMinSize") == null) allcorrect = false;
                if (prop.getProperty("cascadeMaxSize") == null) allcorrect = false;

                if (allcorrect){
                    AudioPhase1 = prop.getProperty("AudioPhase1");
                    AudioPhase2 = prop.getProperty("AudioPhase2");
                    AudioPhase3 = prop.getProperty("AudioPhase3");
                    AudioPhase4 = prop.getProperty("AudioPhase4");
                    AudioPhase5 = prop.getProperty("AudioPhase5");

                    CameraLeft90 = prop.getProperty("CameraLeft90");
                    CameraLeft45 = prop.getProperty("CameraLeft45");
                    CameraCenter = prop.getProperty("CameraCenter");
                    CameraRight45 = prop.getProperty("CameraRight45");
                    CameraRight90 = prop.getProperty("CameraRight90");

                    FTPHost = prop.getProperty("FTPHost");
                    FTPPort = prop.getProperty("FTPPort");
                    FTPUser = prop.getProperty("FTPUser");
                    FTPPass = prop.getProperty("FTPPass");
                    FTPPath = prop.getProperty("FTPPath");
                    PhotoDirectory = prop.getProperty("PhotoDirectory");

                    ConfigLeft90 = gson.fromJson(prop.getProperty(CameraConfigEnum.CameraConfigLeft90.toString()), CameraConfig.class);
                    ConfigLeft45 = gson.fromJson(prop.getProperty(CameraConfigEnum.CameraConfigLeft45.toString()), CameraConfig.class);
                    ConfigCenter = gson.fromJson(prop.getProperty(CameraConfigEnum.CameraConfigCenter.toString()), CameraConfig.class);
                    ConfigRight45 = gson.fromJson(prop.getProperty(CameraConfigEnum.CameraConfigRight45.toString()), CameraConfig.class);
                    ConfigRight90 = gson.fromJson(prop.getProperty(CameraConfigEnum.CameraConfigRight90.toString()), CameraConfig.class);

                    cascadeScaleFactor = toDouble(prop.getProperty("cascadeScaleFactor"));
                    cascadeMinNeighbors = toInt(prop.getProperty("cascadeMinNeighbors"));
                    cascadeMinSize = toInt(prop.getProperty("cascadeMinSize"));
                    cascadeMaxSize = toInt(prop.getProperty("cascadeMaxSize"));

                    Detectors.setFaceMaxSize(cascadeMaxSize);
                    Detectors.setFaceMinSize(cascadeMinSize);
                    Detectors.setScaleFactor(cascadeScaleFactor);

                    MirrorCamera = toBoolean(prop.getProperty("MirrorCamera"));
                    FlipCamera = toBoolean(prop.getProperty("FlipCamera"));

                    SharpnessThreshold = toDouble(prop.getProperty("SharpnessThreshold"));

                    Logger.info("Config Loaded");
                    MakeDirectories();
                    return;
                } else {
                    Logger.info("Config File Not Correct, Creating Default");
                }
            } catch (Exception e){
                Logger.error("Error Load Config: " + e.getMessage()+", Creating Default");
            }
        } else {
            Logger.info("Config File Not Found, Creating Default");
        }
        CreateDefault();
    }

    private double toDouble(String cascadeScaleFactor) {
        try{
            return Double.parseDouble(cascadeScaleFactor);
        } catch (Exception e){
            return 1.1;
        }
    }

    private void CreateDefault(){
        AudioPhase1 = Path.of(currentDirectory, "audio", "phase1.mp3").toString();
        AudioPhase2 = Path.of(currentDirectory, "audio", "phase2.mp3").toString();
        AudioPhase3 = Path.of(currentDirectory, "audio", "phase3.mp3").toString();
        AudioPhase4 = Path.of(currentDirectory, "audio", "phase4.mp3").toString();
        AudioPhase5 = Path.of(currentDirectory, "audio", "phase5.mp3").toString();
        CameraLeft90 = "";
        CameraLeft45 = "";
        CameraCenter = "";
        CameraRight45 = "";
        CameraRight90 = "";
        FTPHost = "192.168.10.2";
        FTPPort = "21";
        FTPUser = "user";
        FTPPass = "password";
        FTPPath = "/";
        PhotoDirectory = currentDirectory;
        SetDefaultCameraConfig(ConfigLeft90);
        SetDefaultCameraConfig(ConfigLeft45);
        SetDefaultCameraConfig(ConfigCenter);
        SetDefaultCameraConfig(ConfigRight45);
        SetDefaultCameraConfig(ConfigRight90);
        cascadeScaleFactor = 1.1;
        cascadeMinNeighbors = 3;
        cascadeMinSize = 250;
        cascadeMaxSize = 500;
        Detectors.setFaceMaxSize(cascadeMaxSize);
        Detectors.setFaceMinSize(cascadeMinSize);
        Detectors.setScaleFactor(cascadeScaleFactor);
        MirrorCamera = false;
        FlipCamera = false;
        SharpnessThreshold = 300.0;
        Logger.info("Default Config Created");
        needsave = true;
        Save();
        MakeDirectories();
    }

    private void SetDefaultCameraConfig(CameraConfig cc){
        if (cc!=null){
            cc.AutoExposure = true;
            cc.AutoFocus = true;
            cc.AutoWhiteBalance = true;
            cc.Brightness = 0;
            cc.Contrast = 0;
            cc.Exposure = 0;
            cc.Gain = 0;
            cc.Saturation = 0;
            cc.Hue = 0;
            cc.Gamma = 0;
            cc.Sharpness = 0;
        }
    }

    public void Save(){
        if (!needsave) return;
        needsave = false;
        Properties prop = new Properties();
        prop.setProperty("AudioPhase1", AudioPhase1);
        prop.setProperty("AudioPhase2", AudioPhase2);
        prop.setProperty("AudioPhase3", AudioPhase3);
        prop.setProperty("AudioPhase4", AudioPhase4);
        prop.setProperty("AudioPhase5", AudioPhase5);

        prop.setProperty("CameraLeft90", CameraLeft90);
        prop.setProperty("CameraLeft45", CameraLeft45);
        prop.setProperty("CameraCenter", CameraCenter);
        prop.setProperty("CameraRight45", CameraRight45);
        prop.setProperty("CameraRight90", CameraRight90);

        prop.setProperty("FTPHost", FTPHost);
        prop.setProperty("FTPPort", FTPPort);
        prop.setProperty("FTPUser",FTPUser);
        prop.setProperty("FTPPass", FTPPass);
        prop.setProperty("FTPPath", FTPPath);
        prop.setProperty("PhotoDirectory", PhotoDirectory);

        prop.setProperty(CameraConfigEnum.CameraConfigLeft90.toString(), gson.toJson(ConfigLeft90));
        prop.setProperty(CameraConfigEnum.CameraConfigLeft45.toString(), gson.toJson(ConfigLeft45));
        prop.setProperty(CameraConfigEnum.CameraConfigCenter.toString(), gson.toJson(ConfigCenter));
        prop.setProperty(CameraConfigEnum.CameraConfigRight45.toString(), gson.toJson(ConfigRight45));
        prop.setProperty(CameraConfigEnum.CameraConfigRight90.toString(), gson.toJson(ConfigRight90));

        prop.setProperty("cascadeScaleFactor", String.valueOf(cascadeScaleFactor));
        prop.setProperty("cascadeMinNeighbors", String.valueOf(cascadeMinNeighbors));
        prop.setProperty("cascadeMinSize", String.valueOf(cascadeMinSize));
        prop.setProperty("cascadeMaxSize", String.valueOf(cascadeMaxSize));

        prop.setProperty("MirrorCamera", String.valueOf(MirrorCamera));
        prop.setProperty("FlipCamera", String.valueOf(FlipCamera));
        prop.setProperty("SharpnessThreshold", String.valueOf(SharpnessThreshold));

        try{
            prop.store(new FileOutputStream(Path.of(currentDirectory, "config.properties").toString()), null);
            Logger.info("Config Saved");
        } catch (Exception e){
            Logger.error("Error Save Config: " + e.getMessage());
        }
    }

    private @Getter String FullQualityDirectory;
    private @Getter String FullQualityCropDirectory;
    private @Getter String CompressedDirectory;
    private @Getter String CompressedCropDirectory;
    private @Getter String ThumbsDirectory;

    private void MakeDirectories(){
        FullQualityDirectory = Path.of(PhotoDirectory,"FullQuality").toString();
        FullQualityCropDirectory = Path.of(PhotoDirectory,"FullQualityCrop").toString();
        CompressedDirectory = Path.of(PhotoDirectory,"Compressed").toString();
        CompressedCropDirectory = Path.of(PhotoDirectory,"CompressedCrop").toString();
        ThumbsDirectory = Path.of(PhotoDirectory,"thumbs").toString();
        MakeDirectory(FullQualityDirectory);
        MakeDirectory(FullQualityCropDirectory);
        MakeDirectory(CompressedDirectory);
        MakeDirectory(CompressedCropDirectory);
        MakeDirectory(ThumbsDirectory);
    }
}
