package idc.comment.command;

import java.sql.*;
import java.util.ArrayList;

import javax.naming.*;
import javax.sql.DataSource;

public class CommentDAO {
	
	DataSource ds;
	private int WRITING_PER_PAGE = 10;
	
	public CommentDAO(){
		try {
			Context initContext = (Context)new InitialContext().lookup("java:comp/env/");
			ds = (DataSource)initContext.lookup("jdbc/mysql");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//커넥션 제공
	public Connection getConn(){
		try {
			return ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//게시판 목록 조회 기능 수행
	public ArrayList<CommentDTO> getCommentList(String curPage, String id){		
		ArrayList<CommentDTO> list = new ArrayList<CommentDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
				
		try {
			conn = ds.getConnection();
			String sql = "SELECT m.name, m.rank, c.id, c.num, c.comment_case, c.content, c.write_date, c.write_time, c.read_check, receiver  ";
			sql +=" from memberDB as m Inner join commentDB as c where m.id = c.id and c.id=? ORDER BY c.num DESC ";
			sql += " LIMIT ?, ?";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			pstmt.setInt(2, WRITING_PER_PAGE * (Integer.parseInt(curPage) - 1));
			pstmt.setInt(3, WRITING_PER_PAGE);
			
			rs = pstmt.executeQuery();
			
			while(rs.next()){				
				int num = rs.getInt("num");
				String name = rs.getString("name");
				String rank = rs.getString("rank");
				String comment_case = rs.getString("comment_case");
				String content = rs.getString("content");				
				Date writeDate = rs.getDate("write_date");
				Time writeTime = rs.getTime("write_time");
				int read_check = rs.getInt("read_check");
				String receiver = rs.getString("receiver");
								
				CommentDTO writing = new CommentDTO();
				
				writing.setNum(num);
				writing.setName(name);
				writing.setRank(rank);
				writing.setId(id);
				writing.setComment_case(comment_case);
				
				String temp = "";
				switch (comment_case){
				case "0":
					temp = "일반 업무";
					break;
				case "1":
					temp = "긴급";
					break;
				case "2":
					temp = "지시 사항";
					break;
				case "3":
					temp = "전달 사항";
					break;
				case "4":
					temp = "기타";
					break;
				}
				
				writing.setTemp(temp);
				writing.setContent(content);
				writing.setWrite_date(writeDate);
				writing.setWrite_time(writeTime);
				writing.setRead_check(read_check);
				writing.setReceiver(receiver);
				
				list.add(writing);					
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return list;	
	}
	
	
	//게시판의 페이징 처리를 위한 기능 수행
	public int commentPageCnt(String id){
		int pageCnt = 0;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = ds.getConnection();
			String sql = "SELECT COUNT(*) AS num FROM commentDB where id=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				pageCnt = rs.getInt("num")/WRITING_PER_PAGE + 1;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	
	//게시글 등록 기능 수행
	public void writeComment(String id, String comment_case, String content, String receiver){
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null;
		int num = 1;
		
		try {
			conn = ds.getConnection();
			String sql = "SELECT IFNULL(MAX(num), 0) + 1 AS NUM FROM commentDB";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				num = rs.getInt("num");
			}
						
			sql = "INSERT INTO commentDB (num, id, comment_case, content, receiver, write_date, write_time, read_check) ";
			sql += "values(?, ?, ?, ?, ?, curdate(), curtime(), 0)";
			pstmt1 = conn.prepareStatement(sql);
			
			pstmt1.setInt(1, num);
			pstmt1.setString(2, id);
			pstmt1.setString(3, comment_case);
			pstmt1.setString(4, content);
			pstmt1.setString(5, receiver);						
			
			pstmt1.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(pstmt1 != null) pstmt1.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
			
	//게시글 수정 기능 수행
	public void boardUpdate(String inputNum, String inputSubject, String inputContent, String inputName, String inputPassword){
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = ds.getConnection();
			String sql = "UPDATE BOARD SET subject=?, content=?, name=?, password=? WHERE num=?";
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, inputSubject);
			pstmt.setString(2, inputContent);
			pstmt.setString(3, inputName);
			pstmt.setString(4, inputPassword);
			pstmt.setInt(5, Integer.parseInt(inputNum));
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
				
	
	//게시글 삭제 기능 수행
	public void deleteComment(String inputNum){
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = ds.getConnection();									
			String sql = "DELETE FROM commentDB WHERE num=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(inputNum));			
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}		
	
	//받은 목록 조회 기능 수행
	public ArrayList<CommentDTO> getReceivedList(String curPage, String id){
		
		ArrayList<CommentDTO> list = new ArrayList<CommentDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
				
		try {
			conn = ds.getConnection();
			String sql = "SELECT m.name, m.rank, c.id, c.num, c.comment_case, c.content, c.write_date, c.write_time, c.read_check, receiver  ";
			sql +=" from memberDB as m Inner join commentDB as c where m.id = c.id and receiver = ? ORDER BY c.num DESC ";
			sql += " LIMIT ?, ?";						
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			pstmt.setInt(2, WRITING_PER_PAGE * (Integer.parseInt(curPage) - 1));
			pstmt.setInt(3, WRITING_PER_PAGE);
			
			rs = pstmt.executeQuery();
			
			while(rs.next()){				
				int num = rs.getInt("num");
				String name = rs.getString("name");
				String rank = rs.getString("rank");
				String comment_case = rs.getString("comment_case");
				String content = rs.getString("content");				
				Date writeDate = rs.getDate("write_date");
				Time writeTime = rs.getTime("write_time");
				int read_check = rs.getInt("read_check");
				String receiver = rs.getString("receiver");
								
				CommentDTO writing = new CommentDTO();
				
				writing.setNum(num);
				writing.setName(name);
				writing.setRank(rank);
				writing.setId(id);
				writing.setComment_case(comment_case);
				String temp = "";
				switch (comment_case){
				case "0":
					temp = "일반 업무";
					break;
				case "1":
					temp = "긴급";
					break;
				case "2":
					temp = "지시 사항";
					break;
				case "3":
					temp = "전달 사항";
					break;
				case "4":
					temp = "기타";
					break;
				}
				
				writing.setTemp(temp);				
				writing.setContent(content);
				writing.setWrite_date(writeDate);
				writing.setWrite_time(writeTime);
				writing.setRead_check(read_check);
				writing.setReceiver(receiver);
				
				list.add(writing);					
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return list;	
	}
	
	
	//게시판의 페이징 처리를 위한 기능 수행
	public int receivedPageCnt(String id){
		int pageCnt = 0;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = ds.getConnection();
			String sql = "SELECT COUNT(*) AS num FROM commentDB WHERE receiver=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				pageCnt = rs.getInt("num")/WRITING_PER_PAGE + 1;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	
}