package com.angkorteam.mbaas.server.page.menuitem;

import com.angkorteam.framework.extension.wicket.extensions.markup.html.repeater.data.table.DataTable;
import com.angkorteam.framework.extension.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import com.angkorteam.framework.extension.wicket.extensions.markup.html.repeater.data.table.filter.*;
import com.angkorteam.mbaas.server.page.MBaaSPage;
import com.angkorteam.mbaas.server.provider.MenuItemProvider;
import com.google.common.collect.Maps;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.border.Border;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by socheat on 10/24/16.
 */
public class MenuItemBrowsePage extends MBaaSPage {

    @Override
    public String getPageUUID() {
        return MenuItemBrowsePage.class.getName();
    }

    @Override
    protected void doInitialize(Border layout) {
        add(layout);

        MenuItemProvider provider = new MenuItemProvider();
        provider.selectField("menuItemId", String.class);

        FilterForm<Map<String, String>> filterForm = new FilterForm<>("filter-form", provider);
        layout.add(filterForm);

        List<IColumn<Map<String, Object>, String>> columns = new ArrayList<>();
        columns.add(new TextFilterColumn(provider, ItemClass.String, Model.of("title"), "title", this::modelValue));
        columns.add(new TextFilterColumn(provider, ItemClass.String, Model.of("icon"), "icon", this::modelValue));
        columns.add(new TextFilterColumn(provider, ItemClass.String, Model.of("page"), "page", this::modelValue));
        columns.add(new TextFilterColumn(provider, ItemClass.String, Model.of("menu"), "menu", this::modelValue));
        columns.add(new TextFilterColumn(provider, ItemClass.String, Model.of("section"), "section", this::modelValue));
        columns.add(new TextFilterColumn(provider, ItemClass.Boolean, Model.of("system"), "system", this::modelValue));
        columns.add(new ActionFilterColumn(Model.of("action"), this::actions, this::clickable, this::itemCss, this::itemClick));

        DataTable<Map<String, Object>, String> dataTable = new DefaultDataTable<>("table", columns, provider, 20);
        dataTable.addTopToolbar(new FilterToolbar(dataTable, filterForm));
        filterForm.add(dataTable);

        BookmarkablePageLink<Void> refreshLink = new BookmarkablePageLink<>("refreshLink", MenuItemBrowsePage.class);
        layout.add(refreshLink);

        BookmarkablePageLink<Void> createLink = new BookmarkablePageLink<>("createLink", MenuItemCreatePage.class);
        layout.add(createLink);
    }

    private Map<String, IModel<String>> actions() {
        Map<String, IModel<String>> actions = Maps.newHashMap();
        actions.put("Edit", Model.of("Edit"));
        return actions;
    }

    private void itemClick(String link, Map<String, Object> object, AjaxRequestTarget ajaxRequestTarget) {
        String menuItemId = (String) object.get("menuItemId");
        PageParameters parameters = new PageParameters();
        parameters.add("menuItemId", menuItemId);
        setResponsePage(MenuItemModifyPage.class, parameters);
    }

    private Boolean clickable(String link, Map<String, Object> object) {
        Boolean system = (Boolean) object.get("system");
        if ("Edit".equals(link)) {
            return !system;
        }
        return false;
    }

    private ItemCss itemCss(String link, Map<String, Object> model) {
        if ("Edit".equals(link)) {
            return ItemCss.INFO;
        }
        return ItemCss.NONE;

    }

    private Object modelValue(String name, Map<String, Object> stringObjectMap) {
        return stringObjectMap.get(name);
    }
}

@@ -360,16 +360,59 @@
 lter.*;%0A
+import com.angkorteam.mbaas.server.Spring;%0A
 import c
@@ -1026,16 +1026,67 @@
 ameters;
+%0Aimport org.springframework.jdbc.core.JdbcTemplate;
 %0A%0Aimport
@@ -1249,16 +1249,79 @@
 Page %7B%0A%0A
+    private DataTable%3CMap%3CString, Object%3E, String%3E dataTable;%0A%0A
     @Ove
@@ -2675,47 +2675,8 @@
     
- DataTable%3CMap%3CString, Object%3E, String%3E
  dat
@@ -3318,24 +3318,75 @@
 f(%22Edit%22));%0A
+        actions.put(%22Delete%22, Model.of(%22Delete%22));%0A
         retu
@@ -3494,20 +3494,9 @@
 get 
-ajaxRequestT
+t
 arge
@@ -3496,24 +3496,63 @@
 t target) %7B%0A
+        if (%22Edit%22.equals(link)) %7B%0A    
         Stri
@@ -3598,24 +3598,28 @@
 nuItemId%22);%0A
+    
         Page
@@ -3672,16 +3672,20 @@
         
+    
 paramete
@@ -3726,16 +3726,20 @@
         
+  
+  
 setRespo
@@ -3781,24 +3781,354 @@
 arameters);%0A
+        %7D else if (%22Delete%22.equals(link)) %7B%0A            JdbcTemplate jdbcTemplate = Spring.getBean(JdbcTemplate.class);%0A            String menuItemId = (String) object.get(%22menuItemId%22);%0A            jdbcTemplate.update(%22DELETE FROM menu_item WHERE menu_item_id = ?%22, menuItemId);%0A            target.add(this.dataTable);%0A        %7D%0A
     %7D%0A%0A    p
@@ -4275,32 +4275,57 @@
 it%22.equals(link)
+ %7C%7C %22Delete%22.equals(link)
 ) %7B%0A            
@@ -4517,16 +4517,95 @@
 s.INFO;%0A
+        %7D else if (%22Delete%22.equals(link)) %7B%0A            return ItemCss.DANGER;%0A
         

allow to delete menu item