package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class ErrorFixAttempt implements IStrategy {
    private IConsole console;
    private IContext context;
    private IDataService dataService;
    private IEngine engine;
    private IHistory history;
    private IIndicators indicators;
    private IUserInterface userInterface;
    
    @Configurable("Instrument")
    public Instrument tInstrument = Instrument.EURUSD;
    @Configurable("Period")
    public Period tPeriod = Period.FOUR_HOURS;
    @Configurable("Sample Size")
    public int sampleSize = 10000;
    @Configurable("Maximum Request Size")
    public int maxRequest = 100;
    
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.context = context;
        this.dataService = context.getDataService();
        this.engine = context.getEngine();
        this.history = context.getHistory();
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        
        List<IBar> bars = new ArrayList<IBar>();
        List<IBar> barsTemp;
        long from;
        int fromBars;
        long to;
        int toBars;
        List<IFXSentimentIndexBar> sentInstrument = new ArrayList<IFXSentimentIndexBar>();
        List<IFXSentimentIndexBar> sentInstrumentTemp;
        List<IFXSentimentIndexBar> sentPrimaryCurrency = new ArrayList<IFXSentimentIndexBar>();
        List<IFXSentimentIndexBar> sentPrimaryCurrencyTemp;
        List<IFXSentimentIndexBar> sentSecondaryCurrency = new ArrayList<IFXSentimentIndexBar>();
        List<IFXSentimentIndexBar> sentSecondaryCurrencyTemp;
        
        
        for (int i = 0; i < sampleSize/maxRequest + 1; i++) {
            fromBars = sampleSize - i * maxRequest;
            toBars = sampleSize - (i + 1) * maxRequest + 1;
            if (toBars < 1) {
                toBars = 1;
            }
            console.getOut().println("Bar Request #" + (i + 1) + ": Requesting bars from index " + fromBars + " to index " + toBars);
            from = history.getBar(tInstrument, tPeriod, OfferSide.ASK, fromBars).getTime();
            to = history.getBar(tInstrument, tPeriod, OfferSide.ASK, toBars).getTime();
            
            barsTemp = history.getBars(tInstrument, tPeriod, OfferSide.ASK, Filter.NO_FILTER, from, to);
            sentInstrumentTemp = dataService.getFXSentimentIndex(tInstrument, tPeriod, from, to);
            sentPrimaryCurrencyTemp = dataService.getFXSentimentIndex(tInstrument.getPrimaryCurrency(), tPeriod, from, to);
            sentSecondaryCurrencyTemp = dataService.getFXSentimentIndex(tInstrument.getSecondaryCurrency(), tPeriod, from, to);
            
            bars.addAll(barsTemp);
            barsTemp.clear();
            sentInstrument.addAll(sentInstrumentTemp);
            sentInstrumentTemp.clear();
            sentPrimaryCurrency.addAll(sentPrimaryCurrencyTemp);
            sentPrimaryCurrencyTemp.clear();
            sentSecondaryCurrency.addAll(sentSecondaryCurrencyTemp);
            sentSecondaryCurrencyTemp.clear();
        }
        
        console.getOut().println("Printing Last Element From Total Data Request");
        console.getOut().println(bars.get(bars.size()-1));
        console.getOut().println(sentInstrument.get(sentInstrument.size()-1));
        console.getOut().println(sentPrimaryCurrency.get(sentPrimaryCurrency.size()-1));
        console.getOut().println(sentSecondaryCurrency.get(sentSecondaryCurrency.size()-1));
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
    
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}