/**
 * 
 */
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 org.mynonglam.connectdb.ConnectionDTB;
import org.mynonglam.entitys.Account;
import org.mynonglam.entitys.CommentStatus;
import org.mynonglam.entitys.Status;
import org.mynonglam.entitys.StatusLike;

/**
 * @author Dao
 *
 */
public class StatusDAO {
	

	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;
	
	private AccountDAO accountDAO;
	/**
	 * 
	 */
	public StatusDAO() {
		connection = ConnectionDTB.getConnection();
		accountDAO = new AccountDAO();
	}
	
	public boolean insertStatus(Status status) {
		boolean result = false;
		
		String sql = "INSERT INTO status(idstatus, email, idshare, content, datecreate) VALUES (?,?,'1',?,?)";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, status.getIdstatus());
			statement.setString(2, status.getAccount().getEmail());
			statement.setString(3, status.getContent());
			statement.setString(4, status.getDatecreate());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getIDStatusLast() {
		String result = "";
		
		String sql = "SELECT idstatus FROM status ORDER BY idstatus DESC LIMIT 0,1";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result = resultSet.getString("idstatus");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public ArrayList<CommentStatus> getCommentStatus(String idstatus) {
		ArrayList<CommentStatus> result = new ArrayList<CommentStatus>();
		
		String sql = "SELECT idcomstatus, email, content, commentdate FROM commentstatus WHERE idstatus=?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idstatus);
			
			ResultSet resultSet = statement.executeQuery();
			while(resultSet.next()) {
				CommentStatus temp = new CommentStatus();
				temp.setIdcomstatus(resultSet.getString("idcomstatus"));
				temp.setContent(resultSet.getString("content"));
				temp.setCommentdate(resultSet.getString("commentdate"));
				temp.setAccount(accountDAO.getAccountInfoByComment(resultSet.getString("email")));
				result.add(temp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public ArrayList<StatusLike> getStatusLike(String idstatus) {
		ArrayList<StatusLike> result = new ArrayList<StatusLike>();
		
		String sql = "SELECT idstatus, emaillikestatus, datelikestatus FROM statuslike WHERE idstatus=?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idstatus);
			
			ResultSet resultSet = statement.executeQuery();
			while(resultSet.next()) {
				StatusLike temp = new StatusLike();
				temp.setAccount(accountDAO.getAccountInfoByComment(resultSet.getString("emaillikestatus")));
				result.add(temp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean insertStatusLike(String idstatus, String email, String datelike) {
		boolean result = false;
		
		String sql = "INSERT INTO statuslike(idstatus, emaillikestatus, datelikestatus) VALUES (?,?,?)";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idstatus);
			statement.setString(2, email);
			statement.setString(3, datelike);
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean deleteStatusLike(String idstatus, String email) {
		boolean result = false;
		
		String sql = "DELETE FROM statuslike WHERE idstatus=? AND emaillikestatus=?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idstatus);
			statement.setString(2, email);
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public ArrayList<String> getEmailLikeStatus(String idstatus) {
		ArrayList<String> result = new ArrayList<String>();
		String sql = "SELECT emaillikestatus FROM statuslike WHERE idstatus=?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idstatus);
			
			ResultSet resultSet = statement.executeQuery();
			while(resultSet.next()) {
				result.add(resultSet.getString("emaillikestatus"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public ArrayList<String> getEmailCommentStatus(String idstatus) {
		ArrayList<String> result = new ArrayList<String>();
		String sql = "SELECT email FROM commentstatus WHERE idstatus=?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idstatus);
			
			ResultSet resultSet = statement.executeQuery();
			while(resultSet.next()) {
				result.add(resultSet.getString("email"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public Account getNameAccountNewStatus(String idStatus) {
		Account result = new Account();
		
		String sql = "SELECT a.email, a.fullname FROM status as s inner join account as a on a.email=s.email WHERE idstatus=?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idStatus);
			
			ResultSet resultSet = statement.executeQuery();
			if (resultSet.next()) {
				result.setFullname(resultSet.getString("fullname"));
				result.setEmail(resultSet.getString("email"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}

}
