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.log.LogUtil;
import zutil.parser.Templator;

import java.util.Map;
import java.util.logging.Logger;

import static zutil.ui.UserMessageManager.*;


public class SymbolConfigPage extends TraderPage {
    private static final Logger logger = LogUtil.getLogger();
    private static final String TEMPLATE = TraderContext.RESOURCE_WEB_ROOT + "/symbol_config.tmpl";


    public SymbolConfigPage() {
        super("symbol_config");
        super.getRootNav().createSubNav("Settings").createSubNav(this.getId(), "Symbol Settings").setWeight(200);
    }

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

        DBConnection db = TraderContext.getDB();

        // Save new input
        if (request.containsKey("action")) {
            int id = (ObjectUtil.isEmpty(request.get("id")) ? -1 : Integer.parseInt(request.get("id")));
            Symbol symbol = null;

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

            switch(request.get("action")) {
                case "create_symbol":
                    logger.info("Creating symbol: " + request.get("name"));
                    symbol = new Symbol();
                    /* FALLTHROUGH */
                case "modify_symbol":
                    if (symbol != null) {
                        logger.info("Modifying sensor: " + symbol.getName());
                        symbol.setName(request.get("name"));
                        symbol.save(db);

                        TraderContext.getMessageManager().add(new UserMessage(
                                MessageLevel.SUCCESS, "Successfully saved symbol: " + symbol.getName(), MessageTTL.ONE_VIEW));
                    } else {
                        logger.warning("Unknown sensor id: " + id);
                        TraderContext.getMessageManager().add(new UserMessage(
                                MessageLevel.ERROR, "Unknown sensor id: " + id, MessageTTL.ONE_VIEW));
                    }
                    break;

                case "remove_symbol":
                    if (symbol != null) {
                        logger.warning("Removing sensor: " + symbol.getName());
                        symbol.delete(db);

                        TraderContext.getMessageManager().add(new UserMessage(
                                MessageLevel.SUCCESS, "Successfully removed sensor: " + symbol.getName(), MessageTTL.ONE_VIEW));
                    } else {
                        logger.warning("Unknown sensor id: " + id);
                        TraderContext.getMessageManager().add(new UserMessage(
                                MessageLevel.ERROR, "Unknown sensor id: " + id, MessageTTL.ONE_VIEW));
                    }
                    break;
            }
        }

        // Output
        Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
        tmpl.set("symbols", Symbol.getSymbols(db));

        return tmpl;

    }

}
