package idc.main.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcMemberDBDTO;

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

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

public class IdcLoginCheckCmd implements IdcCmd 
{
	IdcDAO dao;
	
	public IdcLoginCheckCmd() 
	{
		dao = new IdcDAO();
	}
	
	//로그인 체크
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		IdcMemberDBDTO dto = new IdcMemberDBDTO();
		setDTO(dto, request);
		sendCheckTest(idcLoginCheckCmd(dto), dto, request);
	}
	
	//로그인 폼에서 받은 id, pass값 dto에 데이터 넣기
	private void setDTO(IdcMemberDBDTO dto, HttpServletRequest request)
	{
		dto.setId(request.getParameter("member_id"));
		dto.setPass(request.getParameter("password"));
	}
	//db로부터 데이터 받아온것으로 로그인 성공인지 아닌지 판단.
	private void sendCheckTest(boolean check, IdcMemberDBDTO dto, HttpServletRequest request )
	{
		if(check)
		{
			dto.passDel();	
			request.getSession().setAttribute("member", dto);
		}
		request.setAttribute("memberCheck", check);
	}
	//db에 쿼리 날려 존재자인지 아닌지 판단
	private boolean idcLoginCheckCmd(IdcMemberDBDTO dto)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		boolean check=false;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM memberdb WHERE id=? AND pass=MD5(?)";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, dto.getId());
			pstmt.setString(2, dto.getPass());
			rs = pstmt.executeQuery();
			//System.out.println(pstmt);
			
			if(rs.next())
			{
				dto.setId(rs.getString("id"));
				dto.setPass(rs.getString("pass"));
				dto.setNum(rs.getString("num"));
				dto.setName(rs.getString("name"));
				dto.setRank(rs.getString("rank"));
				dto.setPhone(rs.getString("phone"));
				dto.setLocation(rs.getString("location"));
				dto.setPower(rs.getInt("power"));
				dto.setJoin_date(rs.getDate("join_date"));
				dto.setQuick1(rs.getString("quick1"));
				dto.setQuick2(rs.getString("quick2"));
				dto.setQuick3(rs.getString("quick3"));
				dto.setQuick4(rs.getString("quick4"));
				dto.setQuick5(rs.getString("quick5"));
				dto.setQuick6(rs.getString("quick6"));
				dto.setQuick7(rs.getString("quick7"));
				dto.setQuick8(rs.getString("quick8"));
				dto.setQuick9(rs.getString("quick9"));
				dto.setQuick10(rs.getString("quick10"));	
				dto.setQuick11(rs.getString("quick11"));	
				dto.setQuick12(rs.getString("quick12"));	
				dto.setQuick13(rs.getString("quick13"));	
				dto.setQuick14(rs.getString("quick14"));	
				dto.setQuick15(rs.getString("quick15"));
				dto.setQuick16(rs.getString("quick16"));
				dto.setQuick17(rs.getString("quick17"));
				dto.setQuick18(rs.getString("quick18"));
				dto.setQuick19(rs.getString("quick19"));
				dto.setQuick20(rs.getString("quick20"));
				dto.setQuick21(rs.getString("quick21"));
				dto.setQuick22(rs.getString("quick22"));
				dto.setQuick23(rs.getString("quick23"));
				dto.setQuick24(rs.getString("quick24"));
				dto.setQuick25(rs.getString("quick25"));
				dto.setSchedule(rs.getString("schedule"));
				dto.setOngoing(rs.getString("ongoing"));
				dto.setNote(rs.getString("note"));
				
				dto.setPower1(rs.getString("power1"));
				dto.setPower2(rs.getString("power2"));
				dto.setPower3(rs.getString("power3"));
				dto.setPower4(rs.getString("power4"));
				dto.setPower5(rs.getString("power5"));
				dto.setPower6(rs.getString("power6"));
				dto.setPower7(rs.getString("power7"));
				dto.setPower8(rs.getString("power8"));
				dto.setPower9(rs.getString("power9"));
				dto.setPower10(rs.getString("power10"));
				dto.setPart(rs.getString("part"));
				
				check=true;
			}
		}
		catch (Exception e) 
		{
			System.out.println("idcLoginCheckCmd 에서의 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("idcLoginCheckCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		return check;
	}
}
