package idc.line.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcIPDBDTO;
import idc.model.IdcLineDBDTO;

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

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

public class IdcLineWriteCmd implements IdcCmd 
{
	IdcDAO dao;
	public IdcLineWriteCmd() 
	{
		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 haveip;
		IdcLineDBDTO dto = new IdcLineDBDTO();
		ArrayList<IdcIPDBDTO> list;
		
		list = (ArrayList<IdcIPDBDTO>)request.getSession().getAttribute("IdcIPDBList");
		request.getSession().removeAttribute("IdcIPDBList");
	
		String s = request.getParameter("IdcIPDBList");
		dto.setLine_code(request.getParameter("line_code"));
		dto.setLine_case(request.getParameter("line_case"));
		dto.setLine_IP(request.getParameter("line_ip"));
		dto.setLine_able(request.getParameter("line_able"));
		dto.setLine_block(request.getParameter("line_block"));
		dto.setLine_user_allocatingday(request.getParameter("line_user_allocatingday"));
		dto.setLine_client_name(request.getParameter("line_client_name"));
		dto.setLine_mainip(request.getParameter("line_mainip"));
		dto.setLine_subip1(request.getParameter("line_subip1"));
		dto.setLine_subip2(request.getParameter("line_subip2"));
		dto.setLine_subip3(request.getParameter("line_subip3"));
		dto.setLine_id(request.getParameter("line_id"));
		dto.setLine_pass(request.getParameter("line_pass"));
		dto.setLine_phone(request.getParameter("line_phone"));
		dto.setLine_name(request.getParameter("line_name") );
		dto.setLine_gateway(request.getParameter("line_gateway"));
		dto.setLine_subnet(request.getParameter("line_subnet"));
		//haveip = ip 대역 
		String[] dae = request.getParameterValues("ip_dae");
		haveip = dae[0]+"."+dae[1]+"."+dae[2]+"."+dae[3]+" / "+dae[4];
		dto.setLine_haveip(haveip);
		
		idcLineWrite(dto, list);
	}

	private void idcLineWrite(IdcLineDBDTO dto, ArrayList<IdcIPDBDTO> list)
	{
		
		Connection conn =null;
		PreparedStatement pstmt=null,pstmt2=null,pstmt3=null, pstmt4=null;
		ResultSet rs = null, rs4=null;
		
		try 
		{
			conn = dao.getConn();
			String sql ="INSERT INTO lineDB (line_code, line_case, line_ip, line_able, line_block, line_client_name, ";
			sql+="line_mainip, line_subip1, line_subip2, line_subip3, line_user_allocatingday, line_user_endday, ";
			sql+="line_before_user, line_before_allocatingday, line_before_endday, ";
			sql+="line_before_mainip, line_before_subip1, line_before_subip2, line_before_subip3, ";
			sql+="line_id, line_pass, line_name, line_phone, line_subnet, line_gateway, line_haveip) ";
			sql+="values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			
			pstmt.setString(1, dto.getLine_code());
			pstmt.setString(2, dto.getLine_case());
			pstmt.setString(3, dto.getLine_IP());
			pstmt.setString(4,  dto.getLine_able());
			pstmt.setString(5, dto.getLine_block());
			pstmt.setString(6,  dto.getLine_client_name());
			pstmt.setString(7, dto.getLine_mainip());
			pstmt.setString(8, dto.getLine_subip1());
			pstmt.setString(9, dto.getLine_subip2());
			pstmt.setString(10, dto.getLine_subip3());
			pstmt.setDate(11, dto.getLine_user_allocatingday());
			pstmt.setDate(12, dto.getLine_user_endday());
			
			pstmt.setString(13, dto.getLine_before_user());
			pstmt.setDate(14, dto.getLine_before_allocatingday());
			pstmt.setDate(15, dto.getLine_before_endday());
			pstmt.setString(16, dto.getLine_before_mainip());
			pstmt.setString(17,  dto.getLine_before_subip1());
			pstmt.setString(18,  dto.getLine_before_subip2());
			pstmt.setString(19,  dto.getLine_before_subip3());
			
			pstmt.setString(20, dto.getLine_id());
			pstmt.setString(21, dto.getLine_pass());
			pstmt.setString(22, dto.getLine_name());
			pstmt.setString(23, dto.getLine_phone());
			pstmt.setString(24, dto.getLine_subnet());
			pstmt.setString(25, dto.getLine_gateway());
			pstmt.setString(26, dto.getLine_haveip());
			pstmt.executeUpdate();

			int line_num=0;
			sql="SELECT max(line_num) as a FROM linedb";
			pstmt4 = conn.prepareStatement(sql);
			rs4 = pstmt4.executeQuery();
			if(rs4.next())
				line_num=rs4.getInt("a");
			
			sql="INSERT INTO IPDB(line_code, line_name, ip_case, ip_case2, ip_user, ip_before_user, ";
			sql+="ip_able, ip_address, ip1, ip2, ip3, ip4, ip5, ip6, line_num) ";
			sql+="values(?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?)";
			pstmt3=conn.prepareStatement(sql);
			
			String sql2;
			int inex=0;
			String user="";
			if(dto.getLine_case().equals("전용"))
				user = dto.getLine_client_name();
			
			//받아온 ip들을 등록해준다.
			for(int i=0;i<list.size();i++)
			{
				inex=1;
				//처음등록하는 ip라면 이쪽
				if(list.get(i).getLine_code()==null)
				{
					pstmt3.setString(inex++, dto.getLine_code());
					pstmt3.setString(inex++, dto.getLine_name());
					pstmt3.setInt(inex++, list.get(i).getIp_case());
					pstmt3.setString(inex++, dto.getLine_case());
					pstmt3.setString(inex++, user);
					pstmt3.setString(inex++, "");
					pstmt3.setInt(inex++, 1);
					pstmt3.setString(inex++,list.get(i).getIp_address());
					pstmt3.setInt(inex++, list.get(i).getIp1());
					pstmt3.setInt(inex++, list.get(i).getIp2());
					pstmt3.setInt(inex++, list.get(i).getIp3());
					pstmt3.setInt(inex++, list.get(i).getIp4());
					pstmt3.setInt(inex++, list.get(i).getIp5());
					pstmt3.setInt(inex++, list.get(i).getIp6());
					pstmt3.setInt(inex++, line_num);
					pstmt3.executeUpdate();
				}
				else
				{
					//기존에 있던 ip에 덧씌우는거라면 이쪽
					sql2="UPDATE ipdb SET line_code=?, ip_case=?, ip_case2=? ,ip_user=?, ip_before_user=?, ip_able=?, line_num=?";
					sql2+="WHERE ip_address=?";
					pstmt2=conn.prepareStatement(sql2);

					pstmt2.setString(1, dto.getLine_code());
					pstmt2.setInt(2, list.get(i).getIp_case());
					pstmt2.setString(3, list.get(i).getIp_case2());
					pstmt2.setString(4, user);
					pstmt2.setString(5, list.get(i).getIp_user());
					pstmt2.setInt(6, 0);
					pstmt2.setString(7, list.get(i).getIp_address());
					pstmt2.setInt(8, line_num);
					pstmt2.executeUpdate();
				}
			}
		}
		catch (Exception e) 
		{
			System.out.println("IDCLIENWRITECMD 서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null)conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
				if(rs4!=null)rs4.close();
				if(pstmt2!=null)pstmt2.close();
				if(pstmt3!=null)pstmt3.close();
				if(pstmt4!=null)pstmt4.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IDCIPWRITECMD 서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
