/*
 * 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 SendOrder {

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

	private SrvrSessionListener listener;
	private SrvrSession srvrSession;
	private Table ordersTable;
	private Table dealsTable;
	private Transaction transaction;

	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);
		}

		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 {
				ordersTable = new Table(srvrSession, "ORDERS_PSFU", new TableListenerImpl());
				ordersTable.setOnlineUpdates(true);
				dealsTable = new Table(srvrSession, "DEALS_PSFU", new TableListenerImpl());
				dealsTable.setOnlineUpdates(true);

				transaction = new Transaction(srvrSession, "FUTADDORDER");
				Row transactionRow = transaction.createRow();
				transactionRow.setStringByAcronym("I_SECBOARD", "PSFU");
				transactionRow.setStringByAcronym("I_SECCODE", "RTS-9.13");
				transactionRow.setStringByAcronym("I_BUYSELL", "B");
				transactionRow.setIntByAcronym("I_QUANTITY", 1);
				transactionRow.setIntByAcronym("I_PRICE", 126370);
				transactionRow.setStringByAcronym("I_MKTLIMIT", "M");
				transactionRow.setStringByAcronym("I_BROKERREF", "060");
				transactionRow.setStringByAcronym("I_ACCOUNT", "MF00");
				transaction.execute(transactionRow);
			} 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("SendOrder example");
		System.out.println("Press ENTER to quit");
		SendOrder example = new SendOrder();
		example.run();
		System.in.read();
	}
}
