package idc.line.command;

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

import java.io.IOException;
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;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class IdcLineIPWriteCmd implements IdcCmd 
{
	IdcDAO dao;
	ArrayList<IdcIPDBDTO> dto;
	public IdcLineIPWriteCmd() 
	{
		dao = new IdcDAO();
		dto = new ArrayList<IdcIPDBDTO>();
	}
	
	//ip등록
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		String[] ip = request.getParameterValues("Line_ip");
		int[] limit= new int[2];
				limit[0]=Integer.parseInt(ip[3]);
				limit[1]=Integer.parseInt(ip[4]);
		
		dto = IdcLineIPWrite(ip, limit);
		JSONArray jArr = new JSONArray();
		JSONObject jObj = new JSONObject();
		jObj.put("ip", dto);
		jArr.add(jObj);
		
		try
		{
			response.getWriter().write(jArr.toString());
			request.getSession().setAttribute("IdcIPDBList", dto);
		} 
		catch (IOException e)
		{
			System.out.println("IdcLineIPWriteCmd - json 보내기 실패");
			e.printStackTrace();
		}
	}
	
	private ArrayList<IdcIPDBDTO> IdcLineIPWrite(String[] ip, int[] limit)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		ArrayList<IdcIPDBDTO> list= new ArrayList<IdcIPDBDTO>();
		ArrayList<IdcIPDBDTO> temp= new ArrayList<IdcIPDBDTO>();
		
		try
		{
			conn=dao.getConn();
			String sql="SELECT * FROM ipdb WHERE ip4 BETWEEN ? AND ? AND ip1=? AND ip2=? AND ip3=? ";
					sql+="ORDER BY ip4 asc ";
			pstmt =conn.prepareStatement(sql);
			
			pstmt.setInt(1, limit[0]);
			pstmt.setInt(2, limit[1]);
			pstmt.setString(3, ip[0]);
			pstmt.setString(4, ip[1]);
			pstmt.setString(5, ip[2]);
			
			rs=pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcIPDBDTO dto= new IdcIPDBDTO();
				dto.setLine_code(rs.getString("line_code"));
				dto.setIp_case(rs.getInt("ip_digit"));
				dto.setIp_case(rs.getInt("ip_case"));
				dto.setIp_case2(rs.getString("ip_case2"));
				dto.setIp_user(rs.getString("ip_user"));
				dto.setIp_before_user(rs.getString("ip_before_user"));
				dto.setIp_able(rs.getInt("ip_able"));
				dto.setIp_address(rs.getString("ip_address"));
				dto.setIp1(rs.getInt("ip1"));
				dto.setIp2(rs.getInt("ip2"));
				dto.setIp3(rs.getInt("ip3"));
				dto.setIp4(rs.getInt("ip4"));
				dto.setIp5(rs.getInt("ip5"));
				dto.setIp6(rs.getInt("ip6"));
				temp.add(dto);
			}
			int index = 0;
			for(int i=limit[0]; i<=limit[1];i++)
			{
				if(temp.size()>index && temp.get(index).getIp4()==i)
				{
					index++;
					continue;
				}
				else
				{
					list.add(returnNullDTO(ip,i));
				}
			}
		}
		catch (Exception e) 
		{
			System.out.println("IDCLINEIPWRITECMD 에서의 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("IDCLINEIPWRITECMD 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		return list;
	}
	private IdcIPDBDTO returnNullDTO(String[] ip, int ip4)
	{
		String sip[]=new String[4];
		IdcIPDBDTO dto = new IdcIPDBDTO();
		dto.setLine_code(null);
		dto.setIp_case(1);
		dto.setIp_case2("Main_ip");
		dto.setIp_user(null);
		dto.setIp_before_user(null);
		dto.setIp_able(1);
		dto.setIp_address(ip[0]+"."+ip[1]+"."+ip[2]+"."+ip4);
		dto.setIp1(Integer.parseInt(ip[0]));
		dto.setIp2(Integer.parseInt(ip[1]));
		dto.setIp3(Integer.parseInt(ip[2]));
		dto.setIp4(ip4);
		dto.setIp5(0);
		dto.setIp6(0);
		return dto;
	}
}
