package org.mynonglam.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.sql.DataSource;
import org.mynonglam.connectdb.DbConnection;
import org.mynonglam.entitys.Message;

public class MessageMod {
	
	private DataSource dataSource;
	 
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public ArrayList<Message> getMessages(String sendr, String receivr, int offset, int limit) throws SQLException {
		
		ArrayList<Message> list = new ArrayList<Message>();
		Connection con = dataSource.getConnection();
   
		PreparedStatement ps = con.prepareStatement("SELECT * FROM message WHERE (sender = ? AND receiver = ?) OR (sender = ? AND receiver = ?) LIMIT ? OFFSET ?");
		ps.setString(1, sendr);
		ps.setString(2, receivr);
		ps.setString(3, receivr);
		ps.setString(4, sendr);
		ps.setInt(5, limit);
		ps.setInt(6, offset);
		ResultSet rs = ps.executeQuery();
		Message cm = null;
		
		while (rs.next()) {
			String id = rs.getString(1);
			String sender = rs.getString(2);
			String receiver = rs.getString(3);
			String content = rs.getString(4);
			String date = rs.getString(5);
			String isActive = rs.getString(6);
			cm = new Message(id, sender, receiver, content, date, isActive);
			list.add(cm);
		}
		
		ps.close();
		con.close();
		return list;
	}
	
	public ArrayList<Message> getOffMessages(String me) throws SQLException {
		
		ArrayList<Message> list = new ArrayList<Message>();
		Connection con = dataSource.getConnection();
   
		PreparedStatement ps = con.prepareStatement("SELECT ms.sender, acc.fullname, acc.avatar, ms.content FROM message as ms, account as acc WHERE ms.receiver = ? AND acc.email = ms.sender AND ms.isactive = 0");
		ps.setString(1, me);
		ResultSet rs = ps.executeQuery();
		Message cm = null;
		
		while (rs.next()) {
			String id = rs.getString(1);
			String sender = rs.getString(2);
			String receiver = rs.getString(3);
			String content = rs.getString(4);
			String date = "";
			String isActive = "";
			cm = new Message(id, sender, receiver, content, date, isActive);
			list.add(cm);
		}
		
		ps.close();
		con.close();
		return list;
	}
	
	 public void createMessage(Message ms) {
			Connection con = new DbConnection().getConnection();
			try {
			
				PreparedStatement pre = con.prepareStatement("INSERT INTO message(sender, receiver, content, date, isactive) values(?, ?, ?, ?, ?)");
				pre.setString(1, ms.getSender());
				pre.setString(2, ms.getReceiver());
				pre.setString(3, ms.getContent());
				pre.setString(4, ms.getDate());
				pre.setString(5, ms.getIsActive());
				pre.executeUpdate();
				
				pre.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	
	    public int getRecordNumber(String sender, String receiver) throws SQLException {
	    	int record = 0;
			Connection con = new DbConnection().getConnection();	
			PreparedStatement ps  = con.prepareStatement("SELECT COUNT(id) FROM message WHERE (sender = ? AND receiver = ?) OR (sender = ? AND receiver = ?)");
			ps.setString(1, sender);
			ps.setString(2, receiver);
			ps.setString(3, receiver);
			ps.setString(4, sender);
			ResultSet rs = ps.executeQuery();
			
			while (rs.next()) { 	        	
				record = rs.getInt(1);
			}
			ps.close();
			con.close();
			return record;
		}
	    
		public boolean setViewedMessage(String from, String to) throws SQLException {
			boolean result = false;
			Connection con = dataSource.getConnection();
			try {
				
				PreparedStatement ps = con.prepareStatement("UPDATE message SET isactive = 1 WHERE sender = ? AND receiver = ?");
				ps.setString(1, from);
				ps.setString(2, to);
				int number = ps.executeUpdate();
				if (number > 0) {
					result = true;
				}
				ps.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return result;
		}
	 
	public static void main(String[] args) throws SQLException {
//		System.out.println(new Gson().toJson(new MessageMod().getMessages("luanxt", "hahaha", 11, 7)));
//		System.out.println(new Gson().toJson(new MessageMod().getRecordNumber("luanxt", "hahaha")));
//		SimpleDateFormat da = new SimpleDateFormat("dd/MM/yy - hh:mm:ss");
//		String d = da.format(new Date());
////		System.out.println(d);
//		ArrayList<Message> list = new MessageMod().getOffMessages("luanxt@zing.vn");
//		HashMap<String, Message> myMap = new HashMap<String, Message>();
//		int amount = 1;
//		TreeSet<String> mySet = new TreeSet<String>();
//		for (Message message : list) {
//			mySet.add(message.getSender());
//		}
//		for (String sender : mySet) {
//			for (Message message : list) {
//				if (sender.equals(message.getSender())){
//					myMap.put(sender, new Message(sender, message.getSender(), message.getReceiver(), message.getContent(), "" + amount, "" + mySet.size()));
//					amount++;
//				} else {
//					amount = 1;
//				}
//			}
//		}
//		System.out.println("SIze: " + mySet.size());
//		System.out.println(new Gson().toJson(myMap));
		
		new MessageMod().setViewedMessage("dlduynam@gmail.com", "luanxt@zing.vn");
	}
}
