package jforex.strategies;

import java.text.SimpleDateFormat;

import com.dukascopy.api.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Strategy1 implements IStrategy {

    private IConsole console;
    private IHistory history;

    @Configurable("time")
    public Calendar time = new GregorianCalendar();
    
    
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.history = context.getHistory();
        
//        // this code sets time variable to 12:30 GMT, tomorrow
//        time = new GregorianCalendar();
//        time.setTimeZone(TimeZone.getTimeZone("GMT")); // time zone GMT
//        
//        time.setTimeInMillis(history.getLastTick(Instrument.EURUSD).getTime()); // set time to last tick
//        
//        time.add(Calendar.DATE, 1); // add one day to the time
//        
//        time.set(Calendar.HOUR_OF_DAY, 12); // set hours
//        time.set(Calendar.MINUTE, 30);      // set minutes
//        
//        console.getOut().println(toStr(time.getTimeInMillis()));
        
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (instrument != Instrument.EURUSD) {
            return;
        }
        
        if(time != null && time.getTimeInMillis() <= tick.getTime()) {
            console.getOut().println("Time is now " + toStr(time.getTimeInMillis()) + " " + toStr(tick.getTime()) );
        }        
    }

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

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }
    
    public String toStr(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") {
            {
                setTimeZone(TimeZone.getTimeZone("GMT"));
            }
        };
        return sdf.format(time);
    }
}