package se.koc.trader.api;

import se.koc.trader.TraderContext;
import se.koc.trader.endpoint.AlertEndpoint;
import zutil.io.file.FileUtil;
import zutil.net.http.HttpHeader;
import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream;
import zutil.parser.Templator;
import zutil.ui.Navigation;
import zutil.ui.UserMessageManager;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class TraderPage implements HttpPage {
    private static final String TEMPLATE_MAIN = TraderContext.RESOURCE_WEB_ROOT + "/main.tmpl";
    private static final String TEMPLATE_NAVIGATION = TraderContext.RESOURCE_WEB_ROOT + "/main_nav.tmpl";
    private static final String TEMPLATE_SUB_NAVIGATION = TraderContext.RESOURCE_WEB_ROOT + "/main_subnav.tmpl";

    private static Navigation rootNav = Navigation.createRootNav();
    private static Navigation userNav = Navigation.createRootNav();

    private String pageId;
    private boolean showSubNav = true;


    public TraderPage(String id){
        this.pageId = id;
    }


    public String getId(){
        return pageId;
    }


    @Override
    public void respond(HttpPrintStream out, HttpHeader header,
                        Map<String, Object> session, Map<String, String> cookie,
                        Map<String, String> request) throws IOException {
        try {
            // Prepare common template data

            Map<String,Object> data = new HashMap<>();
            data.put("rootNav", rootNav.createPagedNavInstance(header).getSubNavs());
            data.put("userNav", userNav.createPagedNavInstance(header).getSubNavs());
            data.put("breadcrumb", Navigation.getBreadcrumb(Navigation.getPagedNavigation(header)));
            data.put("page", Navigation.getPagedNavigation(header));
            data.put("alerts", AlertEndpoint.getInstance().generateAlerts());
            data.put("user", "Profile");

            // Create templates

            Templator navigationTemplate = new Templator(FileUtil.find(TEMPLATE_NAVIGATION));
            navigationTemplate.setAll(data);

            Templator subNavigationTemplate = null;
            if (showSubNav) {
                subNavigationTemplate = new Templator(FileUtil.find(TEMPLATE_SUB_NAVIGATION));
                subNavigationTemplate.setAll(data);
            }

            Templator main = new Templator(FileUtil.find(TEMPLATE_MAIN));
            main.setAll(data);
            main.set("navigation", navigationTemplate);
            main.set("sub_navigation", subNavigationTemplate);
            main.set("content", httpRespond(session, cookie, request));

            out.print(main.compile());
        } catch (Exception e) {
            throw new IOException(e);
        }
    }

    /**
     * Defines if the sub-navigation should be shown on the page
     */
    protected void showSubNav(boolean show) {
        this.showSubNav = show;
    }

    public static Navigation getRootNav(){
        return rootNav;
    }
    public static Navigation getUserNav(){
        return userNav;
    }


    public abstract Templator httpRespond(
            Map<String, Object> session,
            Map<String, String> cookie,
            Map<String, String> request)
                throws Exception;

}