//package jforex;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
///import CommonEvaluateData.Symbol;

import com.dukascopy.api.*;

public class CheckGetBar implements IStrategy {
	@SuppressWarnings( "unused"  )
	private IEngine engine;
	private IConsole console;
//	@SuppressWarnings( "unused"  )
	private IHistory history;
	private IContext context;
	@SuppressWarnings( "unused"  )
	private IIndicators indicators;
	//private IUserInterface userInterface;
	private PrintStream out;
	@SuppressWarnings( "unused"  )
	private PrintStream err;
	@SuppressWarnings( "unused"  )
	private IUserInterface gui;
	//
	
	long TimeFrom = 0;
	long TimeTo   = 0;
	//
	int  BarShiftFrom = 0;
	int  BarShiftTo   = 0;
	
	Set<Instrument> instruments = new HashSet<Instrument>();
		
	
	
	
	
	
	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.out = console.getOut();
		this.err = console.getErr();
		
		out.println( "STARTING..." );
		
		
		onStart_setInstruments();
		
		onStart_subscribeInstruments();
		
		onStart_setTimeFromTo();
		
		Period period = null;
		
		period = Period.FIVE_MINS;
		
		onStart_setBarShiftFromTo( period );
		
		onStart_checkGetBar( period );
		
		period = Period.FIFTEEN_MINS;
		
		onStart_setBarShiftFromTo( period );
		
