package idc.board.command;

import idc.model.IdcMemberDBDTO;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NoteBookCmd implements BoardCmd{
	
	BoardDAO dao = new BoardDAO();
	private int WRITING_PER_PAGE = 10;
	
	public void execute(HttpServletRequest request, HttpServletResponse response) {
								
		ArrayList<NoteBookDTO>list;
		
		//세션에서 이름 추출
		IdcMemberDBDTO d1to = (IdcMemberDBDTO)request.getSession().getAttribute("member");
		String name = d1to.getName();		
		request.setAttribute("name", name);
		
		int pageCnt = 0;
		String curPage = request.getParameter("curPage");
		
		if(curPage == null) curPage = "1";
		
		list = getNoteBookList(curPage, name);		
		request.setAttribute("list", list);				
		pageCnt = noteBookPageCnt(name);
		request.setAttribute("pageCnt", pageCnt);
				
			
	}
		
	
	//게시판 목록 조회 기능 수행
	private ArrayList<NoteBookDTO> getNoteBookList(String curPage, String name){
		
		ArrayList<NoteBookDTO> list = new ArrayList<NoteBookDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT num, name, content, write_date, write_time";
			sql +=" FROM noteDB WHERE name=? ORDER BY num desc";
			sql += " LIMIT ?, ?";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			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 content = rs.getString("content");
				Date writeDate = rs.getDate("write_date");
				Time writeTime = rs.getTime("write_time");				
				
				NoteBookDTO writing = new NoteBookDTO();
				
				writing.setNum(num);
				writing.setName(name);								
				writing.setContent(content);
				writing.setWrite_date(writeDate);
				writing.setWrite_time(writeTime);				
				
				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 noteBookPageCnt(String name){
		int pageCnt = 0;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT COUNT(*) AS num FROM noteDB WHERE name=? ";			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				int num = rs.getInt("num");
				//pageCnt = rs.getInt("num")/WRITING_PER_PAGE + 1;
				
				if(num%WRITING_PER_PAGE == 0){
					pageCnt = num/WRITING_PER_PAGE;
				}else{
					pageCnt = 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;
	}

}
