package idc.main.command;

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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

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

public class IdcGearModifyCmd implements IdcCmd
{
	IdcDAO dao;
	
	public IdcGearModifyCmd()
	{
		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 process = request.getParameter("process");
		int old = Integer.parseInt(request.getParameter("old"))-1;
		int num = Integer.parseInt(request.getParameter("num"))-1;
		String spec = request.getParameter("spec");
		String cpuName = request.getParameter("cpuName");
		System.out.println(cpuName);
		int price = Integer.parseInt(request.getParameter("price"));
		int firarr = Integer.parseInt(request.getParameter("firarr"));
		LinkedList<IdcGearDBDTO2> list = new LinkedList<IdcGearDBDTO2>();
		setList(list, firarr);
		modiList(list, old, num, cpuName, spec, price);
		setdb(list, firarr);
	}
//	리스트에 해당 프로세스 다 불러오기.
	private void setList(LinkedList<IdcGearDBDTO2> list, int firarr)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM gearlist WHERE firarr=? ORDER BY firArr ASC, secArr ASC";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, firarr);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				IdcGearDBDTO2 dto = new IdcGearDBDTO2();
				dto.setFirarr(rs.getInt("firarr"));
				dto.setSecarr(rs.getInt("secarr"));
				dto.setPrice(rs.getInt("price"));
				dto.setProcess(rs.getString("process"));
				dto.setSpec(rs.getString("spec"));
				dto.setCpuname(rs.getString("cpuname"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcGearModifyCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcGearModifyCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
// 불러온 리스트 수정하기.
	private void modiList(LinkedList<IdcGearDBDTO2> list, int old, int num, String cpuName, String spec, int price)
	{
		IdcGearDBDTO2 dto =  list.get(old);
		list.remove(old);
		
		dto.setCpuname(cpuName);
		dto.setPrice(price);
		dto.setSecarr(num);
		dto.setSpec(spec);
		
		list.add(num, dto);
		
		for(int i=0;i<list.size();i++)
		{
			list.get(i).setSecarr(i);
//			System.out.println(list.get(i).getPrice());
		}
		
	}
//	수정한 리스트 db에 설정하기.
	private void setdb(LinkedList<IdcGearDBDTO2> list, int firarr)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="DELETE FROM gearlist WHERE firarr=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, firarr);
			pstmt.executeUpdate();
			
			sql="INSERT INTO gearlist (firarr, secarr, process, spec, cpuname, price) ";
			sql+="values(?,?,?,?,?,?)";
			pstmt2 = conn.prepareStatement(sql);
			IdcGearDBDTO2 dto;
			for(int i=0;i<list.size();i++)
			{
				dto = list.get(i);
				pstmt2.setInt(1,dto.getFirarr());
				pstmt2.setInt(2, dto.getSecarr());
				pstmt2.setString(3, dto.getProcess());
				pstmt2.setString(4, dto.getSpec());
				pstmt2.setString(5, dto.getCpuname());
				pstmt2.setInt(6, dto.getPrice());
				pstmt2.executeUpdate();
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcGearModifyCmd - setdb 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcGearModifyCmd - setdb 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
