package sandbox;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IDataService;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IFXSentimentIndex;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.IUserInterface;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.JFUtils;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;

public class ErrorDemoImproved implements IStrategy {
    // Declaring Initial Variables
    private IAccount account;
    private IConsole console;
    private IContext context;
    private IDataService dataService;
    private IEngine engine;
    private IHistory history;
    private IIndicators indicators;
    private IUserInterface userInterface;
    private JFUtils utils;
    
    // Declaring Configurable Variables
    @Configurable("Trading Period")
    public Period tradingPeriod = Period.FOUR_HOURS;
    
    // Declaring Other Variables
    private double primarySentimentChange;
    private double secondarySentimentChange;
    private IBar prevAskBar;
    private IBar prevBidBar;
    
    private DateFormat dfm = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSS");
    
    public void onStart(IContext context) throws JFException {
        this.account = context.getAccount();
        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();
        this.utils = context.getUtils();
        dfm.setTimeZone(TimeZone.getTimeZone("GMT"));
    }

    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 {
        // Filtering Non-Trading Periods
        if (!period.equals(tradingPeriod)) {
            return;
        }
        
        // Collecting Previous Bar Data
        prevAskBar = history.getBar(instrument, period, OfferSide.ASK, 2);
        prevBidBar = history.getBar(instrument, period, OfferSide.BID, 2);
        
        // Calculating Sentiment
        console.getOut().println("--------------------------------------------------");
        console.getOut().println("Calculating sentiment indices for: " + instrument.name() + ", current time: " + dfm.format(askBar.getTime()) + "(" + askBar.getTime() + ")" + ", previous time: " + dfm.format(prevAskBar.getTime()) + "(" + prevAskBar.getTime() + ")");
        IFXSentimentIndex currentPrimarySentimentIndex = dataService.getFXSentimentIndex(instrument.getPrimaryCurrency(), askBar.getTime());
        IFXSentimentIndex previousPrimarySentimentIndex = dataService.getFXSentimentIndex(instrument.getPrimaryCurrency(), prevAskBar.getTime());
        console.getOut().println(String.format("Current Pirmary: %s", currentPrimarySentimentIndex));
        console.getOut().println(String.format("Previous Primary: %s", previousPrimarySentimentIndex));

        if ((currentPrimarySentimentIndex != null) && (previousPrimarySentimentIndex != null)){
        	primarySentimentChange = currentPrimarySentimentIndex.getIndexValue() - previousPrimarySentimentIndex.getIndexValue();
        	console.getOut().println(instrument.name() + " Primary Change: "+primarySentimentChange);
        }

        IFXSentimentIndex currentSecondarySentimentIndex = dataService.getFXSentimentIndex(instrument.getSecondaryCurrency(), askBar.getTime());
        IFXSentimentIndex previousSecondarySentimentIndex = dataService.getFXSentimentIndex(instrument.getSecondaryCurrency(), prevAskBar.getTime());
        console.getOut().println(String.format("Current Secondary: %s", currentSecondarySentimentIndex));
        console.getOut().println(String.format("Previous Secondary: %s", previousSecondarySentimentIndex));

        if ((currentSecondarySentimentIndex != null) && (previousSecondarySentimentIndex != null)){
        	secondarySentimentChange = currentSecondarySentimentIndex.getIndexValue() - previousSecondarySentimentIndex.getIndexValue();
        	console.getOut().println(instrument.name()+" Secondary Change: "+secondarySentimentChange);
        }
    }
}