import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;  // Import the Scanner class

public class User{
	
	protected InetAddress addIP;
	protected String pseudo;
	protected int nport;
	protected boolean actif;
	
	protected User[] remoteUserList;
	public Boolean haveToStopThread=false;
	
	public User(int nport) throws IOException{
		//localUser
		try
		{
			this.addIP = InetAddress.getLocalHost();
		}
		catch(UnknownHostException e) {
			System.out.println("Could not find local address!");
		}
		
		this.nport = nport;
		
		this.setPseudo();
		
		this.actif = true;
		
	}
	
	public User(int nport,String pseudo) {
		//remoteUser
		try
		{
			this.addIP = InetAddress.getLocalHost();
		}
		catch(UnknownHostException e) {
			System.out.println("Could not find local address!");
		}
		this.pseudo = pseudo;
		this.nport = nport;
		this.actif = true;
		UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
		th.start();
	}
	
	
	public InetAddress getAddIP() {
		return addIP;
	}


	public void setAddIP(InetAddress addIP) {
		this.addIP = addIP;
	}


	public String getPseudo() {
		return pseudo;
	}


	public void setPseudo() throws IOException {
	    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
	    System.out.println("Enter pseudo :");
	    String tmpPseudo = myObj.nextLine();  // Read user input

		while(!(this.validatePseudo(tmpPseudo))) {
			System.out.println("Already taken, choose another one :");
		    tmpPseudo = myObj.nextLine();  // Read user input
		}
		
		this.pseudo = tmpPseudo;
	}
	
	public Boolean validatePseudo(String tmpPseudo) throws IOException {

		
		// Call broadcast 
		InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
		
		DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);

		byte[] buffer = new byte[256];
		
		DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);

		dgramSocket.send(outPacket);
		
		Boolean valid = true;
		
		Date oldDate = new Date();
        Date newDate = new Date();
		
        DatagramPacket inPacket;
        String response = null;
        String[] lstresponse= new String [3];
		dgramSocket.setSoTimeout(1000);

		
		System.out.println("Wait for pseudo validation ...");
		while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
			inPacket= new DatagramPacket(buffer, buffer.length);
			try{
				dgramSocket.receive(inPacket);
			}catch (Exception e) {
			
			} 
			buffer = inPacket.getData();
			response = new String(buffer);
			if(response!="\0") {
				System.out.println("Reponse : "+response);
			}
			lstresponse = response.split(":");
			System.out.println(lstresponse[0]);

			System.out.println(lstresponse[1]);

			System.out.println(lstresponse[2]);
			
			System.out.println(tmpPseudo);
			
			System.out.println(tmpPseudo==lstresponse[2]);

			valid=!(lstresponse[2].equals(tmpPseudo));
			if(!valid) {
				System.out.println("Déjà pris");
			}
	        newDate = new Date();
	        buffer = new byte[256];
		}
		System.out.println(valid);
		dgramSocket.close();
		
		return valid;
	}


	public int getNport() {
		return nport;
	}


	public void setNport(int nport) {
		this.nport = nport;
	}


	public boolean isActif() {
		return actif;
	}


	public void setActif(boolean actif) {
		this.actif = actif;
	}

	

	public static void main(String[] args) throws IOException {
			User activeUsr1 = new User(12228,"Mike");
		
			User usr1 = new User(12229);
			
			Scanner myObj = new Scanner(System.in);  // Create a Scanner object
		    System.out.println("Do you want to stop :");
		    String response = myObj.nextLine();  // Read user input
		    
		    if(response=="Yes") {
				activeUsr1.haveToStopThread=true;
		    }
	}
}
