package controller;

import java.sql.Connection;

import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;

import javax.sql.rowset.JdbcRowSet;

public class Historique {
	
	public void saveMessage(model.User ctr1, model.RemoteUser ctr2, String input, String date) {
		try {
			Class.forName("java.sql.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306","tp_servlet_003","povu3Ma2");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Statement stat = null;
		try {
			stat = con.createStatement();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int nb_changed_rows = 0;
		try {
			nb_changed_rows = stat.executeUpdate("USE tp_servlet_003");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
		nb_changed_rows = stat.executeUpdate("CREATE TABLE chat ( user_IPcode1 INTEGER, user_IPcode2 INTEGER ,Message VARCHAR(450) ,temps VARCHAR(450) )");
		}catch (Exception e) {
			e.printStackTrace();

		}
		System.out.println("coucou");

		try {
			nb_changed_rows = stat.executeUpdate("INSERT INTO chat VALUES ('"+ctr1.getIPcode()+"','"+ctr2.getIPcode()+"','"+input+"','"+date+"')");
		} catch (SQLException e) {
			System.out.println("insertion pas établie");
			e.printStackTrace();
		}
//		try {
//			nb_changed_rows = stat.executeUpdate("DELETE FROM chat WHERE data(VARCHAR(255))="valeur"");
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		try {
			stat.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public String retrieveMessage(model.LocalUser user1, model.RemoteUser user2) throws SQLException {
		try {
			Class.forName("java.sql.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Connection con=DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306","tp_servlet_003","povu3Ma2");
		Statement stat=con.createStatement();
		Statement stat2=con.createStatement();
		int	nb_changed_rows = stat.executeUpdate("USE tp_servlet_003");

		try {
		nb_changed_rows = stat.executeUpdate("CREATE TABLE chat ( user_IPcode1 INTEGER, user_IPcode2 INTEGER ,Message VARCHAR(450) ,temps VARCHAR(450) )");
		}catch (Exception e) {
			e.printStackTrace();

		}
		ResultSet RSSent=(stat.executeQuery("SELECT user_IPcode1, user_IPcode2, Message, temps FROM chat WHERE user_IPcode1="+user1.getIPcode()));
		//System.out.println(RSSent);
		boolean encore1 = RSSent.next();
		ResultSet RSReceived=(stat2.executeQuery("SELECT user_IPcode1, user_IPcode2, Message, temps FROM chat WHERE user_IPcode1="+user2.getIPcode()));
	    boolean encore2 = RSReceived.next();
	    String message=null;
	    String[] string1=null;
	    String[] string2=null;
	    Calendar date1=Calendar.getInstance();
	    Calendar date2=Calendar.getInstance();
	    while(encore1 || encore2) {
			//RSSent=(stat.executeQuery("SELECT user_IPcode1, user_IPcode2, Message, temps FROM chat WHERE user_IPcode1="+user1.getIPcode()));
			//boolean temp=RSSent.next();
			
	    	while (RSSent.getInt(2)!=user2.getIPcode()) {
	    		encore1=RSSent.next();
	    	}
    		string1=RSSent.getString(4).split(" ");
    		System.out.println(RSSent.getString(4));
    		date1.set(Integer.valueOf(string1[0]), Integer.valueOf(string1[1]), Integer.valueOf(string1[2]),Integer.valueOf(string1[3]), Integer.valueOf(string1[4]), Integer.valueOf(string1[5]));

    		//RSReceived=(stat.executeQuery("SELECT user_IPcode1, user_IPcode2, Message, temps FROM chat WHERE user_IPcode1="+user2.getIPcode()));
    	    //boolean tempi = RSReceived.next();

	    	while (RSReceived.getInt(1)!=user1.getIPcode()) {
		    	encore2=RSReceived.next();
	    	}
    		string2=RSReceived.getString(4).split(" ");
    		date2.set(Integer.valueOf(string2[0]), Integer.valueOf(string2[1]), Integer.valueOf(string2[2]),Integer.valueOf(string2[3]), Integer.valueOf(string2[4]), Integer.valueOf(string2[5]));

	    	if (date1.compareTo(date2)>=0) {
	    		message=message+"\n"+RSReceived.getString(3);
	    		message=message+"\n"+RSSent.getString(3);
	    	}else {
	    		message=message+"\n"+RSSent.getString(3);
	    		message=message+"\n"+RSReceived.getString(3);
	    	}
    		encore1=RSSent.next();
	    	encore2=RSReceived.next();

	    }
		RSSent.close();
		RSReceived.close();
		stat.close();
		stat2.close();
		con.close();
		
		return message;
	}
	

//	//MAIN
//	public Historique() {
//		
//		System.out.println("coucoudb");
//		
//		
//	}
	
	
}
