package jforex.test;

import java.util.HashMap;
import java.util.Map;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class DeserializeApiParams implements IStrategy {    
    
    Map<String,Class> apiObjectNames = new HashMap<String,Class> (){{
        put("TYPICAL_PRICE", AppliedPrice.class);
        put("BID", OfferSide.class);
        put("TEN_MINS", Period.class);
    }};   

    @Override
    public void onStart(IContext context) throws JFException {
        
        for(Map.Entry<String, Class> entry : apiObjectNames.entrySet()){
            String name = entry.getKey();
            Class clazz = entry.getValue();
            Object o = null;
            if(clazz.isEnum()){
                o = Enum.valueOf(clazz, name);
            } else {
                try {
                    //we assume the field to be of the same class as clazz and that it is static field (no need to pass object instance)
                    o = clazz.getField(name).get(null);
                } catch (Exception e) {                    
                    e.printStackTrace(context.getConsole().getErr());
                }
            }
            context.getConsole().getOut().println(o.getClass().getSimpleName() + " - " + o.toString());
        }                    
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {}

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}

    @Override
    public void onMessage(IMessage message) throws JFException {}

    @Override
    public void onAccount(IAccount account) throws JFException {}

    @Override
    public void onStop() throws JFException {}
}
