/*
 * Copyright 2012 IFH-InfoCentre, Ltd.
*/

package ru.mfd.api.examples;

import ru.mfd.api.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * User: MMarkelov
 * Date: 12.10.12
 * Time: 18:34
 */
public class GetAllTrades {

	private static final String host = "online.mfd.ru";
	private static final int port = 2700;
	private static final String login = "";
	private static final String password = "";
	private static final int connectionTimeout = 60000;

	private SrvrSessionListener listener;
	private SrvrSession srvrSession;
	private Table table;

	private class TableListenerImpl implements TableListener {
		public void onData(String code, String tableName, List<Row> rows) {
			System.out.println("onData: " + tableName + "(" + rows.size() + ")");
		}

		public void onDataComplete(String code, String tableName) {
			System.out.println("onDataComplete: " + tableName);
			try {
				table.addUpdateFilter(Filter.ROOT_NODE, Filter.ELogicalOperation.AND, "I_NAME", Filter.ECondition.EQUAL, "RUR");
				table.addUpdateFilterView("I_RID", "I_NAME", "I_LAST", "I_BID", "I_ASK");
				table.setOnlineUpdates(true);
			} catch (ClientException e) {
				System.err.println(e);
			}
		}

		public void onInsert(String tableName, Row row) {
			System.out.println("onInsert: " + row);
		}

		public void onPartInsert(String tableName, Row row) {
			System.out.println("onPartInsert: " + row);
		}

		public void onModify(String tableName, Row row) {
			System.out.println("onModify: " + row);
		}

		public void onDelete(String tableName, Long recid) {
			System.out.println("onDelete: " + tableName + " " + recid);
		}

		public void onClear(String tableName) {
			System.out.println("onClear: " + tableName);
		}

		public void onClose(String tableName) {
			System.out.println("onClose: " + tableName);
		}
	}

	private class Listener implements SrvrSessionListener {
		public void onLogin(int result, String description) {
			if (result != 0) {
				System.err.println("onLogin: failed [" + result + "] " + description);
				srvrSession.disconnect();
				return;
			}

			try {
				table = new Table(srvrSession, "FEV", new TableListenerImpl());
				List<Filter> filters = new ArrayList<Filter>();
				filters.add(table.createFilter("I_NAME", Filter.ECondition.EQUAL, "RUR"));
				filters.add(table.createFilter("I_DATE", Filter.ECondition.EQUAL, new SimpleDateFormat("dd.MM.yyyy").format(new Date())));
				table.getData(null, filters);
			} catch (ClientException e) {
				System.err.println(e);
			}
		}

		public void onConnectionLost(String description) {
			System.err.println("onConnectionLost: " + description);
			try {
				Thread.sleep(1000);
				srvrSession.connect(host, port, login, password, listener, connectionTimeout);
			} catch (Exception ex) {
				System.err.println("Failed to reconnect: " + ex);
			}
		}

		public void onStatusMessage(int i, String[] s) {
			System.out.println("onStatusMessage: " + i + " " + Arrays.toString(s));
		}
	}

	private void run() {
		try {
			Runtime.getRuntime().addShutdownHook(new Thread() {
				public void run() {
					if (srvrSession != null) {
						srvrSession.disconnect();
					}
				}
			});

			listener = new Listener();
			srvrSession = new SrvrSession();
			srvrSession.connect(host, port, login, password, listener, connectionTimeout);
		} catch (Exception e) {
			System.err.println("Exception: " + e);
		}
	}

	public static void main(String[] args) throws Exception {
		System.out.println("GetData example");
		System.out.println("Press ENTER to quit");
		GetAllTrades example = new GetAllTrades();
		example.run();
		System.in.read();
	}
}
