package idc.client.command;

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

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

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

import utility.StringToDate;

public class IdcClientDBWriteCmd implements IdcCmd
{
	private IdcDAO dao;
	public IdcClientDBWriteCmd() 
	{
		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");
		}
		
		String client_code=null; 
		String client_name=request.getParameter("client_name");
		String client_reseller=request.getParameter("client_reseller");
		String client_phone1=request.getParameter("client_phone1");
		String client_phone2=request.getParameter("client_phone2");
		String client_email1=request.getParameter("client_email1");
		String client_email2=request.getParameter("client_email2");
		String client_note=request.getParameter("client_note");
		String str_date = request.getParameter("client_receive_date");
		
		StringToDate st = new StringToDate();
		Date client_receive_date;		
		if(str_date == "" || str_date == null){
			client_receive_date = null;
		}else{
			client_receive_date = st.stringToDate(str_date);			
		}			
		
		
		idcClientDBWrite(client_code, client_name, client_reseller, client_phone1, 
				client_phone2, client_email1, client_email2, client_note, client_receive_date);
	}
	//게시글 등록 기능 수행
	
	public void idcClientDBWrite(String code, String name, String reseller, String phone1, String phone2,
												String email1, String email2, String note, Date client_receive_date)
	{
		Connection conn = null;
		PreparedStatement pstmt = null,pstmt2=null,pstmt3=null,pstmt4=null;
		ResultSet rs = null,rs2=null;
		
		try
		{
			conn=dao.getConn();
			
			String sql="SELECT client_code FROM clientdb WHERE client_num>0";
			pstmt4 = conn.prepareStatement(sql);
			rs2 = pstmt4.executeQuery();
			ArrayList<String> cList = new ArrayList<String>();
			
			while(rs2.next())
			{
				cList.add(rs2.getString("client_code"));
			}
			code = getCode(cList);
			
			//데이터 입력하기
			sql = "INSERT INTO clientdb( client_code, client_name,client_reseller,client_phone1,client_phone2, client_email1,";
			sql+="client_email2,client_receive_date,client_renew_date,client_note) ";
			sql+="values(?,?,?,?,?,?,?,?,?,?)";
			pstmt2 = conn.prepareStatement(sql);
			
			pstmt2.setString(1, code);
			pstmt2.setString(2, name);
			pstmt2.setString(3, reseller);
			pstmt2.setString(4, phone1);
			pstmt2.setString(5, phone2);
			pstmt2.setString(6, email1);
			pstmt2.setString(7, email2);
			pstmt2.setDate(8, client_receive_date);
			pstmt2.setDate(9, client_receive_date);
			pstmt2.setString(10, note);
			pstmt2.executeUpdate();
			
			//havelist에 추가하기
			if(!reseller.equals("") && !reseller.equals("0"))
			{
				sql="INSERT INTO havelist(client_code, client_name) values(?,?)";
				pstmt3 = conn.prepareStatement(sql);
				pstmt3.setString(1, code);
				pstmt3.setString(2, name);
				pstmt3.executeUpdate();
			}
			
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
	 		{
	 			if(rs!=null)rs.close();
	 			if(pstmt!=null)pstmt.close();
	 			if(pstmt2!=null)pstmt2.close();
	 			if(pstmt3!=null)pstmt3.close();
	 			if(conn!=null)conn.close();
	 		}
	 		catch(SQLException e)
	 		{
	 			e.printStackTrace();
	 		}
		}
	}
	
	private String getCode(ArrayList<String> list)
	{
		Random r = new Random();
		int a = r.nextInt(999)+1;
		String result ="";
		boolean check=false;
		int temp=0;
		do
		{
			for(int i=0;i<list.size();i++)
			{
				temp = Integer.parseInt(list.get(i).substring(1));
				if(temp == a)
				{
					a = r.nextInt(999)+1;
					check=true;
					break;
				}
				check=false;
			}
		}while(check);
		
		return "C"+result.format("%03d", a);
	}
}
