package jforex.orders;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.dukascopy.api.*;

public class OrderHistoryLastWeekAllInstr implements IStrategy {

    private IConsole console;
    
    private Set<Instrument> instruments = new HashSet<Instrument>(
            Arrays.asList(Instrument.values())
    );  
    
    @Override
    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        
        context.setSubscribedInstruments(instruments);
        
        //wait on subscription
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        Set<Instrument> subscribedInstruments = context.getSubscribedInstruments();
        Set<Instrument> unSubscribedInstruments = new HashSet<Instrument>(instruments); 
        unSubscribedInstruments.removeAll(subscribedInstruments);
        
        console.getOut().println(String.format("subscribed: %s \nunsubscribed: %s", subscribedInstruments, unSubscribedInstruments));

        long time = System.currentTimeMillis();
        for(Instrument instrument : subscribedInstruments){
            console.getOut().println(instrument + " history orders: ");
            List<IOrder> orders = context.getHistory().getOrdersHistory(instrument, time - Period.MONTHLY.getInterval(), time + Period.WEEKLY.getInterval());
            if (orders != null) {
                for (IOrder order : orders) {
                    console.getOut().println(instrument + " history order: " + order );            
                    }
            }
        }
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
    public void onMessage(IMessage message) throws JFException {}
    public void onAccount(IAccount account) throws JFException {}
    public void onStop() throws JFException {}

}
