package _VisualDVM.TestingSystem.Common.Test.UI;
import Common.Database.Tables.DataSet;
import Common.Passes.PassCode_;
import Common.Visual.Menus.DataMenuBar;
import Common.Visual.Tables.ColumnInfo;
import Common.Visual.Tables.HeaderTextFilter;
import Common.Visual.Tables.RendererLongCell;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ServerObjectsCache.TestCache;
import _VisualDVM.ServerObjectsCache.VisualCaches;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Test.Test;
import _VisualDVM.Visual.RDataSetControlForm;

import javax.swing.*;
public class TestsForm extends RDataSetControlForm<Test> {
    public TestsForm(DataSet<?, Test> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<Test>("имя") {
                    @Override
                    public Object getFieldAt(Test object) {
                        return object.description;
                    }
                },
                new ColumnInfo<Test>("описание") {
                    @Override
                    public Object getFieldAt(Test object) {
                        return object.extended_description.replace("\n", ";\n");
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererLongCell.class;
                    }
                },
                new ColumnInfo<Test>("max_dim") {
                    @Override
                    public Object getFieldAt(Test object) {
                        return object.max_dim;
                    }
                },
                new ColumnInfo<Test>("файлы") {
                    @Override
                    public Object getFieldAt(Test object) {
                        return ((TestCache) VisualCaches.GetCache(object)).srcNames;
                    }
                }
        );
    }
    @Override
    public PassCode_ getDeletePassCode() {
        return PassCode.DeleteTest;
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription()) {
            {
                addMenus(new AddTestMenu(), new EditTestMenu());
                addPasses(PassCode.DetectSelectedTestsMinMaDim, PassCode.DownloadTest, PassCode.DeleteTest);
            }
        };
    }
    @Override
    public boolean isObjectVisible(Test object) {
        return super.isObjectVisible(object) &&
                Global.testingServer.db.getTable(Group.class).getUI().matchCurrentID(object.group_id);
    }
    @Override
    protected DBObjectDialog getDialog() {
        return new TestDialog();
    }
    @Override
    protected boolean isObjectEditable(Test object) {
        if (Global.mainModule.getAccount().CheckRegistered(null)) {
            switch (Global.mainModule.getAccount().role) {
                case Admin:
                    return true;
                default:
                    return (Global.mainModule.getAccount().email.equals(object.sender_address));
            }
        }
        return false;
    }
    @Override
    protected void createFilters() {
        AddFilters(
                new HeaderTextFilter<>(dataSource, 2, 65)
        );
    }
    @Override
    protected void MouseAction2() throws Exception {
        Global.mainModule.getPass(PassCode.DownloadTest).Do();
    }
};

