package jforex.strategies;

import java.awt.Color;
import java.util.*;

import com.dukascopy.api.*;
import com.dukascopy.api.bar.ITickBar;
import com.dukascopy.api.drawings.IChartObjectFactory;
import com.dukascopy.api.drawings.ILabelChartObject;
import com.dukascopy.api.listener.ITickBarFeedListener;


public class TickCountStrategy implements IStrategy {
    private IConsole console;
    private IHistory history;
    private IChart chart = null;

    private ILabelChartObject label;
    int tickCount;
    double price;

    @Configurable("Instrument")
    public Instrument strategyInstrument = Instrument.EURUSD;
    
    @Configurable("Tick bar size")
    public int tickBarSize = 10;

    double previousValue = 0;

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.history = context.getHistory();
        
        context.subscribeToTickBarFeed(Instrument.EURUSD, OfferSide.BID,  TickBarSize.valueOf(tickBarSize), new ITickBarFeedListener() {
            @Override
            public void onBar(Instrument instrument, OfferSide offerSide, TickBarSize size, ITickBar bar) {
                console.getOut().println("On Tick Bar " + " " + instrument + " " + offerSide + " " + tickBarSize + " " + bar);
                label = null; 
            }
        });
        
        for(IChart ch : context.getCharts(strategyInstrument)){
            this.chart = ch;
            Period chartPeriod = ch.getSelectedPeriod();
            Period basicPeriod = Period.isPeriodBasic(chartPeriod);
            if( basicPeriod == null ){
                basicPeriod = Period.getBasicPeriodForCustom(ch.getSelectedPeriod());
            }
            
            if(basicPeriod.equals(Period.TICK)){
                this.chart = ch;
                return;
            }
        }
        
        throw new JFException("There is no chart with tick bars");
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        print("Stopped!");
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument.equals(strategyInstrument)) {
            if(label == null){
                price = tick.getBid();
                tickCount = 1;
                label = drawLabel(tick.getTime(), tick.getBid());
            } else {
                price = Math.max(tick.getBid(), price);
                label.setPrice(0, Math.max(tick.getBid(), price));
                tickCount++;
                label.setText(tickCount+"");
                label.setTime(0, tick.getTime());
            }          
        }
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    public void print(Object o) {
        this.console.getOut().println(o.toString());
    }
    
    private ILabelChartObject drawLabel(long time, double price) {        
        IChartObjectFactory factory = chart.getChartObjectFactory();
        String key = ("label" + UUID.randomUUID().toString().replace('-', '0'));
        ILabelChartObject l = factory.createLabel(key, time, price);
        l.setText("1");
        l.setColor(Color.BLACK);
        chart.addToMainChart(l);
        return l;
    }
    
    
    
}
