/*
 * renegroomlist.java
 *
 * Created on November 30, 2008, 9:32 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Srikrishna
 */
public class regnegroomlist extends HttpServlet {
    
  Connection con;
   Statement st;
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String type=request.getParameter("type");
        String[]datetimelist=request.getParameterValues("datetimelist");
        System.out.println("array length:"+datetimelist.length);
        try{
             con=DataBase.getConnection();
             st=con.createStatement();
        }catch(SQLException sq){
            sq.printStackTrace();
        }
        String[]datetimepairs=new String[8];
        String[] dummy=new String[8];
        
       for(int i=0;i<datetimelist.length;++i){
           
           if(datetimelist[i].contains("],")){
               //System.out.println("new pair :"+datetimelist[i]);
               dummy[0]=datetimelist[i].substring(0,datetimelist[i].lastIndexOf("],")+1);
               dummy[1]=datetimelist[i].substring(datetimelist[i].lastIndexOf("],")+2);
               if(dummy[0].contains("],")){
                   
                  datetimepairs[0]= dummy[0].substring(0,dummy[0].indexOf("],")+1);
                  datetimepairs[1]=dummy[0].substring(dummy[0].indexOf("],")+2);
                  datetimepairs[2]=dummy[1];
               }
               else{
                   datetimepairs=dummy;
               }
               
              
           }
           else{
               datetimepairs[0]=datetimelist[i];
           }
           
           
       }
        
        ArrayList<String> rooms=new ArrayList<String>() ;
        ArrayList<String> employees=new ArrayList<String>() ;
                
        for(int i=0;i<datetimepairs.length;++i){
            
            if(datetimepairs[i]==null)break;
            try {
                //System.out.println("to query time pair:"+datetimepairs[i]);
                
                String starttime = datetimepairs[i].substring(0,datetimepairs[i].indexOf("-"));
                String endtime = datetimepairs[i].substring(datetimepairs[i].indexOf("-")+1);
                //System.out.println("start time :"+starttime);
                //System.out.println("end time :"+endtime);
                String starttimestamp = parseDate(starttime);
                String endtimestamp = parseDate(endtime);
                ResultSet rs = st.executeQuery("select * from roomlist where type = '" + type + "' and roomid not in(select roomid from bookedrooms where startDateTime='" + starttimestamp + "' and endDateTime='" + endtimestamp + "')");
                while (rs.next()) {
                    rooms.add(rs.getInt("roomid")+" / "+rs.getString("name") + " : " + starttime + "-" + endtime);
                }
            } catch (SQLException ex) {
                Logger.getLogger(regnegroomlist.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.out.println("roomslist"+rooms.size());
        request.getSession().setAttribute("rooomlist", rooms);
        try{
            ResultSet emprs=st.executeQuery("select * from signup");
            while(emprs.next()){
                employees.add(emprs.getString("first_name")+" "+emprs.getString("last_name")+"-"+emprs.getString("empid"));
            }
            System.out.println("employeeslist"+employees.size());
            String id = (String)request.getSession().getAttribute("id");
            String agenda = (String)request.getSession().getAttribute("agenda");
            request.getSession().setAttribute("rooomlist", rooms);
            request.getSession().setAttribute("empllist", employees);
            request.getSession().setAttribute("agenda", agenda);
            request.getSession().setAttribute("id", id);
            String[] finaldatelist = (String[])(request.getSession().getAttribute("finaldatelist"));
            //ArrayList<ArrayList<String[]>> totallist = (ArrayList<ArrayList<String[]>>)(request.getSession().getAttribute("totallist"));
            
            request.getSession().setAttribute("finaldatelist", finaldatelist);
           // request.getSession().setAttribute("totallist", totallist);
            if(!(employees.isEmpty()) && !(rooms.isEmpty()) )
            {
                if(employees != null && rooms !=null)
                {
                 response.sendRedirect("renogotiatenewrequest.jsp");   
                }
            }
            
            //RequestDispatcher rd=getServletContext().getRequestDispatcher("/renogotiatenewrequest.jsp");
           // rd.forward(request, response);
        
        }catch (SQLException ex) {
                Logger.getLogger(regnegroomlist.class.getName()).log(Level.SEVERE, null, ex);
            }
  } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public String parseDate(String str){
        String startdate = str.substring(0,str.indexOf("["));
        String sttime = str.substring(str.indexOf("[")+1,str.length()-1);
        
        Date stdt=new Date(startdate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newstDate=sdf.format(stdt);
          
               
        sdf=new SimpleDateFormat("HH:mm:ss");
        Date sttd = new Date(startdate + " "+sttime);
        
        String newstTime=sdf.format(sttd);
        String starttimestamp=newstDate+" "+newstTime;
        
        return starttimestamp;
        
    }
    
}
