package _VisualDVM.GlobalData.RunConfiguration.UI;
import Common.CommonConstants;
import Common.MainModule_;
import Common.Visual.UI;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Compiler.Compiler;
import _VisualDVM.GlobalData.Compiler.CompilerType;
import _VisualDVM.GlobalData.Machine.Machine;
import _VisualDVM.GlobalData.RunConfiguration.RunConfiguration;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RunConfigurationDialog extends DBObjectDialog<RunConfiguration, RunConfigurationFields> {
    public RunConfigurationDialog() {
        super(RunConfigurationFields.class);
    }
    @Override
    public void fillFields() {
        for (Compiler compiler : (Global.mainModule.getDb()).compilers.Data.values()) {
            if (Global.mainModule.getDb().compilers.getUI().isObjectVisible(compiler)
                    && compiler.type.equals(CompilerType.dvm))
                fields.cbLauncherCall.addItem(compiler);
        }
        UI.TrySelect_s(fields.cbLauncherCall, Result.LauncherCall);
        UI.TrySelect(fields.cbLaunchOptions, Result.LauncherOptions);
        fields.tfArgs.setText(Result.args);
        fields.cbLauncherCall.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (fields.cbLauncherCall.getSelectedItem() instanceof Compiler) {
                    UI.TrySelect(fields.cbLaunchOptions, "run");
                }
            }
        });
        fields.cbCube.setSelected(Result.isCube());
        //------------------------------------------->>>
        fields.sMaxDim.setModel(new SpinnerNumberModel(
                Result.dim,
                0, RunConfiguration.maxProc, 1));
        fields.minMatrixPanel.add(fields.minMatrixBar = new MatrixBar(Result.minMatrix));
        fields.maxMatrixPanel.add(fields.maxMatrixBar = new MatrixBar(Result.maxMatrix));
        //------------------------------------------->>>
        if (!edit)
            fields.sMaxDim.setValue(Global.mainModule.getProject().maxdim);
    }
    @Override
    public void ProcessResult() {
        Result.machine_id = MainModule_.instance.getDb().getTable(Machine.class).getUI().getCurrent().id;
        Result.LauncherCall = fields.cbLauncherCall.getSelectedItem().toString();
        Result.LauncherOptions = (String) fields.cbLaunchOptions.getSelectedItem();
        if (fields.cbLauncherCall.getSelectedItem() instanceof Compiler) {
            Result.compiler_id = ((Compiler) (fields.cbLauncherCall.getSelectedItem())).id;
        } else Result.compiler_id = CommonConstants.Nan;
        //-
        Result.dim = (int) fields.sMaxDim.getValue();
        Result.minMatrix = fields.minMatrixBar.pack(Result.dim);
        Result.maxMatrix = fields.maxMatrixBar.pack(Result.dim);
        //-
        Result.args = fields.tfArgs.getText();
        Result.cube = fields.cbCube.isSelected() ? 1 : 0;
    }
    @Override
    public void validateFields() {
        String launcher_call = fields.cbLauncherCall.getSelectedItem().toString();
        String launcher_options = (String) fields.cbLaunchOptions.getSelectedItem();
        if (launcher_call.isEmpty() && !launcher_options.isEmpty())
            Log.Writeln_("Непустые опции запуска допускаются только для DVM системы или MPI");
        if (fields.cbLauncherCall.getSelectedItem() instanceof Compiler) {
            Compiler compiler = (Compiler) (fields.cbLauncherCall.getSelectedItem());
            switch (compiler.type) {
                case dvm:
                case mpi:
                    int dim_ = (int) fields.sMaxDim.getValue();
                    RunConfiguration.validateMatrixes(
                            fields.minMatrixBar.pack(dim_),
                            fields.maxMatrixBar.pack(dim_),
                            dim_,
                            fields.cbCube.isSelected(),
                            Log
                    );
                    break;
            }
        }
    }
}
