package Visual_DVM_2021.UI.Main;
import Common.Constants;
import Common.Current;
import Common.Global;
import GlobalData.Credentials.Credentials;
import Visual_DVM_2021.UI.Interface.CredentialsWindow;
import Visual_DVM_2021.UI.Interface.FormWithSplitters;

import javax.swing.*;
public class CredentialsForm implements CredentialsWindow, FormWithSplitters {
    public JSplitPane SC18;
    public JSplitPane SC19;
    private JPanel machinesPanel;
    private JPanel usersPanel;
    private JPanel compilersPanel;
    private JPanel content;
    public CredentialsForm(){
        LoadSplitters();
        Global.db.machines.mountUI(machinesPanel);
        Global.db.users.mountUI(usersPanel);
        Global.db.compilers.mountUI(compilersPanel);
    }
    @Override
    public JPanel getContent() {
        return content;
    }
    @Override
    public void ShowAll() {
        Global.db.machines.ShowUI();
        RestoreLastCredentials();
    }
    public void RestoreLastCredentials() {
        Credentials credentials = (Credentials) Current.get(Current.Credentials);
        if (credentials.machine_id != Constants.Nan) {
            if (Global.db.machines.containsKey(credentials.machine_id)) {
                Global.db.machines.ShowUI(credentials.machine_id);
                if (Global.db.users.containsKey(credentials.user_id)) {
                    Global.db.users.ShowUI(credentials.user_id);
                } else {
                    credentials.user_id = Constants.Nan;
                    Global.db.UpdateCredentials();
                }
                if (Global.db.compilers.containsKey(credentials.compiler_id)) {
                    Global.db.compilers.ShowUI(credentials.compiler_id);
                } else {
                    credentials.compiler_id = Constants.Nan;
                    Global.db.UpdateCredentials();
                }
                //-
                if (Global.db.makefiles.containsKey(credentials.makefile_id)) {
                    Global.db.makefiles.ShowUI(credentials.makefile_id);
                } else {
                    credentials.makefile_id = Constants.Nan;
                    Global.db.UpdateCredentials();
                }
                //-
                if (Global.db.runConfigurations.containsKey(credentials.runconfiguration_id)) {
                    Global.db.runConfigurations.ShowUI(credentials.runconfiguration_id);
                } else {
                    credentials.runconfiguration_id = Constants.Nan;
                    Global.db.UpdateCredentials();
                }
            } else {
                credentials.machine_id = Constants.Nan;
                credentials.user_id = Constants.Nan;
                credentials.compiler_id = Constants.Nan;
                credentials.remotesapfor_id = Constants.Nan;
                credentials.makefile_id = Constants.Nan;
                credentials.runconfiguration_id = Constants.Nan;
                Global.db.UpdateCredentials();
            }
        }
    }
}
