package _VisualDVM;
import Common.MainModule_;
import Common.Utils.Vector_;
import _VisualDVM.ComponentsServer.Component.Sapfor.Sapfor;
import _VisualDVM.ComponentsServer.UserAccount.UserAccount;
import _VisualDVM.GlobalData.GlobalDatabase;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.Files.DBProjectFile;
import _VisualDVM.ProjectData.Project.db_project_info;
import _VisualDVM.ProjectData.ProjectView;
import _VisualDVM.ProjectData.SapforData.Functions.FuncInfo;
import _VisualDVM.Visual.MainUI;

import javax.swing.tree.DefaultMutableTreeNode;
import java.io.File;
import java.util.Vector;
public class MainModule extends MainModule_<GlobalDatabase, MainUI> {
    UserAccount account = null;
    //--
    Vector<PassCode> accountRoleDependentPasses = new Vector_<>(
            //--
            PassCode.DownloadAllBugReportsArchives,
            //--
            PassCode.CompileServerSapfor,
            PassCode.DeleteServerSapfor,
            //
            PassCode.ShutdownComponentsServer,
            PassCode.StartComponentsServer,
            PassCode.PublishComponentsServer,
            PassCode.ComponentsServerBackUp,
            //
            PassCode.ShutdownTestingServer,
            PassCode.StartTestingServer,
            PassCode.PublishTestingServer,
            PassCode.TestingServerBackUp,
            PassCode.CreateTestsGroupFromSelectedVersions,
            PassCode.RefreshDVMPackageResults,
            PassCode.ConvertCorrectnessTests
    );
    public MainModule() throws Exception {
        super(GlobalDatabase.class, MainUI.class);
        account = new UserAccount(Global.normalProperties.Name, Global.normalProperties.Email);
    }
    @Override
    public Class getPassCodesEnum() {
        return PassCode.class;
    }
    @Override
    public String getAllPassesClassPrefix() {
        return "_VisualDVM.Passes.All.";
    }
    //--
    public UserAccount getAccount() {
        return account;
    }
    public void setAccount(UserAccount account_in) {
        account = account_in;
    }
    public boolean HasProject() {
        return get(Current.Project) != null;
    }
    public db_project_info getProject() {
        return (db_project_info) get(Current.Project);
    }
    //--
    public boolean HasFile() {
        return get(Current.File) != null;
    }
    public boolean HasRemoteFile() {
        return get(Current.RemoteFile) != null;
    }
    public boolean HasProjectView() {
        return get(Current.ProjectView) != null;
    }
    public DBProjectFile getFile() {
        return (DBProjectFile) get(Current.File);
    }
    public db_project_info getRoot() {
        return (db_project_info) get(Current.Root);
    }
    public boolean HasRoot() {
        return get(Current.Root) != null;
    }
    public db_project_info getVersion() {
        return (db_project_info) get(Current.Version);
    }
    public RemoteFile getRemoteFile() {
        return (RemoteFile) get(Current.RemoteFile);
    }
    public Sapfor getSapfor() {
        return (Sapfor) get(Current.Sapfor);
    }
    public boolean HasVersion() {
        return get(Current.Version) != null;
    }
    public RemoteFile getComponentServerBackup() {
        return (RemoteFile) get(Current.ComponentServerBackup);
    }
    public boolean HasComponentServerBackup() {
        return get(Current.ComponentServerBackup) != null;
    }
    public DefaultMutableTreeNode getProjectNode() {
        return (DefaultMutableTreeNode) get(Current.ProjectNode);
    }
    public DefaultMutableTreeNode getProjectCurrentParentNode() {
        DefaultMutableTreeNode node = getProjectNode();
        //если в дереве еще никто не выделялся, берем корень.
        if (node == null)
            return getProject().filesTreeRoot;
        return (node.getUserObject() instanceof DBProjectFile) ? (DefaultMutableTreeNode) node.getParent() : node;
    }
    public File getSelectedDirectory() {
        return (File) get(Current.SelectedDirectory);
    }
    public DBProjectFile getSelectedFile() {
        return (DBProjectFile) get(Current.SelectedFile);
    }
    public boolean HasFunction() {
        return get(Current.Function) != null;
    }
    public boolean HasSelectedFunction() {
        return get(Current.SelectedFunction) != null;
    }
    public FuncInfo getFunction() {
        return (FuncInfo) get(Current.Function);
    }
    public FuncInfo getSelectionFunction() {
        return (FuncInfo) get(Current.SelectedFunction);
    }
    public ProjectView getProjectView() {
        return (ProjectView) get(Current.ProjectView);
    }
    public void DropCurrentFile() {
        set(Current.File, null);
        set(Current.FileGraphElement, null);
    }
    public void DropCurrentProject() {
        set(Current.Project, null);
        set(Current.File, null);
        set(Current.Function, null);
        set(Current.SelectedFunction, null);
        set(Current.ProjectNode, null);
        set(Current.SelectedFile, null);
        set(Current.SelectedDirectory, null);
    }
    public void SetUserPassesAccess() {
        for (PassCode code : accountRoleDependentPasses)
            getPass(code).setControlsVisible(false);
    }
    public void SetStudentPassesAccess() {
        SetUserPassesAccess();
    }
    public void SetDeveloperPassesAccess() {
        SetUserPassesAccess();
        getPass(PassCode.CreateTestsGroupFromSelectedVersions).setControlsVisible(true);
        getPass(PassCode.DownloadAllBugReportsArchives).setControlsVisible(true);
    }
    public void SetAdminPassesAccess() {
        for (PassCode code : accountRoleDependentPasses)
            getPass(code).setControlsVisible(true);
    }
    //--
    @Override
    public int getFirstAccessPassesCount() {
        return Global.normalProperties.FastAccessPassesCount;
    }
    @Override
    public boolean confirmPassesStart() {
        return Global.normalProperties.ConfirmPassesStart;
    }
    @Override
    public boolean confirmPassesDone() {
        return Global.normalProperties.ShowPassesDone;
    }
    @Override
    public boolean focusPassesResult() {
        return Global.normalProperties.FocusPassesResult;
    }
}
