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

package web;

import ejb.Lines;
import ejb.LinesFacade;
import ejb.SeferFacade;
import ejb.Trains;
import ejb.TrainsFacade;
import ejb.Users;
import ejb.UsersFacade;
import ejb.Schedule;
import ejb.ScheduleFacade;
import ejb.Sefer;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;
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;

/**
 *
 * @author e1502608
 */
@WebServlet(name="AddSchedule", urlPatterns={"/AddSchedule"})
public class AddSchedule extends HttpServlet {
    @EJB
    private TrainsFacade trainsFacade;
    @EJB
    private LinesFacade linesFacade;
    @EJB
    private SeferFacade seferFacade;
    @EJB
    private ScheduleFacade scheduleFacade;
    @EJB
    private UsersFacade usersFacade;




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

        String dateOfScheduleStr= request.getParameter("date");
        String line= request.getParameter("line");



        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            out.println(
                    "<html>"+
                    "<head>"+
                    "<link href=\"design/css/addschedule.css\" type=\"text/css\" rel=\"stylesheet\" />"+
                    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-9\">"+
                    "</head>"+

                    "<body>"+
                    "<div id=\"genel\">"+

                    "<div id=\"baslik\">"+
                    "<a href='ListNews' ><img src=\"design/Pictures/homesmall.jpg\" width=\"70px\" height=\"70px\"/></a><font id=\"basliktext\"> Sefer Atama</font>"+
                    "</div>"+

                    "<table id=\"anatablo\">"+
                    "<tr><td>"+
                   "<div id=\"links\">"+
                    "<p>"+
                    "<input type=\"button\" value=\"Haber Ekle\"  id=\"linkbutton\" ONCLICK=\"window.location.href='News'\"/>"+
                    "</p><p>"+
                    "<input type=\"submit\" value=\"Görev Ekle\" id=\"linkbutton\" ONCLICK=\"window.location.href='AddSchedule'\"/>"+
                    "</p><p>"+
                    "<input type=\"button\" value=\"Sorun Giderme\"  id=\"linkbutton\" ONCLICK=\"window.location.href='jsps/KnowledgeDB.jsp'\"/>"+
                    "</p><p>"+
                    "<input type=\"button\" value=\"Kişisel Bilgiler\"  id=\"linkbutton\" ONCLICK=\"window.location.href='findperson'\"  />"+
                    "</p>"+
                    "</div>"+
                    "</td>");

                    out.println(
                    "<td>"+
                    "<div id=\"secondcell\">"+
                    "<p>"+
                    "<b>Sefer Atama Sayfasına Hoşgeldiniz! </b><br />"+
                    "Tarihe ve bölgeye göre sefer atamalarını bu sayfadan yapabilirsiniz."+
                    "</p>"+
                    "</div>"+
                    "</table>"+
                    "<form name=\"ListScheduleForm\" action=\"AddSchedule\" method=\"POST\">"+
                    "<p id=\"gorevdrop\"> Görev Bölgesi Seçiniz : <select name=\"line\"> <option SELECTED> Ankara-Istanbul Hatti </option> " +
                                                                                         "<option>Ankara-Sivas Hatti</option>"+
                                                                                         "<option>Yozgat-Nevsehir Hatti</option>"+
                                                                                         "<option>Nevsehir-Yozgat Hatti</option>"+
                                                                                         "<option>Sivas-Ankara Hatti</option>"+
                            "</select>" +
                    "<br><br> "+
                    "Tarih Seçiniz : &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp  <input name=\"date\" type=\"text\" size=\"12\" height=\"20\"/> " +
                    "<img src=\"design/Pictures/calender2.jpg\" width=\"30px\" height=\"25px\"> " +
                    "<input type=\"submit\"  size=\"20\" height=\"20\" value=\"Listele!\"/></p>"+
                    "</form>"
                           );

