package smclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
 * This class is a connection wrapper
 * for the SM protocol.
 * It's for connecting to an SM Server
 * It's for Protocol version 1 of SMprot
 * @author miko
 * @version 1.0.0
 * @category Protocolwrapper
 * @date 20080829
 */
public class SMclientCom {

	private Socket conn;
	private BufferedReader br;
	private PrintWriter pw;
	private String ip;
	private int port;
	private String nick;
	private String passwd;
	
	/**
	 * Constructor for SMclientCom
	 * Needs an IP String and an Port as an integer
	 * @param pIp
	 * @param pPort
	 * @throws Exception
	 */
	public SMclientCom(String pIp, int pPort) throws Exception {
		ip = pIp;
		port = pPort;
		conn = new Socket(ip, port);
		
		try {
			br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			pw = new PrintWriter(conn.getOutputStream());
		} catch (Exception ex) {
			throw new Exception("Broken Connectionpointer");
		}
	}
	/**
	 * Functions for receiving and sending data
	 * 
	 */
	/**
	 * This Function is reading one Line from the
	 * connection buffer
	 * @return String
	 */
	public String readLine() {
		String data;
		try { data = br.readLine(); }
		catch (Exception ex) { System.err.println("Can't read from buffer"); return null; }
		//if (data.length() > 0) { data.substring(0, data.length() - 1); }
		return data;
	}
	/**
	 * This Function sends the String pData and
	 * adds the endline character at the end
	 * @param String
	 */
	public void sendln(String pData) {
		try {
			pw.println(pData);
			pw.flush();
		} catch (Exception ex) {
			System.err.println("Can't send data");
		}
	}
	/**
	 * Implementation of protocol
	 * 
	 */
	/**
	 * Sends HELO message to server
	 * @param pNick
	 */
	public void HELO(String pNick) {
		nick = pNick;
		try {
			this.sendln("HELO " + nick);
		} catch (Exception ex) {
			System.err.println("Can't send HELO");
		}
	}
	/**
	 * Sends PW message to server
	 * @param pPasswd
	 * @return Server response
	 */
	public String PW(String pPasswd) {
		passwd = pPasswd;
		this.sendln("PW " + passwd);
		return this.readLine();
	}
	/**
	 * Registers a user on the server
	 * @param pUser
	 * @param pPasswd
	 */
	public void REG(String pUser, String pPasswd) {
		this.sendln("REG " + pUser + " " + pPasswd);
	}
	/**
	 * You get a list of all users who are
	 * registered on the server you are connected to.
	 * They are seperated with ';' like the SMprot standard
	 * version one provides.
	 * @return Serverawnser (String)
	 */
	public String LISTUSERS() {
		this.sendln("LISTUSERS");
		return this.readLine();
	}
	/**
	 * Returns you a list of all your Messages
	 * seperated by ';'.
	 * @return String with your Messages
	 */
	public String LISTMSGS() {
		this.sendln("LISTMSGS");
		return this.readLine();
	}
	/**
	 * Returns all messages which were never read
	 * @return One String seperated by ';'
	 */
	public String GETNEW() {
		this.sendln("MSG GETNEW");
		return this.readLine();
	}
	/**
	 * Closes active connection	affectionate
	 */
	public void QUIT() {
		this.sendln("QUIT");
		/* Automatic use is bad
		try {		
			this.close();
		} catch (Exception ex) {}
		*/
	}
	/**
	 * Alias for QUIT()
	 */
	public void EXIT() {
		this.QUIT();
	}
	/**
	 * Sends an Message to a user
	 * @param Receipient
	 * @param Message
	 */
	public void MSGTO(String pToUsr, String pMessage) {
		this.sendln("MSG MSGTO:" + pToUsr + ";" + pMessage);
	}
	/**
	 * Functions for controlling the connection
	 * 
	 */
	/**
	 * Connects to given serveraddress
	 */
	public void connect() throws Exception {
		conn = new Socket(ip, port);
	}
	/**
	 * Closeing the connection
	 * @throws Exception
	 */
	public void close() throws Exception {
		this.QUIT();
		conn.close();
	}
	/**
	 * Returns the active connection instance
	 * @return
	 */
	public Socket getConn() {
		return conn;
	}
}