package _VisualDVM.ProjectData.SapforData.Arrays.UI;
import Common.CommonConstants;
import Common.Database.Tables.DataSet;
import Common.Visual.DataSetControlForm;
import Common.Visual.Tables.ColumnInfo;
import Common.Visual.Tables.DBObjectSelectionRenderer;
import Common.Visual.Tables.DBObjectSelector;
import Common.Visual.Tables.RendererHiddenList;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.SapforData.Arrays.ProjectArray;
import _VisualDVM.Visual.Tables.EditorFilesHyperlinks;
import _VisualDVM.Visual.Tables.RendererHyperlinks;

import javax.swing.*;
import java.util.Vector;
public class ProjectArraysForm extends DataSetControlForm<ProjectArray> {
    //-
    public static String filterName = "";
    public static String filterLocation = "";
    public static String filterLocationName = "";
    public static String filterFile = "";
    public static String filterRegion = "*";
    //--
    public ProjectArraysForm(DataSet<?, ProjectArray> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected boolean isPKVisible() {
        return false;
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<ProjectArray>("Имя") {
                    @Override
                    public Object getFieldAt(ProjectArray object) {
                        return object.GetShortNameWithDim();
                    }
                },
                new ColumnInfo<ProjectArray>("Область описания") {
                    @Override
                    public Object getFieldAt(ProjectArray object) {
                        return object.locName + " : " + object.getLocation();
                    }
                },
                new ColumnInfo<ProjectArray>("Файлы объявления") {
                    @Override
                    public Object getFieldAt(ProjectArray object) {
                        return object.GetDeclPlacesList();
                    }
                    @Override
                    public Class getRendererClass() {
                        //todo все настройки переделать в проперти(?)
                        return Global.normalProperties.ShowFullArraysDeclarations ? RendererHyperlinks.class : RendererHiddenList.class;
                    }
                    @Override
                    public Class getEditorClass() {
                        return Global.normalProperties.ShowFullArraysDeclarations ? EditorFilesHyperlinks.class : null;
                    }
                    @Override
                    public int getMaxWidth() {
                        return Global.normalProperties.ShowFullArraysDeclarations ? CommonConstants.Nan : 200;
                    }
                },
                new ColumnInfo<ProjectArray>("Размерность") {
                    @Override
                    public Object getFieldAt(ProjectArray object) {
                        return object.dimSize;
                    }
                },
                new ColumnInfo<ProjectArray>("Размер элемента(байт)") {
                    @Override
                    public Object getFieldAt(ProjectArray object) {
                        return object.typeSize;
                    }
                },
                new ColumnInfo<ProjectArray>("Область распараллеливания") {
                    @Override
                    public Object getFieldAt(ProjectArray object) {
                        return object.GetRegionsText();
                    }
                }
        );
    }
    @Override
    protected ColumnInfo<ProjectArray> createCheckBoxColummn() {
        return new ColumnInfo<ProjectArray>("") {
            @Override
            public Object getFieldAt(ProjectArray object) {
                return object.getState();
            }
            @Override
            public int getMinWidth() {
                return 25;
            }
            @Override
            public int getMaxWidth() {
                return 25;
            }
            @Override
            public Class getRendererClass() {
                return DBObjectSelectionRenderer.class;
            }
            @Override
            public Class getEditorClass() {
                return DBObjectSelector.class;
            }
        };
    }
    @Override
    public boolean isObjectVisible(ProjectArray object) {
        if (super.isObjectVisible(object)) {
            if (object.isTemplFlag > 0) return true;
            return object.shortName.startsWith(filterName) &&
                    object.getLocation().toString().startsWith(filterLocation) &&
                    object.locName.startsWith(filterLocationName) &&
                    object.has_decl_place(filterFile) &&
                    object.has_region(filterRegion);
        }
        return false;
    }
    @Override
    public void SelectAll(boolean flag) {
        Global.mainModule.getPass(PassCode.MassSelectArrays).Do(flag,
                new Vector(Global.mainModule.getProject().declaratedArrays.Data.values()));
    }
}
