//package jforex;

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

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.Type;

public class CheckGetBar0 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>();
	
	
	private CSymbol symbols[] = {
			new CSymbol( Instrument.AUDCAD, true ),
			new CSymbol( Instrument.AUDCHF, true ),
			new CSymbol( Instrument.AUDJPY, true ),
			new CSymbol( Instrument.AUDNZD, true ),
			new CSymbol( Instrument.AUDUSD, true ),
			new CSymbol( Instrument.CADCHF, true ),
			new CSymbol( Instrument.CADJPY, true ),
			new CSymbol( Instrument.CHFJPY, true ),
			new CSymbol( Instrument.EURAUD, true ),
			new CSymbol( Instrument.EURCAD, true ),
			new CSymbol( Instrument.EURCHF, true ),
			new CSymbol( Instrument.EURGBP, true ),
			new CSymbol( Instrument.EURJPY, true ),
			new CSymbol( Instrument.EURNOK, true ),
			new CSymbol( Instrument.EURNZD, true ),
			new CSymbol( Instrument.EURSEK, true ),
			new CSymbol( Instrument.EURUSD, true ),
			new CSymbol( Instrument.GBPAUD, true ),
			new CSymbol( Instrument.GBPCAD, true ),
			new CSymbol( Instrument.GBPCHF, true ),
			new CSymbol( Instrument.GBPJPY, true ),
			new CSymbol( Instrument.GBPNZD, true ),
			new CSymbol( Instrument.GBPUSD, true ),
			new CSymbol( Instrument.NZDCAD, true ),
			new CSymbol( Instrument.NZDCHF, true ),
			new CSymbol( Instrument.NZDJPY, true ),
			new CSymbol( Instrument.NZDUSD, true ),
			new CSymbol( Instrument.USDCAD, true ),
			new CSymbol( Instrument.USDCHF, true ),
			new CSymbol( Instrument.USDDKK, true ),
			new CSymbol( Instrument.USDJPY, true ),
			new CSymbol( Instrument.USDSEK, true ),
	};
	
	
	
	
	@Override
	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_subscribeSymbols();
		
		Period period = null;
		
		period = Period.ONE_MIN;
		
		onStart_checkBar0( period );
		
		period = Period.FIVE_MINS;
		
		onStart_checkBar0( period );
		
		period = Period.FIFTEEN_MINS;
		
		onStart_checkBar0( period );
		
		period = Period.ONE_HOUR;
		
		onStart_checkBar0( period );
		
	}
	
	private void onStart_subscribeSymbols() {
		//
		if ( engine.getType() == Type.TEST ) return;
		//
		out.format( "onStart_subscribeSymbols: " );
		out.format( "%n" );
		//
		Set<Instrument> subinstruments = new HashSet<Instrument>();
		//
		for ( CSymbol symbol : symbols ) {
			if ( !symbol.IsEnabled ) continue;
			//
			if ( !context.getSubscribedInstruments().contains( symbol.instrument ) ) {
				if ( subinstruments.isEmpty() | !subinstruments.contains( symbol.instrument ) ) {
					subinstruments.add( symbol.instrument );
					//
					out.format( "..." );
					out.format( "%s: ", symbol.instrument.toString() );
					out.format( "%n" );
				}
			}
		}
		//
		context.setSubscribedInstruments( subinstruments, true );
		//
		try {
			Thread.sleep( 1000 );
		}
		catch ( Exception exc ) {
		}
		//
	    int i = 1;
	    while ( !context.getSubscribedInstruments().containsAll( subinstruments ) && i <= 5 ) {
            console.getOut().println( "...Instruments not subscribed yet " + i );
            //
    		try {
    			Thread.sleep( 1000 );
    		}
    		catch ( Exception exc ) {
    		}
	        i ++;
	    }
	}
	

	private void onStart_checkBar0( Period period ) {
		//
		out.format( "onStart_checkBar0: " );
		out.format( "%n" );
		//
		for ( CSymbol symbol : symbols ) {
			if ( !symbol.IsEnabled ) continue;
			//
			onStart_checkBar0_checkOne( symbol.instrument, period );
		}
		
		
	}
	
	private void onStart_checkBar0_checkOne( Instrument instrument, Period period ) {
		//
		int Shift = 0;
		//
		if ( instrument == null ) {
        	out.format( "===" );
			out.format( "getPeriodData: " );
			out.format( "instrument == null!! " );
			out.format( "Per: %s; ", period.toString() );
			out.format( "Shift: %d; ", Shift );
			out.format( "" );
			out.format( "%n" );
			//
			return;
		}
		if ( period == null ) {
        	out.format( "===" );
			out.format( "getPeriodData: " );
			out.format( "period == null!! " );
        	out.format( "%s: ", instrument.toString() );
			out.format( "Shift: %d; ", Shift );
			out.format( "" );
			out.format( "%n" );
			//
			return;
		}
		//
		int count_real = 0;
		int count_max  = 10;
		//
		IBar AskBar = null;
		IBar BidBar = null;
		//
		count_real = 0;
		while ( count_real < count_max ) {
			try {
				AskBar = history.getBar( instrument, period, OfferSide.ASK, Shift );
				BidBar = history.getBar( instrument, period, OfferSide.BID, Shift );
				if ( AskBar != null && BidBar != null ) {
					break;
				}
			}
			catch ( Exception exc ) {
				count_real ++;
/*
				if ( count_real >= count_max ) {
					//
					getXXX_showErrorInfo( StrCode, instrument, "getPeriodData", "Too much errors!", exc );
					//
					return pdata;
				}
*/
			}
		}
		//
		if ( AskBar == null || BidBar == null ) {
			if ( AskBar == null ) {
	        	out.format( "===" );
				out.format( "getPeriodData: " );
				out.format( "AskBar == null!! " );
	        	out.format( "%s: ", instrument.toString() );
				out.format( "Per: %s; ", period.toString() );
				out.format( "Shift: %d; ", Shift );
				out.format( "" );
				out.format( "%n" );
			}
			if ( BidBar == null ) {
	        	out.format( "===" );
				out.format( "getPeriodData: " );
				out.format( "BidBar == null!! " );
	        	out.format( "%s: ", instrument.toString() );
				out.format( "Per: %s; ", period.toString() );
				out.format( "Shift: %d; ", Shift );
				out.format( "" );
				out.format( "%n" );
			}
			//
			return;
		}
		else {
        	out.format( "+++" );
			out.format( "getPeriodData: " );
			out.format( "AskBar and BidBar are OK!! " );
        	out.format( "%s: ", instrument.toString() );
			out.format( "Per: %s; ", period.toString() );
			out.format( "Shift: %d; ", Shift );
			out.format( "" );
			out.format( "%n" );
		}
		//
		
		
		
		
		
		
	}
	
	
	
		
	
	
	
	
	@Override
	public void onStop() throws JFException {
		out.println( "STOPPING..." );
	}

	@Override
	public void onAccount( IAccount account ) throws JFException {
	}

	@Override
	public void onMessage( IMessage message ) throws JFException {
	}

	@Override
	public void onTick( Instrument instrument, ITick tick ) throws JFException {
		//
	}
		
	@Override
    public void onBar( Instrument instrument, Period period, IBar askBar, IBar bidBar ) throws JFException {
    	//
    }
    
	
}


class CSymbol {
	Instrument instrument;
	boolean IsEnabled = false;
	
	CSymbol( Instrument instrument, boolean IsEnabled ) {
		this.instrument = instrument;
		this.IsEnabled = IsEnabled;
	}

}

