package org.dummyclient;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.LoadingProgressListener;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.system.ITesterClient;
import com.dukascopy.api.system.TesterFactory;

public class DummyClient {
	private static final Logger LOGGER = LoggerFactory.getLogger(DummyClient.class);
	
	public static void main(String[] args) throws Exception {
		LOGGER.info("starting");
		ITesterClient client = TesterFactory.getDefaultInstance();
		client.setProcessingStatsEnabled(true);
		client.setEventLogEnabled(true);
		LOGGER.info("starting connection");
		client.connect("https://www.dukascopy.com/client/demo/jclient/jforex.jnlp", "xxx", "xxx");
		LOGGER.info("connection done");
		// wait for it to connect
		int i = 10; // wait max ten seconds
		while (i > 0 && !client.isConnected()) {
			Thread.sleep(1000);
			i--;
			LOGGER.info("waiting for connection...");
		}
		if (!client.isConnected()) {
			System.exit(1);
		}
		LOGGER.info("client connected");

		// set instruments that will be used in testing
		Set<Instrument> instruments = new HashSet<Instrument>();
		instruments.add(Instrument.AUDCAD);
		client.setSubscribedInstruments(instruments);
		// setting initial deposit
		client.setInitialDeposit(Instrument.EURUSD.getSecondaryCurrency(), 1000000);

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		df.setTimeZone(TimeZone.getTimeZone("GMT"));

		Period testInterval = Period.FOUR_HOURS;
		OfferSide offerSide = OfferSide.BID;
		ITesterClient.InterpolationMethod ipMethod = ITesterClient.InterpolationMethod.FOUR_TICKS;
		Date startTestDate;
		Date endTestDate = new Date();
		try {
			startTestDate = df.parse("20012-06-12 00:00:00");
			endTestDate = df.parse("2012-06-13 00:00:00");
			client.setDataInterval(testInterval, offerSide, ipMethod, startTestDate.getTime(), endTestDate.getTime());
		} catch (ParseException e1) {
			e1.printStackTrace();
			System.exit(1);
		}
		IStrategy strategy = new IStrategy() {

			@Override
			public void onAccount(IAccount arg0) throws JFException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onBar(Instrument arg0, Period arg1, IBar arg2, IBar arg3) throws JFException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onMessage(IMessage arg0) throws JFException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStart(IContext arg0) throws JFException {
				LOGGER.info("started");
			}

			@Override
			public void onStop() throws JFException {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTick(Instrument arg0, ITick arg1) throws JFException {
				// TODO Auto-generated method stub
				
			}
			
		};
		LOGGER.info("starting strategy");
		long pid = client.startStrategy(strategy, new LoadingProgressListener() {
			public void dataLoaded(long startTime, long endTime, long currentTime, String information) {
				LOGGER.info("dataLoaded: "+information);
			}

			public void loadingFinished(boolean allDataLoaded, long startTime, long endTime, long currentTime) {
				LOGGER.info("loading finished");
			}

			public boolean stopJob() {
				LOGGER.info("Loading stopped");
				return false;
			}
		});
	}
}
