package controller;

import model.Chat;
import model.LocalUser;
import model.Msg_Text;
import model.RemoteUser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ListeningThreadTCPChat extends ListeningThread{
		
	private Socket socket;
	
	/* CONSTRUCTOR OF ListeningThreadTCPConnection
	 * @parametres 
	 * 		@param s : String => nom du thread
	 * 		@param myUser : User => utilisateur utilisant ce thread
	 * @description
	 * 	<p>
	 * 	</p>
	 */
	public ListeningThreadTCPChat(String s,Controller controller,Socket socket) { 
		super(s,controller);
		this.socket=socket;
		// TODO OpenSession si ouverture exterieur
	}
	
	/* run
	 * @description
	 * <p>
	 * 		
	 * </p>
	 */
	public void run() {
			/**** function variables ****/
			BufferedReader in = null;
			String msg = null;
			String input;
			String dateString;
			Date date;
		    Calendar date1=Calendar.getInstance();

		    
		    Chat c = null;
		    /************         Check rm user information            **********/
		    /*
		    RemoteUser rm = new RemoteUser("Unknown",this.socket.getInetAddress(),this.socket.getPort());
		    int indexRM = controller.myUser.getActiveUserIndexOf(rm);
		    // Check if rm is identifiable
		    System.out.println(rm);
		    if(indexRM!=-1) {
		    	rm = controller.myUser.getRemoteUsersList().get(indexRM);
		    	// Check if chat already created
		    	int indexChat = controller.myUser.getChatIndexOf(rm);
		    	if(indexChat!=-1) {
		    		System.out.println("("+this.controller.myUser.getPseudo()+") Session déjà créer, on recupère le chat associé");
		    		c = controller.myUser.getChats().get(indexChat);
		    	}
		    	else {
		    		c = this.controller.myUser.addChats(rm);
		    		this.controller.openSession(c);
		    	}
		    }
		    else {
		    	System.out.println("("+this.controller.myUser.getPseudo()+") Remote User unidentifiable => CLOSING CONNECTION");
		    }
		   */
		    /*** listening tcp message from rm until session is close ***/
			try {
				System.out.println("("+this.controller.myUser.getPseudo()+") WAIT FOR NEW MESSAGE FROM ");//+rm.getPseudo());
				in =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				while (!(input=in.readLine()).equals("end")/* && c.getActive()*/) {
					System.out.println("("+this.controller.myUser.getPseudo()+") recoit : "+input);
					//c.addMessage(new Msg_Text(rm.getAddIP(),input));
					// TODO Prévenir l'interface de la réception d'un nouveau message

				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			

	
	}
	
	/* close
	 * @description
	 * <p>
	 * 		ferme le socket d'écoute TCP pour le chat
	 * 		interrupt TCP listening thread pour le chat
	 * </p>
	 */
	public void close() {
		try {
			this.socket.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		System.out.println("End of listing thread UDP ("+this.controller.myUser.getPseudo()+")");
		try {
			this.interrupt();
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