                    // bu kısım iften sonra
                    if(dateOfScheduleStr!=null && line!=null)
                    {

                        out.println(
                        "<p id=\"table1baslik\"> Bu hattaki atama yapılmamış makinistler </p>"+
                        "<table id=\"results\">"+
                        "<tr bgcolor=\"Navy\">"+
                        "<th><font color=\"White\"> Makinist No </font></th>"+
                        "<th><font color=\"White\"> Makinist Adı</font></th>"+
                        "<th><font color=\"White\"> Görev Bölgesi</font></th>"+
                        "</tr>"
                        );
                        // generalize!!!!! Burası düzelecek
                        String[] splitted=dateOfScheduleStr.split("/");
                        java.util.Date utilDate=new java.util.Date(Integer.parseInt(splitted[2])-1900,
                                Integer.parseInt(splitted[1])-1,
                                Integer.parseInt(splitted[0])
                                );
                        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
                        //System.out.println("DATE: "+sqlDate);
                        //System.out.println("splitted2: "+splitted[2]);
                        List<Long> notAssignedMacs=usersFacade.getMachinstWithLocation(line,sqlDate);
                        if(notAssignedMacs==null)
                            out.println("<tr><td colspan=\"4\"> Atama yapılmamış makinist bulunmamaktadır</td></tr>");
                        else{
                            int size=notAssignedMacs.size();
                            //System.out.println("SIZEEE:"+size);
                            for(int i=0;i<size;i++)
                            {
                                Users u=usersFacade.find(notAssignedMacs.get(i));
                                out.println("<tr>");
                                out.println("<td>"+notAssignedMacs.get(i) +"</td>" );
                                out.println("<td>"+u.getName()+ " "+u.getSurname() +"</td>" );
                                out.println("<td>"+u.getLocation() +"</td>" );
                                out.println("</tr>");

                            }
                        }
                        out.println("</table>");

                        //------------------------çalışanlar-----------------//
                        out.println(
                        "<p id=\"table2baslik\"> Bu hattaki atama yapılmış makinistler </p>"+
                        "<table id=\"results2\">"+
                        "<tr bgcolor=\"Navy\">"+
                        "<th><font color=\"White\"> Makinist No </font></th>"+
                        "<th><font color=\"White\"> Makinist Adı</font></th>"+
                        "<th><font color=\"White\"> Görev Bölgesi</font></th>"+
                        "<th><font color=\"White\"> Sefer Num</font></th>"+
                        "</tr>"
                        );
                        // generalize!!!!! Burası düzelecek
                        splitted=dateOfScheduleStr.split("/");
                        utilDate=new java.util.Date(Integer.parseInt(splitted[2])-1900,
                                Integer.parseInt(splitted[1])-1,
                                Integer.parseInt(splitted[0])
                                );
                        sqlDate = new java.sql.Date(utilDate.getTime());
                        //System.out.println("DATE: "+sqlDate);
                        //System.out.println("splitted2: "+splitted[2]);
                        List<Long> AssignedMacs=usersFacade.getMachinstWithLocationWithSchedule(line,sqlDate);
                        if(AssignedMacs==null)
                            out.println("<tr><td colspan=\"4\"> Atama yapılmış makinist bulunmamaktadır</td></tr>");
                        else{
                            int size=AssignedMacs.size();
                        //System.out.println("SIZEEE:"+size);
                                for(int i=0;i<size;i++)
                                {
                                    Users u=usersFacade.find(AssignedMacs.get(i));
                                    List<Long> allSchedules=scheduleFacade.scheduleForMach(u.getId());
                                    out.println("<tr>");
                                    out.println("<td>"+AssignedMacs.get(i) +"</td>" );
                                    out.println("<td>"+u.getName()+ " "+u.getSurname() +"</td>" );
                                    out.println("<td>"+u.getLocation() +"</td>" );
                                    String str="";
                                    for(int p=0;p<allSchedules.size();p++)
                                    {
                                        str=str.concat(allSchedules.get(p)+",");
                                    }
                                    str=str.substring(0, str.lastIndexOf(","));
                                    out.println("<td>"+str +"</td>" );
                                    out.println("</tr>");

                                }
                        }// end of else
                        out.println("</table>");
                        //---------------------------bitti-------------------//




                        out.println(
                        "<p id=\"table3baslik\"> Bu hattaki atama yapılmamış seferler </p>"+
                        "<table id=\"results3\">"+
                        "<tr bgcolor=\"Navy\">"+
                        "<th><font color=\"White\"> Sefer No </font></th>"+
                        "<th><font color=\"White\"> Sefer Adı </font></th>"+
                        "<th><font color=\"White\"> Tren No</font></th>"+
                        "<th><font color=\"White\"> Tren Adı</font></th>"+
                        "<th><font color=\"White\"> Çıkış Saati</font></th>"+
                        "</tr>"
                                );

                        Long lineId=linesFacade.findLineIdFromName(line);


                        List<Long> notAssignedSefers=seferFacade.findSefersFromLocation(lineId, sqlDate);
                        if(notAssignedSefers==null)
                            out.println("<tr><td colspan=\"4\"> Atama yapılmamış sefer bulunmamaktadır</td></tr>");
                        else{
                            int size=notAssignedSefers.size();
                            for(int i=0;i<size;i++)
                            {
                                Sefer S=seferFacade.find(notAssignedSefers.get(i));
                                Lines L=linesFacade.find(lineId);
                                Trains T=trainsFacade.find(S.getTrainId());
                                out.println("<tr>");
                                out.println("<td>"+notAssignedSefers.get(i) +"</td>" );
                                out.println("<td>"+L.getName()+"</td>" );
                                out.println("<td>"+T.getId() +"</td>" );
                                out.println("<td>"+T.getName() +"</td>" );
                                out.println("<td>"+S.getTimes().get(0) +"</td>" );
                                out.println("</tr>");
                            }
                        }

                         out.println("</table>");


                out.println(
                "<p id=\"teksefer\"> Tek Sefer Ataması : </br>"+
                "<table>"+
                "<form name=\"singleschedule\" action=\"AddSingleSchedule\" method=\"POST\">"+
                "<tr bgcolor=\"Navy\">"+
                "<th><font color=\"White\"> Makinist No </font></th>"+
                "<th><font color=\"White\"> Sefer No </font></th>"+
                "</tr>"+
                "<tr>"+
                "<td>"+
                "<input type=\"text\" name=\"mid\" size=\"15\" height=\"20\"/></td>"+
                "<td>"+
                "<input type=\"text\"  name=\"sid\" size=\"15\" height=\"20\"/></td></tr>"+
                "<tr>"+
                "<td><input type=\"hidden\" name=\"date\" value=\""+dateOfScheduleStr+"\"></td>"+
                "<td><input type=\"submit\"  size=\"20\" height=\"20\" value=\"Atamayı Yap !\"/></td>"+


                "</form>"+
                "</tr>"+
                "</table>"+
                "</p>"
                );


                out.println(

                "<p  id=\"coksefer\"> Dosyadan Çoklu Atama </br>"+
                "<table>"+
                "<form name=\"fileschedule\" action=\"AddFileSchedule\" method=\"POST\">"+
                "<tr>"+
                "<td>"+
                "<input type=\"text\" name=\"fileName\" size=\"45\" height=\"20\"/></td>"+
                "<td>"+
                "<input type=\"button\"  size=\"15\" height=\"20\" value=\"Gözat\"/></td></tr>"+
                "<tr colspan=\"2\" align=\"center\">"+
                "<td><input type=\"submit\"  size=\"20\" height=\"20\" value=\"Atamayı Yap !\"/></td>"+
                "</tr>"+
                "</form>"+
                "</table>"+
                "</p>"
                        );

                    }// end of if

                    out.println("<p id=\"logout\"><a href=\"Logout\">Logout!</a></p>");
out.println("</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>

}
