package _VisualDVM.TestingSystem.Common.Group.UI;
import Common.Database.Tables.DataSet;
import Common.Passes.PassCode_;
import Common.Visual.Controls.MenuBarButton;
import Common.Visual.Controls.StableMenuItem;
import Common.Visual.DataSetFiltersMenu;
import Common.Visual.FilterFlag;
import Common.Visual.FilterMenuFlag;
import Common.Visual.Menus.DataMenuBar;
import Common.Visual.Tables.ColumnInfo;
import Common.Visual.Tables.HeaderTextFilter;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.LanguageName;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Test.TestType;
import _VisualDVM.Visual.RDataSetControlForm;

import javax.swing.*;
public class GroupsForm extends RDataSetControlForm<Group> {
    public static boolean filterMyOnly = false;
    public GroupsForm(DataSet<?, Group> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<Group>("имя") {
                    @Override
                    public Object getFieldAt(Group object) {
                        return object.description;
                    }
                },
                new ColumnInfo<Group>("автор") {
                    @Override
                    public Object getFieldAt(Group object) {
                        return object.sender_name;
                    }
                },
                new ColumnInfo<Group>("тип") {
                    @Override
                    public Object getFieldAt(Group object) {
                        return object.type.getDescription();
                    }
                },
                new ColumnInfo<Group>("язык") {
                    @Override
                    public Object getFieldAt(Group object) {
                        return object.language.getDescription();
                    }
                }
        );
    }
    @Override
    public PassCode_ getDeletePassCode() {
        return PassCode.DeleteGroup;
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription(),
                PassCode.SynchronizeTests,
                PassCode.ConvertCorrectnessTests) {
            {
                addMenus(new AddGroupMenu(), new EditGroupMenu());
                addPasses(PassCode.DeleteGroup);
            }
        };
    }
    @Override
    protected void createFilters() {
        AddFilters(
                new HeaderTextFilter<>(dataSource, 2, 65),
                new FilterFlag<Group>(dataSource, new MenuBarButton() {
                    {
                        setText("Свои");
                        setToolTipText("Отображать только группы тестов авторства пользователя");
                    }
                }, false) {
                    @Override
                    protected boolean validate(Group object) {
                        return Global.mainModule.getAccount().email.equals(object.sender_address);
                    }
                },
                new FilterFlag<Group>(dataSource, new MenuBarButton() {
                    {
                        setText("Стандарт");
                        setToolTipText("Отображать только стандартные группы тестов");
                    }
                }, false) {
                    @Override
                    protected boolean validate(Group object) {
                        return object.sender_address.equals(Constants.MailAddress);
                    }
                },
                new DataSetFiltersMenu<Group>("Тип", dataSource) {
                    @Override
                    public void fill() {
                        for (TestType type : TestType.values())
                            field_filters.add(new FilterMenuFlag<Group>(dataSource, new StableMenuItem(type.getDescription()), false) {
                                @Override
                                protected boolean validate(Group object) {
                                    return object.type.equals(type);
                                }
                            });
                    }
                }, new DataSetFiltersMenu<Group>("Язык", dataSource) {
                    @Override
                    public void fill() {
                        for (LanguageName languageName : LanguageName.values()) {
                            field_filters.add(new FilterMenuFlag<Group>(dataSource, new StableMenuItem(languageName.getDescription()), false) {
                                @Override
                                protected boolean validate(Group object) {
                                    return object.language.equals(languageName);
                                }
                            });
                        }
                    }
                });
    }
    @Override
    protected DBObjectDialog getDialog() {
        return new GroupDialog();
    }
    @Override
    protected boolean isObjectEditable(Group 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;
    }
}
