package idc.main.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcShareBoardDBDTO;

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

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

public class IdcShareBoardListForm implements IdcCmd
{
	IdcDAO dao;
	private int WRITE_PER_PAGE = 15;
	
	public IdcShareBoardListForm()
	{
		dao = new IdcDAO();
	}
	
	//공용자료실 페이지.
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		int pageCnt = getPageCnt();
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt);

		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		
		ArrayList<IdcShareBoardDBDTO> list = new ArrayList<IdcShareBoardDBDTO>();
		idcShareBoardListForm(list,curPage);
		request.setAttribute("ShareBoardList", list);
	}
	
	private void idcShareBoardListForm(ArrayList<IdcShareBoardDBDTO> list, int curPage)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null, rs2=null;
		int numbering=0;
		int numbering_end=(curPage-1)*WRITE_PER_PAGE;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as a FROM shareboarddb";
			pstmt2=conn.prepareStatement(sql);
			rs2=pstmt2.executeQuery();
			if(rs2.next())
				numbering=rs2.getInt("a")-numbering_end;
			sql="SELECT num,write_date,title,id,code FROM shareboarddb ORDER BY num DESC LIMIT ?,?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, numbering_end);
			pstmt.setInt(2,WRITE_PER_PAGE);
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcShareBoardDBDTO dto = new IdcShareBoardDBDTO();
				
				dto.setId(rs.getString("id"));
				dto.setNum(numbering--);
				dto.setTitle(rs.getString("title"));
				dto.setWrite_date(rs.getDate("write_date"));
				dto.setCode(rs.getInt("code"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("idcShareBoardListForm 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("idcShareBoardListForm 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	private int getCurPage(String strPage)
	{
		int curPage = 1;

		if (strPage != null && strPage != "")
			curPage = Integer.parseInt(strPage);

		return curPage;
	}

	private int getPageCnt()
	{
		int pageCnt = 1;

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try
		{
			conn = dao.getConn();
			String sql = "SELECT COUNT(*) as a FROM memberdb";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs.next())
				pageCnt = rs.getInt("a") / WRITE_PER_PAGE + 1;
		} 
		catch (Exception e)
		{
			System.out.println("IdcMemberListFormCmd - pageCnt 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (conn != null)
					conn.close();
				if (pstmt != null)
					pstmt.close();
				if (rs != null)
					rs.close();
			} catch (SQLException e)
			{
				System.out.println("IdcMemberListFormCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}

		return pageCnt;
	}

	private int getFirstPage(int pageCnt)
	{
		int firstPage = 1;
		if (pageCnt - 10 > 1)
			firstPage = pageCnt - 10;
		return firstPage;
	}
}
