package _VisualDVM.GlobalData.CompilerEnvironment.UI;
import Common.Database.Tables.DataSet;
import Common.Passes.Pass;
import Common.Visual.DataSetControlForm;
import Common.Visual.Menus.DataMenuBar;
import Common.Visual.Tables.ColumnInfo;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.CompilerEnvironment.EnvironmentsLine;
import _VisualDVM.GlobalData.CompilerEnvironment.EnvironmentsLinesSet;
import _VisualDVM.GlobalData.CompilerEnvironment.Json.EnvironmentsJson;
import _VisualDVM.Passes.PassCode;

import javax.swing.*;
public class EnvironmentsLinesForm extends DataSetControlForm<EnvironmentsLine> {
    public EnvironmentsLinesForm(DataSet<?, EnvironmentsLine> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected boolean hasCheckBox() {
        return false;
    }
    @Override
    protected boolean isPKVisible() {
        return false;
    }
    @Override
    protected void createColumns() {
        AddColumns(new ColumnInfo<EnvironmentsLine>("") {
            @Override
            public Object getFieldAt(EnvironmentsLine object) {
                return object.json.toLine();
            }
        });
    }
    @Override
    protected DataMenuBar createMenuBar() {
        DataMenuBar res = super.createMenuBar();
        res.addPasses(
                new Pass<EnvironmentsLine>() {
                    @Override
                    public String getIconPath() {
                        return "/Common/icons/RedAdd.png";
                    }
                    @Override
                    public String getDescription() {
                        return "Добавление набора переменных окружения";
                    }
                    @Override
                    public String getButtonText() {
                        return "";
                    }
                    @Override
                    protected boolean canStart(Object... args) throws Exception {
                        Pass pass = Global.mainModule.getPass(PassCode.PickCompilerEnvironmentsForTesting);
                        if (pass.Do(Global.mainModule.getDb().compilers.getUI().getCurrent())) {
                            target = new EnvironmentsLine((EnvironmentsJson) pass.target);
                            target.id = ((EnvironmentsLinesSet) dataSource).maxId++;
                            return true;
                        }
                        return false;
                    }
                    @Override
                    protected void body() throws Exception {
                        dataSource.put(target.getPK(), target);
                    }
                    @Override
                    protected void showDone() throws Exception {
                        dataSource.ShowUI(target.getPK());
                    }
                },
                new Pass<EnvironmentsLine>() {
                    @Override
                    public String getIconPath() {
                        return "/Common/icons/Edit.png";
                    }
                    @Override
                    public String getDescription() {
                        return "Редактирование набора переменных окружения";
                    }
                    @Override
                    public String getButtonText() {
                        return "";
                    }
                    @Override
                    protected boolean canStart(Object... args) throws Exception {
                        if (dataSource.getUI().CheckCurrent(Log)) {
                            target = dataSource.getUI().getCurrent();
                            Pass pass = Global.mainModule.getPass(PassCode.PickCompilerEnvironmentsForTesting);
                            if (pass.Do(Global.mainModule.getDb().compilers.getUI().getCurrent(), target.json)) {
                                return true;
                            }
                        }
                        return false;
                    }
                    @Override
                    protected void body() throws Exception {
                    }
                    @Override
                    protected void showFinish() throws Exception {
                        dataSource.ShowUI(target.getPK());
                    }
                },
                new Pass<EnvironmentsLine>() {
                    @Override
                    public String getIconPath() {
                        return "/Common/icons/Delete.png";
                    }
                    @Override
                    public String getDescription() {
                        return "Удаление набора переменных окружения";
                    }
                    @Override
                    public String getButtonText() {
                        return "";
                    }
                    @Override
                    protected boolean canStart(Object... args) throws Exception {
                        if (dataSource.getUI().CheckCurrent(Log)) {
                            target = dataSource.getUI().getCurrent();
                            return dataSource.getUI().ShowDeleteObjectDialog(target);
                        }
                        return false;
                    }
                    @Override
                    protected void showPreparation() throws Exception {
                        dataSource.ClearUI();
                    }
                    @Override
                    protected void body() throws Exception {
                        dataSource.Data.remove(target.getPK());
                    }
                    @Override
                    protected void showFinish() throws Exception {
                        dataSource.ShowUI();
                    }
                }
        );
        return res;
    }
}
