package ru.mfd.api.examples;

import ru.mfd.api.*;

import java.util.Arrays;
import java.util.List;

/**
 * User: MMarkelov
 * Date: 08.07.13
 * Time: 13:21
 */
public class CreateClient {

	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 clientsTable;

	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);
			if (tableName.equals("CLIENTS") && row.getStringByAcronym("I_LOGIN", "").equals("testLogin")) {
				try {
					row.setStringByAcronym("I_COMMENT", "comment");
					clientsTable.modify(row);
				} catch (ClientException e) {
					System.err.println(e);
				}
			}
		}

		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 {
				clientsTable = new Table(srvrSession, "CLIENTS", new TableListenerImpl());
				clientsTable.setOnlineUpdates(true);

				Row transactionRow = clientsTable.createRow();
				transactionRow.setStringByAcronym("I_LOGIN", "testLogin");
				transactionRow.setStringByAcronym("I_CLIENT_LOGIN", "testLogin");
				transactionRow.setStringByAcronym("I_PASSWORD", "testLogin");
				transactionRow.setStringByAcronym("I_CLIENT_FULLNAME", "Test Login");
				transactionRow.setIntByAcronym("I_BROKER_STATUS", 0);
				transactionRow.setIntByAcronym("I_CRYPTO_TYPE", 0);
				clientsTable.insert(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("CreateClient example");
		System.out.println("Press ENTER to quit");
		CreateClient example = new CreateClient();
		example.run();
		System.in.read();
	}
}
