package com.jvm_bloggers.frontend.admin_area.blogs;

import com.jvm_bloggers.entities.blog.Blog;
import com.jvm_bloggers.entities.blog.BlogRepository;
import com.jvm_bloggers.frontend.admin_area.panels.CustomFeedbackPanel;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.io.Serializable;
import java.util.function.Consumer;

import static java.lang.String.format;

public class BlogActionPanel extends Panel {

    @SpringBean
    private BlogRepository blogRepository;

    private final Form<?> form;
    private final IModel<Blog> blogModel;
    private final CustomFeedbackPanel feedbackPanel;

    public BlogActionPanel(
        String id, Form<?> form, IModel<Blog> blogModel, CustomFeedbackPanel feedbackPanel) {

        super(id);
        this.form = form;
        this.blogModel = blogModel;
        this.feedbackPanel = feedbackPanel;

        add(createActivateBlogButton());
        add(createDeactivateBlogButton());
        add(createRequireModerationBlogButton());
        add(createDoNotRequireModerationBlogButton());
    }

    private AjaxButton createActivateBlogButton() {
        AjaxButton activate = createBlogActionButton("activateBlog",
            (Consumer<Blog> & Serializable) b -> b.setActive(true),
            format("'%s' blog activated", blogModel.getObject().getAuthor()));
        activate.setVisible(!blogModel.getObject().isActive());
        return activate;
    }

    private AjaxButton createDeactivateBlogButton() {
        AjaxButton deactivate = createBlogActionButton("deactivateBlog",
            (Consumer<Blog> & Serializable) b -> b.setActive(false),
            format("'%s' blog deactivated", blogModel.getObject().getAuthor()));
        deactivate.setVisible(blogModel.getObject().isActive());
        return deactivate;
    }

    private AjaxButton createRequireModerationBlogButton() {
        AjaxButton requireModeration = createBlogActionButton("requireModerationBlog",
            (Consumer<Blog> & Serializable) b -> b.setModerationRequired(true),
            format("'%s' blog require moderation", blogModel.getObject().getAuthor()));
        requireModeration.setVisible(!blogModel.getObject().isModerationRequired());
        return requireModeration;
    }

    private AjaxButton createDoNotRequireModerationBlogButton() {
        AjaxButton notRequireModeration = createBlogActionButton("doNotRequireModerationBlog",
            (Consumer<Blog> & Serializable) b -> b.setModerationRequired(false),
            format("'%s' blog does not require moderation", blogModel.getObject().getAuthor()));
        notRequireModeration.setVisible(blogModel.getObject().isModerationRequired());
        return notRequireModeration;
    }

    private AjaxButton createBlogActionButton(String id, Consumer<Blog> blogAction,
                                              String successMessage) {

        return new AjaxButton(id, form) {

            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                Blog blog = blogModel.getObject();
                blogAction.accept(blog);
                blogRepository.save(blog);

                getSession().success(successMessage);

                target.add(getForm());
                target.add(feedbackPanel);
            }
        };
    }
}

@@ -1413,16 +1413,29 @@
 nButton(
+%0A            
 %22activat
@@ -1521,32 +1521,49 @@
          format(
+%0A                
 %22'%25s' blog activ
@@ -1594,33 +1594,55 @@
 ct().getAuthor()
-)
+%0A            )%0A        
 );%0A        activ
@@ -1833,16 +1833,29 @@
 nButton(
+%0A            
 %22deactiv
@@ -1944,32 +1944,49 @@
          format(
+%0A                
 %22'%25s' blog deact
@@ -2019,33 +2019,55 @@
 ct().getAuthor()
-)
+%0A            )%0A        
 );%0A        deact
@@ -2275,16 +2275,29 @@
 nButton(
+%0A            
 %22require
@@ -2404,32 +2404,49 @@
          format(
+%0A                
 %22'%25s' blog requi
@@ -2486,33 +2486,55 @@
 ct().getAuthor()
-)
+%0A            )%0A        
 );%0A        requi
@@ -2777,16 +2777,29 @@
 nButton(
+%0A            
 %22doNotRe
@@ -2920,16 +2920,33 @@
  format(
+%0A                
 %22'%25s' bl
@@ -3011,17 +3011,39 @@
 Author()
-)
+%0A            )%0A        
 );%0A     

apply paired brackets notation style