/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package web;

import ejb.SeferFacade;
import ejb.SeyirDefteriFacade;
import ejb.UsersFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Vector;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author zwergnase
 */
@WebServlet(name="SeyirDefteri", urlPatterns={"/SeyirDefteri"})
public class SeyirDefteri extends HttpServlet {
    @EJB
    private SeyirDefteriFacade seyirDefteriFacade;
    @EJB
    private UsersFacade usersFacade;
  


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        HttpSession session=request.getSession(true);
        Long creationTime = session.getCreationTime();
        String userName=session.getAttribute("userName").toString();
        int type=Integer.parseInt(session.getAttribute("type").toString());
        boolean isRegistered=(session.getAttribute("userName")==null ||session.getAttribute("activated")==null||
                session.getAttribute("activated").toString().equals("false")) ? false : true;

        Long id=usersFacade.getIdByUser(userName);

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        try {



out.println(
 "<html>"+
    "<head>"+
        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"+
        "<link href=\"design/css/seyirdefteri.css\" type=\"text/css\" rel=\"stylesheet\" />"+
        "<title>Seyir Defteri</title>"+
    "</head>"+
    "<body>"+
        "<div id=\"genel\">"+

        "<div id=\"baslik\">"+
        "<a href=\"jsps/PassengerHome.jsp\"><img src=\"design/Pictures/homesmall.jpg\" width=\"70px\" height=\"70px\"/></a><font id=\"basliktext\">Seyir Defteri</font>"+
        "</div>"+
        "<table id=\"anatablo\">"+
        "<tr><td>"+
        "<div id=\"links\">"+
        "<p>"+
        "<input type=\"button\" value=\"Tren Takip Sistemi\"  id=\"linkbutton\" />"+
        "</p><p>"+
        "<input type=\"button\" value=\"Kişisel Bilgiler\" id=\"linkbutton\" ONCLICK=\"window.location.href='findperson'\"/>"+
        "</p><p>"+
        "<input type=\"button\" value=\"Seyir Defteri\" id=\"linkbutton\" ONCLICK=\"window.location.href='SeyirDefteri'\"/>"+
        "</p><p>"+
        "<input type=\"button\" value=\"Sorun Giderme\"  id=\"linkbutton\" ONCLICK=\"window.location.href='jsps/KnowledgeDB.jsp'\"/>"+
        "</p><p>"+
        "<input type=\"button\" value=\"Bize Ulaşın\"  id=\"linkbutton\"/>"+
        "</p>"+
        "</div>"+
        "</td>"+

        "<td >"+
        "<div id=\"secondcell\">"+
            "<p id=\"genelbaslik\"> Seyir Defteri</p>");

        List<Long> biletIds=seyirDefteriFacade.getBiletByUserId(id);
        List<Long> seferIds=seyirDefteriFacade.getSeferByUserId(id);
        int biletsize=biletIds.size();
        int sefersize=seferIds.size();
        Vector<Long> trainsIds=new Vector<Long>();


        if(sefersize==0 && biletsize==0)
        {
            out.println("<p id=\"nosefer\">Kaydedilmiş bilet ya da sefer bulunmamaktadır.</p>");
        }
        else
        {

            if(biletsize>0)
            {
                out.println("<form name=\"BiletForm\" action=\"modifyBilet\" method=\"POST\">");

                out.println("<H1>BİLETLER</H1>");
              for(int i=0;i<biletsize;i++)
                {

                    ejb.SeyirDefteri bilet=seyirDefteriFacade.find(biletIds.get(i));

                    int linesize=bilet.getPlaces().size()-1;
                    float toplamCost=0;

                    out.println(
                    "<table id=\"results\" align=\"center\" border=\"1\">"+
                        "<tr bgcolor=\"Navy\">"+
                        "<th bgcolor=\"Lavender\" rowspan=\""+linesize+2+" \">" +
                        "<input type=\"checkbox\" name=\"bilets\" value=\""+bilet.getId()+"\"/></th>"+
                        "<th><font color=\"White\"> - </font></th>"+
                        "<th><font color=\"White\"> Tarih</font></th>"+
                        "<th><font color=\"White\"> Tren No</font></th>"+
                        "<th><font color=\"White\"> Kalkış Yeri</font></th>"+
                        "<th><font color=\"White\"> Kalkış Saati</font></th>"+
                        "<th><font color=\"White\"> Varış Yeri</font></th>"+
                        "<th><font color=\"White\"> Varış Saati</font></th>"+
                        "<th><font color=\"White\"> Yolculuk Süresi</font></th>"+
                        "<th><font color=\"White\"> Ücret</font></th>"+
                        "<th><font color=\"White\"> Aktarma</font></th>"+               
                        "</tr>");

                    
                    for(int j=0;j<linesize;j++)
                    {

                        java.sql.Time beginTime=bilet.getDepartureTime().get(j);
                        java.sql.Time endTime=bilet.getArrivalTime().get(j);
                        int hourSum=endTime.getHours()-beginTime.getHours();
                        int minSum=endTime.getMinutes()-beginTime.getMinutes();
                        if(minSum<0)
                            {
                                minSum+=60;
                                hourSum--;
                            }
                        String timeString="";
                        if(hourSum<10)
                                timeString=timeString.concat("0"+hourSum+":");
                            else
                                timeString=timeString.concat(hourSum+":");
                        if(minSum<10)
                            timeString=timeString.concat("0"+minSum);
                        else
                            timeString=timeString.concat(""+minSum);



                        out.println("<tr>"+
                                    "<td>-</td>"+
                                    "<td>"+bilet.getTravelDate() +"</td>"+
                                    "<td>"+bilet.getTrainId().get(j) +"</td>"+
                                    "<td>"+bilet.getPlaces().get(j)+"</td>"+
                                    "<td>"+bilet.getDepartureTime().get(j)+"</td>"+
                                    "<td>"+bilet.getPlaces().get(j+1)+"</td>"+
                                    "<td>"+bilet.getArrivalTime().get(j)+"</td>"+
                                    "<td>"+timeString+"</td>"+
                                    "<td>"+bilet.getCost().get(j)+"</td>"+
                                    "<td>-</td>"+
                                    "</tr>");
                        toplamCost+=bilet.getCost().get(j);
                        if(!trainsIds.contains(bilet.getTrainId().get(j)))
                            trainsIds.addElement(bilet.getTrainId().get(j));

                    }//end of for each line of bilet

                        java.sql.Time beginTime=bilet.getDepartureTime().get(0);
                        java.sql.Time endTime=bilet.getArrivalTime().lastElement();
                        int hourSum=endTime.getHours()-beginTime.getHours();
                        int minSum=endTime.getMinutes()-beginTime.getMinutes();
                        if(minSum<0)
                            {
                                minSum+=60;
                                hourSum--;
                            }
                        String timeString="";
                        if(hourSum<10)
                                timeString=timeString.concat("0"+hourSum+":");
                            else
                                timeString=timeString.concat(hourSum+":");
                        if(minSum<10)
                            timeString=timeString.concat("0"+minSum);
                        else
                            timeString=timeString.concat(""+minSum);


             out.println(
                        "<tr bgcolor=\"RoyalBlue\">"+
                        "<td><b>Özet:</b></td>"+
                        "<td>"+bilet.getTravelDate()+"</td>"+
                        "<td>-</td>"+
                        "<td>"+bilet.getPlaces().get(0)+ "</td>"+
                        "<td>"+bilet.getDepartureTime().get(0)+"</td>"+
                        "<td>"+bilet.getPlaces().lastElement()+"</td>"+
                        "<td>"+bilet.getArrivalTime().lastElement()+"</td>"+
                        "<td>"+timeString+"</td>"+
                        "<td>"+toplamCost+"</td>"+
                        "<td>"+(trainsIds.size()-1)+"</td>"+
                        "</tr>");




                  trainsIds.clear();
                  out.println("</table><br /><br />");
                }//end of for each bilet
             
                out.println(
                            "<p id=\"buttonp20\" align=\"center\">"+
                            "<input type=\"Submit\" value=\"İptal Et\"  id=\"nbtn\" />"+
                            "</p></form>"
                            );

            } // end of if bilet

            trainsIds.clear();

          if(sefersize>0)
          {

           out.println("<form name=\"SeferForm\" action=\"modifySefer\" method=\"POST\">");

            out.println("<H1>SEFERLER</H1>");

            for(int i=0;i<sefersize;i++)
            {
                ejb.SeyirDefteri sefer=seyirDefteriFacade.find(seferIds.get(i));
                int linesize=sefer.getPlaces().size()-1;
                float toplamCost=0;


                out.println(
                "<table id=\"results\" align=\"center\" border=\"1\">"+
                    "<tr bgcolor=\"Navy\">"+
                    "<th bgcolor=\"Lavender\" rowspan=\""+linesize+2+" \">" +
                    "<input type=\"checkbox\" name=\"sefers\" value=\""+sefer.getId()+"\"/></th>"+
                    "<th><font color=\"White\"> - </font></th>"+
                    "<th><font color=\"White\"> Tarih</font></th>"+
                    "<th><font color=\"White\"> Tren No</font></th>"+
                    "<th><font color=\"White\"> Kalkış Yeri</font></th>"+
                    "<th><font color=\"White\"> Kalkış Saati</font></th>"+
                    "<th><font color=\"White\"> Varış Yeri</font></th>"+
                    "<th><font color=\"White\"> Varış Saati</font></th>"+
                    "<th><font color=\"White\"> Yolculuk Süresi</font></th>"+
                    "<th><font color=\"White\"> Ücret</font></th>"+
                    "<th><font color=\"White\"> Aktarma</font></th>"+
                    "</tr>");
               
                for(int j=0;j<linesize;j++)
                {

                        java.sql.Time beginTime=sefer.getDepartureTime().get(j);
                        java.sql.Time endTime=sefer.getArrivalTime().get(j);
                        int hourSum=endTime.getHours()-beginTime.getHours();
                        int minSum=endTime.getMinutes()-beginTime.getMinutes();
                        if(minSum<0)
                            {
                                minSum+=60;
                                hourSum--;
                            }
                        String timeString="";
                        if(hourSum<10)
                                timeString=timeString.concat("0"+hourSum+":");
                            else
                                timeString=timeString.concat(hourSum+":");
                        if(minSum<10)
                            timeString=timeString.concat("0"+minSum);
                        else
                            timeString=timeString.concat(""+minSum);

                    out.println("<tr>"+
                                "<td>-</td>"+
                                "<td>"+sefer.getTravelDate() +"</td>"+
                                "<td>"+sefer.getTrainId().get(j) +"</td>"+
                                "<td>"+sefer.getPlaces().get(j)+"</td>"+
                                "<td>"+sefer.getDepartureTime().get(j)+"</td>"+
                                "<td>"+sefer.getPlaces().get(j+1)+"</td>"+
                                "<td>"+sefer.getArrivalTime().get(j)+"</td>"+
                                "<td>"+timeString+"</td>"+
                                "<td>"+sefer.getCost().get(j)+"</td>"+
                                "<td>-</td>"+
                                "</tr>");
                    toplamCost+=sefer.getCost().get(j);
                    if(!trainsIds.contains(sefer.getTrainId().get(j)))
                        trainsIds.addElement(sefer.getTrainId().get(j));

                }//end of for each line of bilet
                //make a last line
                        java.sql.Time beginTime=sefer.getDepartureTime().get(0);
                        java.sql.Time endTime=sefer.getArrivalTime().lastElement();
                        int hourSum=endTime.getHours()-beginTime.getHours();
                        int minSum=endTime.getMinutes()-beginTime.getMinutes();
                        if(minSum<0)
                            {
                                minSum+=60;
                                hourSum--;
                            }
                        String timeString="";
                        if(hourSum<10)
                                timeString=timeString.concat("0"+hourSum+":");
                            else
                                timeString=timeString.concat(hourSum+":");
                        if(minSum<10)
                            timeString=timeString.concat("0"+minSum);
                        else
                            timeString=timeString.concat(""+minSum);

                        
             out.println(
                        "<tr bgcolor=\"RoyalBlue\">"+
                        "<td><b>Özet:</b></td>"+
                        "<td>"+sefer.getTravelDate()+"</td>"+
                        "<td>-</td>"+
                        "<td>"+sefer.getPlaces().get(0)+ "</td>"+
                        "<td>"+sefer.getDepartureTime().get(0)+"</td>"+
                        "<td>"+sefer.getPlaces().lastElement()+"</td>"+
                        "<td>"+sefer.getArrivalTime().lastElement()+"</td>"+
                        "<td>"+timeString+"</td>"+
                        "<td>"+toplamCost+"</td>"+
                        "<td>"+(trainsIds.size()-1)+"</td>"+
                        "</tr>");
             out.println("</table><br /><br />");
             trainsIds.clear();
            }//end of for

             out.println(
                            "<p id=\"buttonp20\" align=\"center\">"+
                            "<input type=\"Submit\" value=\"İptal Et\" name=\"submit\" id=\"nbtn\" />"+
                            "<input type=\"Submit\" value=\"Bilete Dönüştür\" name=\"submit\" id=\"nbtn\" />"+
                            "</p></form>"
                            );

          }//end of if


        }//end of else
        out.println("</div></td></tr></table></div></body></html>");

        } finally { 
            out.close();
        }
    } 

   
    // <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
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    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
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

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

}
