package se.koc.trader.page;

import se.koc.trader.TraderContext;
import se.koc.trader.api.TraderPage;
import se.koc.trader.struct.Symbol;
import zutil.ObjectUtil;
import zutil.db.DBConnection;
import zutil.io.file.FileUtil;
import zutil.parser.Templator;

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


public class SymbolOverviewPage extends TraderPage {
    private static final String OVERVIEW_TEMPLATE = TraderContext.RESOURCE_WEB_ROOT + "/symbol_overview.tmpl";
    private static final String DETAIL_TEMPLATE   = TraderContext.RESOURCE_WEB_ROOT + "/symbol_detail.tmpl";


    public SymbolOverviewPage() {
        super("symbol_overview");
        super.getRootNav().createSubNav("Market").createSubNav(this.getId(), "Symbols");
    }

    @Override
    public Templator httpRespond(
            Map<String, Object> session,
            Map<String, String> cookie,
            Map<String, String> request)
            throws Exception{

        DBConnection db = TraderContext.getDB();
        int id = (ObjectUtil.isEmpty(request.get("id")) ? -1 : Integer.parseInt(request.get("id")));

        if (id >= 0) {
            Symbol symbol = Symbol.getSymbol(db, id);

            Templator tmpl = new Templator(FileUtil.find(DETAIL_TEMPLATE));
            tmpl.set("symbol", symbol);
            return tmpl;
        } else {
            List<Symbol> sensors = Symbol.getSymbols(db);

            Templator tmpl = new Templator(FileUtil.find(OVERVIEW_TEMPLATE));
            tmpl.set("symbols", sensors);
            return tmpl;
        }
    }

}
