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

import com.angkorteam.mbaas.server.Jdbc;
import com.angkorteam.mbaas.server.nashorn.Disk;
import com.angkorteam.mbaas.server.nashorn.Factory;
import com.angkorteam.mbaas.server.wicket.MasterPage;
import com.angkorteam.mbaas.server.wicket.Mount;
import org.apache.wicket.request.cycle.RequestCycle;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by socheat on 5/28/16.
 */
@Mount("/flow")
public class FlowPage extends MasterPage {

    private String pageId;

    private Factory factory;

    private Map<String, Object> userModel;

    private Disk disk;

    private String script;

    @Override
    protected void onInitialize() {
        super.onInitialize();
        this.pageId = getRequest().getQueryParameters().getParameterValue("pageId").toString("");
        this.userModel = new HashMap<>();
        JdbcTemplate jdbcTemplate = getApplicationJdbcTemplate();
        Map<String, Object> pageRecord = jdbcTemplate.queryForMap("SELECT * FROM " + Jdbc.PAGE + " WHERE " + Jdbc.Page.PAGE_ID + " = ?", this.pageId);
        this.script = (String) pageRecord.get(Jdbc.Page.JAVASCRIPT);
        this.disk = new Disk(getApplicationCode(), getSession().getApplicationUserId());
        this.factory = new Factory(this, this.disk, getApplicationCode(), this.script, this.userModel);
        ScriptEngine engine = getScriptEngine();
        try {
            engine.eval(this.script);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        Invocable invocable = (Invocable) engine;
        IOnInitialize iOnInitialize = invocable.getInterface(IOnInitialize.class);
        if (iOnInitialize != null) {
            iOnInitialize.onInitialize(RequestCycle.get(), jdbcTemplate, this.disk, this.factory, this.userModel);
        }
    }

    @Override
    protected void onBeforeRender() {
        super.onBeforeRender();
        ScriptEngine engine = getScriptEngine();
        try {
            engine.eval(this.script);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        Invocable invocable = (Invocable) engine;
        IOnBeforeRender iOnBeforeRender = invocable.getInterface(IOnBeforeRender.class);
        if (iOnBeforeRender != null) {
            JdbcTemplate jdbcTemplate = getApplicationJdbcTemplate();
            iOnBeforeRender.onBeforeRender(RequestCycle.get(), jdbcTemplate, this.disk, this.factory, this.userModel);
        }
    }

    @Override
    public String getVariation() {
        return this.pageId;
    }

    public interface IOnBeforeRender extends Serializable {

        void onBeforeRender(RequestCycle requestCycle, JdbcTemplate jdbcTemplate, Disk disk, Factory factory, Map<String, Object> userModel);

    }

    public interface IOnInitialize extends Serializable {

        void onInitialize(RequestCycle requestCycle, JdbcTemplate jdbcTemplate, Disk disk, Factory factory, Map<String, Object> userModel);

    }
}

@@ -1928,32 +1928,43 @@
 uestCycle.get(),
+ this.disk,
  jdbcTemplate, t
@@ -1957,35 +1957,24 @@
 dbcTemplate,
- this.disk,
  this.factor
@@ -2586,16 +2586,27 @@
 e.get(),
+ this.disk,
  jdbcTem
@@ -2611,27 +2611,16 @@
 emplate,
- this.disk,
  this.fa
@@ -2851,32 +2851,43 @@
 le requestCycle,
+ Disk disk,
  JdbcTemplate jd
@@ -2893,35 +2893,24 @@
 dbcTemplate,
- Disk disk,
  Factory fac
@@ -3066,16 +3066,27 @@
 stCycle,
+ Disk disk,
  JdbcTem
@@ -3104,27 +3104,16 @@
 emplate,
- Disk disk,
  Factory

arrange param