
/**
 *
 */
package org.devgateway.toolkit.forms.wicket.page.edit;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.lang.Objects;
import org.devgateway.toolkit.forms.security.SecurityConstants;
import org.devgateway.toolkit.forms.validators.UniquePropertyValidator;
import org.devgateway.toolkit.forms.wicket.components.CollapsablePanel;
import org.devgateway.toolkit.forms.wicket.components.form.CheckBoxBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.CheckBoxPickerBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.CheckBoxToggleBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.ColorPickerBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.DateFieldBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.DateTimeFieldBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.FileInputBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.PercentageFieldBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.Select2ChoiceBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.Select2MultiChoiceBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.SummernoteBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.TextAreaFieldBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.components.form.TextFieldBootstrapFormComponent;
import org.devgateway.toolkit.forms.wicket.page.edit.panel.TestFormChildPanel;
import org.devgateway.toolkit.forms.wicket.page.lists.ListTestFormPage;
import org.devgateway.toolkit.forms.wicket.providers.GenericPersistableJpaTextChoiceProvider;
import org.devgateway.toolkit.persistence.dao.Role;
import org.devgateway.toolkit.persistence.dao.TestForm;
import org.devgateway.toolkit.persistence.dao.categories.Group;
import org.devgateway.toolkit.persistence.service.RoleService;
import org.devgateway.toolkit.persistence.service.TestFormService;
import org.devgateway.toolkit.persistence.service.category.GroupService;
import org.wicketstuff.annotation.mount.MountPath;

import java.io.Serial;

/**
 * @author mpostelnicu
 */

@AuthorizeInstantiation(SecurityConstants.Roles.ROLE_USER)
@MountPath("/editTestForm")
public class EditTestFormPage extends AbstractEditStatusEntityPage<TestForm> {

    private static final long serialVersionUID = 1L;

    @SpringBean
    private TestFormService testFormService;

    @SpringBean
    private RoleService roleService;

    @SpringBean
    private GroupService groupService;

    /**
     * @param parameters
     */
    public EditTestFormPage(final PageParameters parameters) {
        super(parameters);

        this.jpaService = testFormService;
        this.listPageClass = ListTestFormPage.class;
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();

        TextFieldBootstrapFormComponent<String> textField = new TextFieldBootstrapFormComponent<>("textField");
        editForm.add(textField);
        textField.required();
        textField.getField().add(new UniquePropertyValidator<>(testFormService, Objects.defaultIfNull(entityId, -1L),
                "textField", this));
        textField.enableRevisionsView();


        TextAreaFieldBootstrapFormComponent<String> textArea = new TextAreaFieldBootstrapFormComponent<>("textArea");
        editForm.add(textArea);
        textArea.required();

        SummernoteBootstrapFormComponent summernote = new SummernoteBootstrapFormComponent("summernote");
        editForm.add(summernote);
        summernote.required().enableRevisionsView();

        editForm.add(new TestFormChildPanel("testFormChildren"));

        Select2ChoiceBootstrapFormComponent<Group> entitySelect = new Select2ChoiceBootstrapFormComponent<Group>(
                "entitySelect", new GenericPersistableJpaTextChoiceProvider<>(groupService));
        entitySelect.required();
        editForm.add(entitySelect);

        Select2MultiChoiceBootstrapFormComponent<Role> entityMultiSelect =
                new Select2MultiChoiceBootstrapFormComponent<Role>(
                        "entityMultiSelect",
                        new GenericPersistableJpaTextChoiceProvider<Role>(roleService)
                );
        editForm.add(entityMultiSelect);

        CheckBoxBootstrapFormComponent checkbox = new CheckBoxBootstrapFormComponent("checkbox");
        checkbox.required();
        editForm.add(checkbox);
        //checkbox.enableRevisionsView();

        CheckBoxPickerBootstrapFormComponent checkboxPicker =
                new CheckBoxPickerBootstrapFormComponent("checkboxPicker");
        checkboxPicker.required();
        editForm.add(checkboxPicker);
        //checkboxPicker.enableRevisionsView();

        CheckBoxToggleBootstrapFormComponent checkboxToggle =
                new CheckBoxToggleBootstrapFormComponent("checkboxToggle");
        checkboxToggle.required();
        editForm.add(checkboxToggle);

        DateFieldBootstrapFormComponent date = new DateFieldBootstrapFormComponent("date");
        date.required();
        editForm.add(date);

        DateTimeFieldBootstrapFormComponent dateTime = new DateTimeFieldBootstrapFormComponent("dateTime");
        dateTime.required();
        editForm.add(dateTime);

        PercentageFieldBootstrapFormComponent percentage = new PercentageFieldBootstrapFormComponent("percentage");
        percentage.required();
        editForm.add(percentage);

        FileInputBootstrapFormComponent fileInput = new FileInputBootstrapFormComponent("fileInput");
        fileInput.required();
        fileInput.allowedFileExtensions("txt");
        editForm.add(fileInput);

        ColorPickerBootstrapFormComponent colorPicker = new ColorPickerBootstrapFormComponent("colorPicker");
        colorPicker.required();
        editForm.add(colorPicker);

        CollapsablePanel collapsable1 = new CollapsablePanelTest("collapsable-panel1", Model.of("Collapsable Panel 1"));
        editForm.add(collapsable1);
        CollapsablePanel collapsable2 = new CollapsablePanelTest("collapsable-panel2", Model.of("Collapsable Panel 2"));
        editForm.add(collapsable2);
    }

    private static class CollapsablePanelTest extends CollapsablePanel {
        @Serial
        private static final long serialVersionUID = -858848352646588874L;

        CollapsablePanelTest(final String id, final IModel<String> headerLabelModel) {
            super(id, headerLabelModel);
        }

        @Override
        protected void onInitializeBody(final WebMarkupContainer body) {
        }
    }

}
