package network;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
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.util.ArrayList;

import model.Contact;
import model.MessageChat;

public class TCPChat extends Thread{
	
	private ArrayList<MessageChat> listeMessagesRecu;
	private Socket socket; 
	private int destID;
	private PropertyChangeSupport support;
	
	
	//connexion faite par un autre user
	public TCPChat(Socket socket) {
		this.socket = socket;
		listeMessagesRecu = new ArrayList<MessageChat>();
		this.support = new PropertyChangeSupport(this);
		this.start();
	}
	
	//connexion faite par le user
	public TCPChat(Contact destUser, int destID) throws IOException {
		this.socket = new Socket(destUser.getAddress(), destUser.getPort());
		listeMessagesRecu = new ArrayList<MessageChat>();
		this.destID = destID;
		this.support = new PropertyChangeSupport(this);
		this.start();
	}
	
	public TCPChat(InetAddress adr, int port) throws IOException {
		this.socket = new Socket(adr, port);
		listeMessagesRecu = new ArrayList<MessageChat>();
		this.support = new PropertyChangeSupport(this);
		this.start();
	}
	
	public void addPropertyChangeListener(PropertyChangeListener pcl) {
		this.support.addPropertyChangeListener("Msg TCP Recu", pcl);
		this.support.addPropertyChangeListener("deconnexion",pcl);
	}
	
	
	@Override
	public void run() {
		BufferedReader in;
		try {
			
			in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			
			while(true) {
				
				try {
						String input = in.readLine();
						MessageChat msgRecu = new MessageChat(this.socket.getInetAddress(), this.socket.getLocalAddress(), this.socket.getPort(), this.socket.getLocalPort(), 0, input);
						listeMessagesRecu.add(msgRecu);
						this.support.firePropertyChange("Msg TCP Recu", true, false);
				} catch (IOException e) {
					this.support.firePropertyChange("deconnexion",true,false);
					System.out.println("Création et read du in impossible");
					break;
				}
				
			}
		} catch (IOException e1) {
			System.out.println("pb création in");
			e1.printStackTrace();
		}
	}
	
	public void sendMsg(String message) throws IOException {
		PrintWriter out = new PrintWriter(this.socket.getOutputStream(),true);
		out.println(message);
	}
	
	
	public MessageChat getMessageRecu() {
		return listeMessagesRecu.remove(0);
	}

	public ArrayList<MessageChat> getListeMessagesRecu() {
		return listeMessagesRecu;
	}

	public void setListeMessagesRecu(ArrayList<MessageChat> listeMessagesRecu) {
		this.listeMessagesRecu = listeMessagesRecu;
	}

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public int getDestID() {
		return destID;
	}

	public void setDestID(int destID) {
		this.destID = destID;
	}
	
	public boolean isConnected() {
		return this.socket != null && socket.isConnected() && !socket.isClosed();
	}
}
