package jforex;

import com.dukascopy.api.*;
import com.dukascopy.api.drawings.IChartObjectFactory;
import com.dukascopy.api.drawings.ILabelChartObject;
import java.awt.Color;
import java.awt.Font;

public class SimpleLabel implements IStrategy {
	private IEngine engine;
	private IConsole console;
	private IHistory history;
	private IContext context;
	private IIndicators indicators;
	private IUserInterface userInterface;
    private IChart chart;
    @Configurable("Instrument")
    private Instrument     selectedInstrument     = Instrument.EURUSD;
    private Period         selectedPeriod;
	
	public void onStart(IContext context) throws JFException {
		this.engine = context.getEngine();
		this.console = context.getConsole();
		this.history = context.getHistory();
		this.context = context;
		this.indicators = context.getIndicators();
		this.userInterface = context.getUserInterface();
        this.chart = context.getChart(selectedInstrument);
        this.selectedPeriod = chart.getSelectedPeriod();
	}

	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 {
        if(!instrument.equals(selectedInstrument)) {
            return;
        }
        drawLabel(tick.getTime(), tick.getAsk());
	}
	
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
    
    private ILabelChartObject label = null;
    
    private void drawLabel(long time, double price) {
        long timeDelta = selectedPeriod.getInterval() / 2;
        if(label == null) {
	        IChartObjectFactory factory = chart.getChartObjectFactory();
	        label = factory.createLabel("label", time + timeDelta, price);
            label.setOpacity(1);
            label.setColor(Color.BLUE);
        } else {
	        label.setPrice(0, price);
	        label.setTime(0, time + timeDelta);
        }        
        label.setText(String.valueOf(price), Font.BOLD);
        chart.addToMainChart(label);
    }
}