package _VisualDVM.Passes.All;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Sapfor.SapforModification;
import _VisualDVM.ProjectData.SapforData.Arrays.Dimension;
import _VisualDVM.ProjectData.SapforData.Arrays.ProjectArray;
import _VisualDVM.ProjectData.SapforData.Regions.ParallelRegion;
import _VisualDVM.ProjectData.SapforData.Regions.UI.ArrayAlignmentBar;
import _VisualDVM.ProjectData.SapforData.Regions.UI.ParallelRegionFields;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class SPF_ModifyArrayDistribution extends SapforModification {
    ParallelRegion region;
    @Override
    public boolean needsConfirmations() {
        return false;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        region = null;
        if (
                Global.mainModule.getProject().parallelRegions.getUI().CheckCurrent(Log)
                        && super.canStart(args) && Global.mainModule.getPass(PassCode.SPF_GetArrayLinks).Do()) {
            region = Global.mainModule.getProject().parallelRegions.getUI().getCurrent();
            DBObjectDialog dialog = new DBObjectDialog<ParallelRegion, ParallelRegionFields>(ParallelRegionFields.class) {
                @Override
                public int getDefaultHeight() {
                    return 250;
                }
                @Override
                public void fillFields() {
                    Vector<ArrayAlignmentBar> bars = new Vector<>();
                    for (ProjectArray array : Result.arraysMap.values()) {
                        if (array.canBeDistributed() && array.align_template != null) {
                            array.bar = null;
                            ArrayAlignmentBar bar = new ArrayAlignmentBar(array);
                            bars.add(bar);
                        }
                    }
                    JPanel panel = fields.Content;
                    panel.setLayout(new GridLayout(bars.size(), 1, 5, 12));
                    for (ArrayAlignmentBar bar : bars)
                        panel.add(bar);
                }
                @Override
                public void ProcessResult() {
                    for (ProjectArray array : Result.arraysMap.values()) {
                        if (array.bar != null) {
                            array.bar.apply_changes();
                            //заполнение новых коеффициентов.
                        }
                    }
                }
            };
            if (!dialog.ShowDialog(region.toString(), region))
                return false;
            boolean needsChange = false;
            for (ProjectArray array : region.arraysMap.values()) {
                if (array.canBeDistributed() && array.ac_need_change()) {
                    needsChange = true;
                    break;
                }
            }
            if (needsChange) {
                addOpt1 = region.regionId.toString();
                Vector<String> coeffs = new Vector<>();
                for (ProjectArray array : region.arraysMap.values()) {
                    if (array.ac_new.size() > 0) {
                        coeffs.add(String.valueOf(array.address));
                        for (int dim : array.ac_new.keySet()) {
                            Dimension dimension = array.ac_new.get(dim);
                            coeffs.add(String.valueOf(dimension.getS()));
                            coeffs.add(String.valueOf(dimension.K));
                            coeffs.add(String.valueOf(dimension.B));
                        }
                    }
                }
                coeffs.insertElementAt(String.valueOf(coeffs.size() + 1), 0);
                addOpt2 = String.join("|", coeffs);
                addOpt2 = String.join("|", coeffs);
                return true;
            }
            Log.Writeln("Коефициенты выравнивания актуальны");
        }
        return false;
    }
    @Override
    protected void performDone() throws Exception {
        super.performDone();
        Global.mainModule.getPass(PassCode.SPF_GetArrayDistribution).Do();
    }
    //распаковать журнал
    @Override
    protected void performFinish() throws Exception {
        target.updateLog(sapfor.getOutput());
    }
    //показать журнал.
    @Override
    protected void showFinish() throws Exception {
        Global.mainModule.getUI().getMainWindow().getProjectWindow().ShowProjectSapforLog();
    }
}
