package idc.service.command;

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 idc.model.*;

public class IdcServiceIpAddCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	//부가서비스 추가 화면 정보
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		
		String client_name = request.getParameter("client_name");
		String service_code = request.getParameter("service_code");
		String addDB_case = request.getParameter("addDB_case");		
				
		IpAddDTO ipAdd;
		ipAdd = getIpAddDTO(service_code, client_name, addDB_case);
		request.setAttribute("ipAdd", ipAdd);
		
		ArrayList<IdcAddServiceCSDTO> csList;
		csList = getCsList(service_code, "cs");
		request.setAttribute("csList", csList);
		
		ArrayList<IdcAddServiceCSDTO> kcsList;
		kcsList = getCsList(service_code, "kcs");
		request.setAttribute("kcsList", kcsList);		
	}
	
	public IpAddDTO getIpAddDTO(String service_code, String client_name, String addDB_case){		
		IpAddDTO ipAdd = new IpAddDTO();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null, pstmt2 = null;
		ResultSet rs = null, rs1 = null, rs2 = null;

		String sql;
		String addDB_code = "";
		String kcs_code = "";
		int num = 0;
		
		try {
			conn = dao.getConn();
			
			sql = "SELECT count(addDB_case) as num FROM addDB WHERE service_code=? and addDB_case=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			pstmt.setString(2, addDB_case);
			rs = pstmt.executeQuery();
			String temp = "";
			
			if(rs.next()){
				num = rs.getInt("num");
				num = num + 1;
				
				switch (addDB_case) {
				case "ip":
					temp = "IP-"; 
					break;
				case "cs":
					temp = "CS-"; 
					break;	
				}
				
				addDB_code = temp + String.format("%03d", num);
			}
			
			sql = "SELECT count(addDB_case) as num FROM addDB WHERE service_code=? and addDB_case='kcs'";
			pstmt1 = conn.prepareStatement(sql);
			pstmt1.setString(1, service_code);
			rs1 = pstmt1.executeQuery();
			
			if(rs1.next()){
				int knum = rs1.getInt("num") + 1;
				kcs_code = "KCS-" + String.format("%03d", knum);
				ipAdd.setKcs_code(kcs_code);
			}
									
			sql = "SELECT server_code, service_ip, service_line, client_code, service_payment_date, service_defence, curdate() AS curdate ";
			sql += " From serviceDB WHERE service_code=? ";
			pstmt2 = conn.prepareStatement(sql);
			pstmt2.setString(1, service_code);
			rs2 = pstmt2.executeQuery();
			
			while(rs2.next()){				
				ipAdd.setService_code(service_code);
				ipAdd.setServer_code(rs2.getString("server_code"));
				ipAdd.setIp_address(rs2.getString("service_ip"));
				
				String service_line = rs2.getString("service_line");
				String line = "";
				
				switch (service_line){
				case "normal":
					line = "일반";
					break;
				case "defence":
					line = "방어";
					break;
				case "solo":
					line = "전용";
					break;
				case "test":
					line = "테스트";
					break;
				case "soloLine":
					line = "전용 라인";
					break;
				case "substitution":
					line = "대체";
					break;
				default:
					break;
				}
				
				ipAdd.setService_line(line);
				
				ipAdd.setClient_name(client_name);
				ipAdd.setAddDB_case(addDB_case);
				ipAdd.setClient_code(rs2.getString("client_code"));
				ipAdd.setAddDB_code(addDB_code);
				ipAdd.setService_payment_date(rs2.getDate("service_payment_date"));
				ipAdd.setService_defence(rs2.getString("service_defence"));
				ipAdd.setCurdate(rs2.getDate("curdate"));
				
			}		
			
		} catch (Exception e) {			
			e.printStackTrace();						
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return ipAdd;
	}
	
public ArrayList<IdcAddServiceCSDTO> getCsList(String service_code, String addDB_case){
		
	ArrayList<IdcAddServiceCSDTO> list = new ArrayList<IdcAddServiceCSDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT addDB_code";
			sql += " FROM addDB ";
			sql += " WHERE service_code=? and addDB_case=?";
			sql += " ORDER BY addDB_code asc";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);			
			pstmt.setString(2, addDB_case);
			
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				String addDB_code = rs.getString("addDB_code");
				
				IdcAddServiceCSDTO writing = new IdcAddServiceCSDTO();				
				writing.setAddDB_code(addDB_code);
				writing.setAddDB_case(addDB_case);
				
				list.add(writing);					
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return list;	
	}		
	
}