import java.io.*;
import java.net.*;
import java.util.Vector;

import no.divvun.Analyzer.Client.*;
import no.divvun.Analyzer.Communication.*;
import no.divvun.Analyzer.Objects.*;


public class ClientSocket_anl implements DivvunRequestListener {
	
	private String host = "localhost";
	private int port = 8081;
	
	private Socket anlSocket;
	private PrintWriter out;
	private BufferedReader in;
	
	
	private boolean bPreprocess;
	private boolean bAnalyze;
	private boolean bGenerate;
	private boolean bHyphenate;
	private boolean bDisamb;
	private boolean bParadigm;
	private String sLanguage;
	private String sFst;
	
	
	public ClientSocket_anl(boolean aPreprocess, boolean aAnalyze,
						  boolean aGenerate, boolean aHyphenate,
						  boolean aDisamb, boolean aParadigm,
						  String aLanguage, String aFst)
		throws IOException
	{
		bPreprocess = aPreprocess;
		bAnalyze = aAnalyze;
		bGenerate = aGenerate;
		bHyphenate = aHyphenate;
		bDisamb = aDisamb;
		bParadigm = aParadigm;
		sLanguage = aLanguage;
		sFst = aFst;
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		String word;
		DivvunBasicServices srv = null;
		
		try {
			srv = DivvunRequestManager.newDivvunBasicServices(this, host, port);
		}
		
		catch (Exception e) {
			System.out.println(e.getMessage());
			System.exit(-1);
		}
		
		Parameters param = new Parameters();
		param.setLang("sme");
		param.setParadigm(true);
		param.setXml_in(true);
		param.setXml_out(true);
		srv.initServer(param);
		
		do {
			System.out.println("Word?");
			word = stdin.readLine();
			
/*			Analysis analysis = srv.getParadigm(word);
			if (analysis != null) {
				System.out.println(analysis.getWord());
				for (int i = 0; i < analysis.size(); i++) {
					System.out.println(analysis.get(i).getReading().getValue());
				}
			}
*/
			Paradigm paradigm = srv.getFullParadigm(word, "N");
			if (paradigm != null) {
				System.out.println(paradigm.getWord());
				for (int i = 0; i < paradigm.size(); i++) {
					System.out.println(paradigm.get(i).getReading().getValue() + "\t" +
									  paradigm.get(i).getReading().getNext().getValue());
				}
			}
		
		} while (!word.equalsIgnoreCase("quit"));

		try {
			srv.disconnect();
		}
		
		catch (IOException e) {
			System.out.println(e.getMessage());
			System.exit(-1);
		}
	}
	
	public ClientSocket_anl() throws IOException {
		this(false, true, false, false, false, false, "sme", "");
	}
	
	public void setHost (String aHost) {
		host = aHost;
	}
	
	public String getHost() {
		return host;
	}
	
	public void setPort (int aPort) {
		port = aPort;
	}
	
	public int getPort () {
		return port;
	}
	
	public void open () {
		anlSocket = null;
		out = null;
		in = null;
		
		try {
			anlSocket = new Socket(host, port);
			out = new PrintWriter(anlSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(anlSocket.getInputStream()));
			
			InitRequest request = new InitRequest();
			request.getParameters().setLang(sLanguage);
//			request.getParameters().setPreprocess(true);
			request.getParameters().setAnalyze(true);
			request.getParameters().setXml_out(true);
			
			System.out.println(in.readLine());

			out.print(request.toXML());

			/*
			System.out.println(in.readLine());
			out.println((int)(bPreprocess?1:0) + "," + (int)(bAnalyze?1:0) + "," +
					    (int)(bGenerate?1:0) + "," + (int)(bHyphenate?1:0) + "," +
					    (int)(bDisamb?1:0) + "," + (int)(bParadigm?1:0));
			out.println(sLanguage);
			System.out.println(in.readLine());
			out.println();
			*/
		}
		catch (UnknownHostException e) {
			System.err.println("Don't know about: " + host + ":" + port);
			System.exit(1);
		}
		catch (IOException e) {
			System.err.println("Don't get IO from: " + host +":" + port);
			System.exit(1);
		}
	}
	
	public void close () {
		try {
			out.print(XMLTags.END_CLIENT);
			out.close();
			in.close();
			anlSocket.close();
		}
		catch (IOException e) {
			System.err.println("Don't get IO from host: victorio.uit.no.");
			System.exit(1);
		}
	}
	
	public Vector analyze(String word) {
		String line;
		Vector<String> analys = new Vector<String>();
		
		out.println(word);
		try {
			while (!(line = in.readLine()).equals(XMLTags.END_RESPONSE)) {
//				System.out.println(line);
				analys.add(line);
			}
		}
		catch (IOException e) {
			System.err.println("Don't get IO from host: victorio.uit.no.");
			System.exit(1);
		}
		
		return analys;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		
		ClientSocket_anl anl = new ClientSocket_anl();
	}
	
	public void requestStatusChanged(DivvunRequestStatus status) {
//		System.out.println(status.getStatus());
	}
	
	public void requestCompleted(Response response) {
//		System.out.println("Response: " + response.getErrorMessage());
	}
}
