package se.koc.trader.page;

import se.koc.trader.TraderContext;
import se.koc.trader.TraderServer;
import se.koc.trader.api.TraderPage;
import se.koc.trader.struct.ClassConfigurationData;
import se.koc.trader.struct.Exchange;
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.ArrayList;
import java.util.Map;
import java.util.logging.Logger;

import static zutil.ui.UserMessageManager.*;


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

    private ArrayList<ClassConfigurationData> classConfigurations = new ArrayList<>();

    public ExchangeConfigPage() {
        super("exchange_config");
        super.getRootNav().createSubNav("Settings").createSubNav(this.getId(), "Exchange Settings").setWeight(100);

        for (Class c : TraderServer.getAvailableExchangeConfigs())
            classConfigurations.add(new ClassConfigurationData(c));
    }

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

        DBConnection db = TraderContext.getDB();

        if (request.containsKey("action")) {
            int id = (ObjectUtil.isEmpty(request.get("id")) ? -1 : Integer.parseInt(request.get("id")));
            Exchange exchange = null;

            if (id >= 0)
                exchange = Exchange.getExchange(db, id);

            switch(request.get("action")) {
                case "create_exchange":
                    logger.info("Creating symbol: " + request.get("name"));
                    exchange = new Exchange();
                    /* FALLTHROUGH */
                case "modify_exchange":
                    if (exchange != null) {
                        logger.info("Modifying exchange: " + exchange.getName());
                        exchange.setName(request.get("name"));
                        exchange.setObjectClass(request.get("type"));
                        exchange.getObjectConfigurator().setValues(request).applyConfiguration();
                        exchange.save(db);

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

                case "remove_exchange":
                    if (exchange != null) {
                        logger.warning("Removing exchange: " + exchange.getName());
                        exchange.delete(db);

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

        // Output
        Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
        tmpl.set("exchanges", Exchange.getExchanges(db));
        tmpl.set("exchangeConfigClasses", TraderServer.getAvailableExchangeConfigs());
        tmpl.set("exchangeObjectConfigs", classConfigurations);

        return tmpl;

    }

}
