package com.angkorteam.mbaas.server.nashorn.wicket.ajax.markup.html.form;

import com.angkorteam.mbaas.server.wicket.Application;
import com.angkorteam.mbaas.server.wicket.ApplicationUtils;
import com.angkorteam.mbaas.server.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.Map;

/**
 * Created by socheat on 5/31/16.
 */
public class NashornAjaxButton extends org.apache.wicket.ajax.markup.html.form.AjaxButton {

    private String script;

    private Map<String, Object> userModel;

    public NashornAjaxButton(String id) {
        super(id);
    }

    public NashornAjaxButton(String id, IModel<String> model) {
        super(id, model);
    }

    public NashornAjaxButton(String id, Form<?> form) {
        super(id, form);
    }

    public NashornAjaxButton(String id, IModel<String> model, Form<?> form) {
        super(id, model, form);
    }

    public Map<String, Object> getUserModel() {
        return userModel;
    }

    public void setUserModel(Map<String, Object> userModel) {
        this.userModel = userModel;
    }

    @Override
    protected final void onSubmit(AjaxRequestTarget target, Form<?> form) {
        Session session = (Session) Session.get();
        Application application = ApplicationUtils.getApplication();
        JdbcTemplate jdbcTemplate = application.getJdbcTemplate(session.getApplicationCode());

        ScriptEngine scriptEngine = ApplicationUtils.getApplication().getScriptEngine();
        if (this.script != null || !"".equals(this.script)) {
            try {
                scriptEngine.eval(this.script);
            } catch (ScriptException e) {
            }
        }
        Invocable invocable = (Invocable) scriptEngine;
        try {
            invocable.invokeFunction(getId() + "__on_submit", RequestCycle.get(), jdbcTemplate, this, target, form, this.userModel);
        } catch (ScriptException e) {
        } catch (NoSuchMethodException e) {
        } catch (EmptyResultDataAccessException e) {
        }
    }

    @Override
    protected final void onError(AjaxRequestTarget target, Form<?> form) {
        Session session = (Session) Session.get();
        Application application = ApplicationUtils.getApplication();
        JdbcTemplate jdbcTemplate = application.getJdbcTemplate(session.getApplicationCode());

        ScriptEngine scriptEngine = ApplicationUtils.getApplication().getScriptEngine();
        if (this.script != null || !"".equals(this.script)) {
            try {
                scriptEngine.eval(this.script);
            } catch (ScriptException e) {
            }
        }
        Invocable invocable = (Invocable) scriptEngine;
        try {
            invocable.invokeFunction(getId() + "__on_error", RequestCycle.get(), jdbcTemplate, this, target, form, this.userModel);
        } catch (ScriptException e) {
        } catch (NoSuchMethodException e) {
        } catch (EmptyResultDataAccessException e) {
        }
    }

    public String getScript() {
        return script;
    }

    public void setScript(String script) {
        this.script = script;
    }
}

@@ -68,16 +68,68 @@
 .form;%0A%0A
+import com.angkorteam.mbaas.server.nashorn.Factory;%0A
 import c
@@ -171,24 +171,24 @@
 pplication;%0A
-
 import com.a
@@ -922,16 +922,46 @@
 Model;%0A%0A
+    private Factory factory;%0A%0A
     publ
@@ -2259,32 +2259,40 @@
 bcTemplate, this
+.factory
 , target, form, 
@@ -3503,24 +3503,24 @@
 g script) %7B%0A
-
         this
@@ -3542,11 +3542,161 @@
 t;%0A    %7D
+%0A%0A    public Factory getFactory() %7B%0A        return factory;%0A    %7D%0A%0A    public void setFactory(Factory factory) %7B%0A        this.factory = factory;%0A    %7D
 %0A%7D%0A

change param ajax button to factory