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 IdcJoinCmd implements IdcCmd
{
	IdcDAO dao;
	public IdcJoinCmd() 
	{
		dao = new IdcDAO();
	}
	
	//회원가입 페이지
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request))
		{
			request.setAttribute("reError", dao.reError);
			return;
		}
		else
		{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
		
		IdcMemberDBDTO dto = setDTO(request);
		idcJoinCmd(dto);
		
		//멤버가입시 인수 인계 DB추가작업
		insertIdcHandover(dto.getName());
	}
	//dto데이터 삽입
	private IdcMemberDBDTO setDTO(HttpServletRequest request)
	{
		String[] power = request.getParameterValues("power");
		String[] p = new String[10];
		IdcMemberDBDTO dto = new IdcMemberDBDTO();
		
		dto.setId(getPara(request, "member_id"));
		dto.setLocation(getPara(request, "location"));
		dto.setName(getPara(request, "name"));
		dto.setNote(getPara(request, "note"));
		dto.setNum(getPara(request, "num"));
		dto.setPass(getPara(request, "pass"));
		dto.setRe_pass(getPara(request, "re_pass"));
		dto.setPhone(getPara(request, "phone"));
		dto.setRank(getPara(request, "rank"));
		dto.setPart((getPara(request, "part")));
		
		int temp=0;
		for(int i=1;i<=10;i++)
		{
			if(power!=null && temp < power.length&& i==Integer.parseInt(power[temp]))
			{
				p[i-1]="o";
				temp++;
			}
			else
				p[i-1]="x";
		}
		dto.setPower1(p[0]);
		dto.setPower2(p[1]);
		dto.setPower3(p[2]);
		dto.setPower4(p[3]);
		dto.setPower5(p[4]);
		dto.setPower6(p[5]);
		dto.setPower7(p[6]);
		dto.setPower8(p[7]);
		dto.setPower9(p[8]);
		dto.setPower10(p[9]);
		
		return dto;
	}
	//requst.getpara쓰기 귀찮아서 만듦
	private String getPara(HttpServletRequest request, String value)
	{
		return request.getParameter(value);
	}
	//데이터 입력
	private void idcJoinCmd(IdcMemberDBDTO dto)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			int index=1;
			conn=dao.getConn();
			String sql="INSERT INTO memberdb (id,pass,name,num,rank,phone,location,power,join_date,note, ";
			sql+="power1, power2, power3, power4, power5, power6, power7, power8, power9, power10, part)";
			sql+="value(?,MD5(?),?,?,?,?,?,0,curdate(),?, ";
			sql+="?, ?, ?, ?, ?, ?, ?, ?, ?, ?,  ?)";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(index++, dto.getId());
			pstmt.setString(index++, dto.getPass());
			pstmt.setString(index++, dto.getName());
			pstmt.setString(index++, dto.getNum());
			pstmt.setString(index++, dto.getRank());
			pstmt.setString(index++, dto.getPhone());
			pstmt.setString(index++, dto.getLocation());
			pstmt.setString(index++, dto.getNote());
			
			pstmt.setString(index++, dto.getPower1());
			pstmt.setString(index++, dto.getPower2());
			pstmt.setString(index++, dto.getPower3());
			pstmt.setString(index++, dto.getPower4());
			pstmt.setString(index++, dto.getPower5());
			pstmt.setString(index++, dto.getPower6());
			pstmt.setString(index++, dto.getPower7());
			pstmt.setString(index++, dto.getPower8());
			pstmt.setString(index++, dto.getPower9());
			pstmt.setString(index++, dto.getPower10());
			
			pstmt.setString(index++, dto.getPart());
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("idcJoinCmd 에서의 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("idcJoinCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	public void insertIdcHandover(String name){
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{			
			conn=dao.getConn();
			String sql="INSERT INTO handoverDB (name) ";
			sql +=" value(?) ";			
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, name);
			pstmt.executeUpdate();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}catch (SQLException e){			
				e.printStackTrace();
			}
		}
	}
}