package se.koc.trader.util;

import se.koc.trader.TraderContext;
import se.koc.trader.struct.Exchange;
import zutil.db.DBConnection;
import zutil.ui.Configurator;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * A value provider that will give all Enum values
 */
public class ConfigExchangeValueProvider implements Configurator.ConfigValueProvider<Exchange> {
    Map<String, Exchange> map = new HashMap<>();

    @Override
    public String[] getPossibleValues() {
        DBConnection db = TraderContext.getDB();

        try {
            for (Exchange exchange : Exchange.getExchanges(db)) {
                map.put(exchange.getName() + " (Id: " + exchange.getId() + ")", exchange);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return map.keySet().toArray(new String[0]);
    }

    @Override
    public Exchange getValueObject(String value) {
        return map.get(value);
    }
}