package server;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;

import config.ConfLoad;
import config.Configuration;
import model.Contact;

public class Request {
	
	private static Configuration conf = ConfLoad.load();
	private static String url = conf.getsConf().getUrl();

	public static boolean sendLogin(String login, String type) throws IOException {
		
		boolean res;
		
		URL obj = new URL(url+"?login="+login+"&type="+type);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("GET");
		
		int responseCode = con.getResponseCode();
		String sRes = con.getHeaderField("result");
		System.out.println("GET Response Code :: " + responseCode);
		
		if (responseCode == HttpURLConnection.HTTP_OK) { 
			System.out.println("GET a réussi");
			
		} else {
			System.out.println("GET n'a pas fonctionné");
		}

		if(sRes.equals("true")) {
			res = true;
		}else {
			res = false;
		}
		return res;
	}
	
	public static boolean sendPseudo(String pseudo, String type) throws IOException {
		boolean res;
		
		URL obj = new URL(url+"?pseudo="+pseudo+"&type="+type);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("GET");
		
		int responseCode = con.getResponseCode();
		String sRes = con.getHeaderField("result");
		System.out.println("GET Response Code :: " + responseCode);
		
		if (responseCode == HttpURLConnection.HTTP_OK) { 
			System.out.println("GET a réussi");
			
		} else {
			System.out.println("GET n'a pas fonctionné");
		}
		
		if(sRes.equals("true")) {
			res = true;
		}else {
			res = false;
		}
		return res;
		
	}
	
	public static void sendPseudoChange(String pseudoAncien, String pseudo, String type) throws IOException {
				
		URL obj = new URL(url+"?pseudoVieux="+pseudoAncien+"&pseudo="+pseudo+"&type="+type);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("GET");
		
		int responseCode = con.getResponseCode();
		String sRes = con.getHeaderField("result");
		System.out.println("GET Response Code :: " + responseCode);
		
		if (responseCode == HttpURLConnection.HTTP_OK) { 
			System.out.println("GET a réussi");
			
		} else {
			System.out.println("GET n'a pas fonctionné");
		}
		
	}
	
	public static void sendUser(String pseudo, String adr, String port, String type) throws IOException {
		
		URL obj = new URL(url+"?pseudo="+pseudo+"&adr="+adr+"&port="+port+"&type="+type);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("GET");
		
		int responseCode = con.getResponseCode();
		String sRes = con.getHeaderField("result");
		System.out.println("GET Response Code :: " + responseCode);
		
		if (responseCode == HttpURLConnection.HTTP_OK) { 
			System.out.println("GET a réussi");
			
		} else {
			System.out.println("GET n'a pas fonctionné");
		}
		
		
	}
	
	public static void sendDeconnexion(String id, String pseudo, String type) throws IOException {
		
		URL obj = new URL(url+"?id="+id+"&pseudo="+pseudo+"&type="+type);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("GET");
		
		int responseCode = con.getResponseCode();
		String sRes = con.getHeaderField("result");
		System.out.println("GET Response Code :: " + responseCode);
		
		if (responseCode == HttpURLConnection.HTTP_OK) { 
			System.out.println("GET a réussi");
			
		} else {
			System.out.println("GET n'a pas fonctionné");
		}
		
		
	}

	public static Contact getUser(String pseudo, String type) throws IOException {
		Contact user;
		
		URL obj = new URL(url+"?pseudo="+pseudo+"&type="+type);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("GET");
		
		int responseCode = con.getResponseCode();
		String sAdr = con.getHeaderField("address");
		String sPort = con.getHeaderField("port");
		System.out.println("GET Response Code :: " + responseCode);
		
		if (responseCode == HttpURLConnection.HTTP_OK) { 
			System.out.println("GET a réussi");
			
		} else {
			System.out.println("GET n'a pas fonctionné");
		}
	
		String adr2= sAdr.substring(1);
		InetAddress address = InetAddress.getByName(adr2);
		int port = Integer.parseInt(sPort);
		user = new Contact(pseudo, address, port);
		
		return user;
	
	}
	
	public static String actifs(String type) throws IOException {
		String actifs;
		
		URL obj = new URL(url+"?type="+type);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
		con.setRequestMethod("GET");
		
		int responseCode = con.getResponseCode();
		actifs = con.getHeaderField("actifs");
		System.out.println("GET Response Code :: " + responseCode);
		
		if (responseCode == HttpURLConnection.HTTP_OK) { 
			System.out.println("GET a réussi");
			
		} else {
			System.out.println("GET n'a pas fonctionné");
		}
	
				
		return actifs;
	
}


}
