package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CommentMod {
	
	public List<CommentInfo> getComment(String idpd, int limit, int offset) throws SQLException {
		PreparedStatement getcm = null;
     List<CommentInfo> result = new ArrayList<CommentInfo>();
     Connection con = new ConnectionPool().getConnection();
     
      getcm = con.prepareStatement("select * from comment where idpd = ? limit ? offset ?");
      getcm.setString(1, idpd);
      getcm.setInt(2, limit);
      getcm.setInt(3, offset);
      ResultSet rs = getcm.executeQuery();
      
      while (rs.next()) {
      	String id = rs.getString(1);
        String idpdx = rs.getString(2);
          String date = rs.getString(3);
          String user = rs.getString(4);
          String content = rs.getString(5);

          CommentInfo cm = new CommentInfo(id,idpdx,date,user,content);
          result.add(cm);
      }

      con.close();
      return result;
  }
	
    public int recordsComment(String idpd) throws SQLException {
    	PreparedStatement rcgetcm = null;
    	int record = 0;
		Connection con = new ConnectionPool().getConnection();
		
			rcgetcm  = con.prepareStatement("select count(id) from comment where idpd = ?");
			rcgetcm.setString(1, idpd);
	        ResultSet rs2 = rcgetcm.executeQuery();
	        while (rs2.next()) { 	        	
	        	record = rs2.getInt(1);
	        }
			con.close();
		return record;
	}
	
	
	 public boolean CreateComment(String idpd, String date,String user, String content) {
		 PreparedStatement createcm = null;
			boolean result = false;
			Connection con = new ConnectionPool().getConnection();
			try {
				
				createcm = con.prepareStatement("insert into comment(idpd, date, user, content) values(?, ?, ?, ?)");
				createcm.setString(1, idpd);
				createcm.setString(2, date);
				createcm.setString(3, user);
				createcm.setString(4, content);
				int number = createcm.executeUpdate();
				if (number > 0) {
					result = true;
				}

				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			return result;
		}
	 
	 	public boolean DeleteCM(String id, String idpd) {
	 		PreparedStatement DelCM = null;
			boolean result = false;

			Connection con = new ConnectionPool().getConnection();
			try {
				DelCM = con.prepareStatement("DELETE FROM comment WHERE idpd = ? and id = ?");
				DelCM.setString(1, idpd);
				DelCM.setString(2, id);
				int number = DelCM.executeUpdate();
				if (number > 0) {
					result = true;
				}

				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			return result;
		}
	 	
	 	public boolean DeleteAllCM(String idpd) {
	 		PreparedStatement DelAllCM = null;
			boolean result = false;

			Connection con = new ConnectionPool().getConnection();
			try {
				DelAllCM = con.prepareStatement("DELETE FROM comment where idpd = ?");
				DelAllCM.setString(1, idpd);
				int number = DelAllCM.executeUpdate();
				if (number > 0) {
					result = true;
				}

				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			return result;
		}
	
	
	public static void main(String[] args) throws SQLException {
		System.out.println(new CommentMod().DeleteAllCM("37"));
	}
}
