package _VisualDVM.GlobalData.CompilerOption.UI;
import Common.Passes.Pass;
import Common.Visual.DataSetControlForm;
import Common.Visual.Menus.DataMenuBar;
import Common.Visual.Tables.ColumnInfo;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.CompilerOption.Json.OptionsJson;
import _VisualDVM.GlobalData.CompilerOption.OptionsLine;
import _VisualDVM.GlobalData.CompilerOption.OptionsLinesSet;
import _VisualDVM.Passes.PassCode;

import javax.swing.*;
public class OptionsLinesForm extends DataSetControlForm<OptionsLine> {
    public OptionsLinesForm(OptionsLinesSet dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(new ColumnInfo<OptionsLine>("") {
            @Override
            public Object getFieldAt(OptionsLine object) {
                return object.json.toLine();
            }
        });
    }
    @Override
    protected boolean hasCheckBox() {
        return false;
    }
    @Override
    protected boolean isPKVisible() {
        return false;
    }
    @Override
    protected DataMenuBar createMenuBar() {
        DataMenuBar res = super.createMenuBar();
        res.addPasses(
                new Pass<OptionsLine>() {
                    @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.PickCompilerOptions);
                        if (pass.Do(Global.mainModule.getDb().compilers.getUI().getCurrent())) {
                            target = new OptionsLine((OptionsJson) pass.target);
                            target.id = ((OptionsLinesSet) 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<OptionsLine>() {
                    @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.PickCompilerOptions);
                            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<OptionsLine>() {
                    @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;
    }
}