		onStart_checkGetBar( period );
		
	}
	
	private void onStart_setInstruments() {
		//
		instruments.add( Instrument.EURUSD );
		instruments.add( Instrument.GBPCHF );
		instruments.add( Instrument.USDCHF );
		instruments.add( Instrument.USDDKK );
		//
	}
	
	private void onStart_subscribeInstruments() {
		//
		context.setSubscribedInstruments( instruments );		
	}
	
	private void onStart_setTimeFromTo() {
		//
		out.format( "onStart_setTimeFromTo: " );
		out.format( "%n" );
		//
		String TimeFromStr = "2012.08.01 00:00:00";
		String TimeToStr   = "2012.08.27 12:00:00";
		//
		final String SDTFormat = "yyyy.MM.dd HH:mm:ss";
		Calendar cal = Calendar.getInstance( new SimpleTimeZone( 0, "GMT" ) );
		final SimpleDateFormat sdf = new SimpleDateFormat( SDTFormat );
		sdf.setCalendar( cal );
		//
		try {
			TimeFrom = sdf.parse( TimeFromStr ).getTime();
		}
		catch ( Exception exc ) {
			//exc.printStackTrace( err );
			out.format( "onStart_setTimeFromTo: " );
			out.format( "TimeFrom: " );
			out.format( "Error!!" );
			out.format( "%n" );
		}
		try {
			TimeTo = sdf.parse( TimeToStr ).getTime();
		}
		catch ( Exception exc ) {
			//exc.printStackTrace( err );
			out.format( "onStart_setTimeFromTo: " );
			out.format( "TimeTo: " );
			out.format( "Error!!" );
			out.format( "%n" );
		}
	}
	
	private void onStart_setBarShiftFromTo( Period period ) {
		//
		out.format( "onStart_setBarShiftFromTo: " );
		out.format( "%n" );
		//
		long PerMill = period.getInterval();
		//
		long TimeToB = TimeTo - PerMill;
		//
		long StartTimeCB = -1;
		try {
			StartTimeCB = history.getStartTimeOfCurrentBar( Instrument.EURUSD, period );
		}
		catch ( Exception exc ) {
	    	out.format( "===" );
			out.format( "onStart_setBarShiftFromTo: " );
	    	out.format( "Period: %s; ", period.toString() );
			out.format( "Exception: %s; ", exc.getMessage() );
	    	out.format( "" );
	    	out.format( "%n" );
	    	//
	//		exc.printStackTrace( err  );
		}
		//
		BarShiftFrom = (int)( ( StartTimeCB - TimeFrom ) / PerMill );
		BarShiftTo   = (int)( ( StartTimeCB - TimeToB  ) / PerMill );
		//
	//	BarShiftFrom = 50000;
	//	BarShiftTo   = 1;
		//
		if ( BarShiftFrom <= 0 ) BarShiftFrom = 1;
		if ( BarShiftTo   <= 0 ) BarShiftTo   = 1;
		//
		out.format( "onStart_setBarShiftFromTo: " );
    	out.format( "Period: %s; ", period.toString() );
    	out.format( "BarShiftFrom: %d; ", BarShiftFrom );
    	out.format( "BarShiftTo: %d; ", BarShiftTo );
    	out.format( "" );
    	out.format( "%n" );
    	//
	}
		
	private void onStart_checkGetBar( Period period ) {
		//
		out.format( "onStart_checkGetBar: " );
		out.format( "%n" );
		//
		int ErrCountAct;
		int ErrCountMax = 50;
		//
		@SuppressWarnings( "unused" )
		IBar bar = null;
		//
	//	for ( Instrument instrument : Instrument.values() ) {
		for ( Instrument instrument : instruments ) {
			//
			out.format( "onStart_checkGetBar: " );
	    	out.format( "%s: ", instrument.toString() );
	    	out.format( "Period: %s; ", period.toString() );
	    	out.format( "" );
	    	out.format( "%n" );
	    	//
	    	ErrCountAct = 0;
	    	//
			for ( int i = BarShiftFrom; i >= BarShiftTo; i -- ) {
				//
				if ( ErrCountAct >= ErrCountMax ) continue;
				//
				try {
					bar = history.getBar( instrument, period, OfferSide.ASK, i );
					bar = history.getBar( instrument, period, OfferSide.BID, i );
				}
				catch ( Exception exc ) {
			    	out.format( "===" );
					out.format( "onStart_checkGetBar: " );
			    	out.format( "%s: ", instrument.toString() );
			    	out.format( "Period: %s; ", period.toString() );
					out.format( "Exception: %s; ", exc.getMessage() );
			    	out.format( "" );
			    	out.format( "%n" );
					//
			    	ErrCountAct ++;
				}
			}
		}
		//
		out.format( "onStart_checkGetBar: " );
    	out.format( "END" );
    	out.format( "" );
		out.format( "%n" );
		//
	}
	
	
	
	
	public void onStop() throws JFException {
		out.println( "STOPPING..." );
	}

	public void onAccount( IAccount account ) throws JFException {
	}

	public void onMessage( IMessage message ) throws JFException {
	}

	public void onTick( Instrument instrument, ITick tick ) throws JFException {
		//
	}
		
    public void onBar( Instrument instrument, Period period, IBar askBar, IBar bidBar ) throws JFException {
    	//
    }
    
    
	
    /*
	public void checkGetTicks() throws JFException {
		//
		Instrument instrument;
		//
		for ( int iSmb = 0; iSmb < symbol.length; iSmb ++ ) {
			//
			instrument = symbol[ iSmb ].instrument;
			//
			out.format( "checkGetTicks: " );
        	out.format( "%s: ", instrument.toString() );
			out.format( "" );
			out.format( "%n" );
			//
			for ( int i = 1; i <= 10; i ++ ) {
				//
				checkGetTicks_checkOne( instrument, Period.WEEKLY     , i );
				//
				checkGetTicks_checkOne( instrument, Period.DAILY      , i + 0 );
				//
				checkGetTicks_checkOne( instrument, Period.DAILY      , i + 1 );
				//
				checkGetTicks_checkOne( instrument, Period.DAILY      , i + 2 );
				//
				checkGetTicks_checkOne( instrument, Period.THIRTY_MINS, i + 0 );
				//
				checkGetTicks_checkOne( instrument, Period.THIRTY_MINS, i + 1 );
				//
				checkGetTicks_checkOne( instrument, Period.THIRTY_MINS, i + 2 );
				//
			}
		}
	}
	
	public void checkGetTicks_checkOne( Instrument instrument, Period period, int i ) throws JFException {
		//
		long TimeFrom = 0;
		long TimeTo   = 0;
		long Time     = history.getTimeOfLastTick( instrument );
		//
		TimeFrom = history.getStartTimeOfCurrentBar( instrument, period ) - period.getInterval() * i;
		TimeTo   = TimeFrom + period.getInterval();
		if ( TimeTo > Time ) TimeTo = Time - Time % period.getInterval();
		//
		if ( TicksList != null && !TicksList.isEmpty() ) TicksList.clear();
		//
	    try {
			TicksList = history.getTicks( instrument, TimeFrom, TimeTo - 1 );
	    }
	    catch ( Exception exc ) {
        	out.format( "===" );
			out.format( "fillTicksList: " );
        	out.format( "%s: ", instrument.toString() );
			out.format( "Exception: %s; ", exc.getMessage() );
        	out.format( "TmFrom %s; ", Utl.dateToStr( TimeFrom ) );
        	out.format( "TmTo %s; ", Utl.dateToStr( TimeTo ) );
			out.format( "" );
			out.format( "%n" );
	    }
	    //
		if ( AskFastBarsList != null && !AskFastBarsList.isEmpty() ) AskFastBarsList.clear();
		if ( BidFastBarsList != null && !BidFastBarsList.isEmpty() ) BidFastBarsList.clear();
	    //
		@SuppressWarnings( "unused" )
		double Volume = 0;
		//
    	for ( ITick tick : TicksList ) {
    		//
    		Volume += tick.getAskVolume() + tick.getBidVolume();
    		//
    	}
		//
	    try {
			AskFastBarsList = history.getBars( instrument, Period.ONE_MIN, OfferSide.ASK, TimeFrom, TimeTo );
			BidFastBarsList = history.getBars( instrument, Period.ONE_MIN, OfferSide.BID, TimeFrom, TimeTo );
	    }
	    catch ( Exception exc ) {
        	out.format( "===" );
			out.format( "fillFastBarsList: " );
        	out.format( "%s: ", instrument.toString() );
			out.format( "Exception: %s; ", exc.getMessage() );
        	out.format( "TmFrom %s; ", Utl.dateToStr( TimeFrom ) );
        	out.format( "TmTo %s; ", Utl.dateToStr( TimeTo ) );
			out.format( "" );
			out.format( "%n" );
	    }
		//
	    Volume = 0;
	    //
		for ( IBar bar : AskFastBarsList ) {
			//
    		Volume += bar.getVolume();
    		//
		}
		for ( IBar bar : BidFastBarsList ) {
			//
    		Volume += bar.getVolume();
    		//
		}
	    //
	}
*/	
	
}