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

package web;

import ejb.AnsweredKDB;
import ejb.AnsweredKDBFacade;
import ejb.Knowledge;
import ejb.KnowledgeFacade;
import ejb.NewsEntity;
import ejb.NewsEntityFacade;
import ejb.Users;
import ejb.UsersFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.ListIterator;
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 java.text.SimpleDateFormat;
import javax.servlet.http.HttpSession;

/**
 *
 * @author zwergnase
 */
@WebServlet(name="ShowTopicContent", urlPatterns={"/ShowTopicContent"})
public class ShowTopicContent extends HttpServlet {
    @EJB
    private UsersFacade usersFacade;
    @EJB
    private KnowledgeFacade knowledgeFacade;
    @EJB
    private AnsweredKDBFacade answeredKDBFacade;





    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        HttpSession session=request.getSession(true);
        boolean isRegistered=(session.getAttribute("userName")==null ||session.getAttribute("activated")==null||
        session.getAttribute("activated").toString().equals("false")) ? false : true;



        int topic= Integer.parseInt(request.getParameter("topic"));
        String section=request.getParameter("section");
        PrintWriter out = response.getWriter();


        out.println(
                    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"+ \"http://www.w3.org" +
                    "/TR/xhtml1/DTD/xhtml1-strict.dtd\">"+
                     "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"+

                     "<head>"+
                        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"+
                        "<title>FAQ</title>"+
                        "<link rel=\"stylesheet\" type=\"text/css\" href=\"design/css/faqstyle.css\" />"+
                        "<script src=\"http://www.google.com/jsapi\"></script>"+
                        "<script>google.load(\"jquery\", \"1.2.6\");"+
                        "</script>"+
                        "<script type=\"text/javascript\" src=\"faq.js\"></script>"+
                        "<script type=\"text/javascript\" src=\"forAnswer.js\"></script>"+
                        "</head>"+
                        "<body>"+
                        "<div id=\"genel\">"+
                        "<div id=\"baslik\">"+
                        "<a href=\"jsps/PassengerHome.jsp\"><img src=\"design/Pictures/homesmall.jpg\" width=\"70px\" height=\"70px\" align=\"left\"/></a><font id=\"basliktext\">" +
                        " Ana Sayfa</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>"+
                            "</tr> </table></div>"
                    );

       if(section.equals("GEN"))
        {
            out.println("<font id=\"header\">"+topic+" konusunda cevaplanmış sorular :</font>");
            out.println("<div id=\"general\">");

            List<AnsweredKDB> l=answeredKDBFacade.findBySectionAndTopic(2, topic);
            if(l!=null)
            {
                out.println("<div id=\"page-wrap\">");

                int size=l.size();
                for(int i=0;i<size;i++)
                {

                     out.println(
                                "<dl class=\"faq\">"+
                                "<dt>"+l.get(i).getTitle()+"</dt>"+
                                "<dd class=\"answer\">"+
                                "<div>"+l.get(i).getBody()+"</div></dd></dl>");
                }

                out.println("</div>");
            }
            else
                out.println("Bu konu başlığında görüntülenecek madde bulunamadı");

            out.println("</div></body></html>");
       }

        else // QUE
        {
             out.println("<div id=\"<general\">");
            switch (topic)
            {
                case 1:
                    out.println("<font id=\"header\">Giriş Sayfası Sorunları:</font>");
                    break;
                case 2:
                    out.println("<font id=\"header\">Ana Sayfa Sorunları:</font>");
                    break;
                case 3:
                    out.println("<font id=\"header\">Tren Takip Sistemi Sorunları:</font>");
                    break;
                case 4:
                    out.println("<font id=\"header\">Haber Bağlantıları Sorunları:</font>");
                    break;
                case 5:
                    out.println("<font id=\"header\">Kayıt Olma Sorunları:</font>");
                    break;
                case 6:
                    out.println("<font id=\"header\">Kişisel Bilgileri Değiştirme Sorunları:</font>");
                    break;
                case 7:
                    out.println("<font id=\"header\">Bulunan Yolları Kaydetme Sorunları:</font>");
                    break;
                case 8:
                    out.println("<font id=\"header\">Kullanım Klavuzuna Ulaşma Sorunları:</font>");
                    break;
                case 9:
                    out.println("<font id=\"header\">Sorun Bildirme Soruları:</font>");
                    break;
                default:
                    out.println("<font id=\"header\">Diğerleri:</font>");

                    break;
            }
            List l=knowledgeFacade.findByTopic(topic);

            if(l!=null)
            {
                int size= l.size();
                out.println("<div id=\"page-wrap\">");
                for(int i=0;i<size;i++)
                {
                    Knowledge k=knowledgeFacade.find(l.get(i));
                    SimpleDateFormat newFormatter = new SimpleDateFormat("dd/MM/yyy");
                    String dateStringInNewFormat = newFormatter.format(k.getCreatedIn());
                    Users U = usersFacade.find(k.getCreatedBy());
                    int type=Integer.parseInt(session.getAttribute("type").toString());
                    List<Long> cevaplar=knowledgeFacade.findAnswerTo(k.getId());
                    int cevapSize=cevaplar.size();
                    Long atan= k.getCreatedBy();
                    Long simdiki=usersFacade.getIdByUser(session.getAttribute("userName").toString());

                    if(type==2 && !atan.equals(simdiki))
                    {
                      
                      out.println(
                                "<dl class=\"faq\">"+
                                "<dt>"+k.getTitle()+" ("+dateStringInNewFormat+")</dt>"+
                                "<dd class=\"answer\">"+
                                "<div>"+"<font color='DarkBlue'><i>Soruyu soran: </i></font> " + U.getUserName()+"<br />"+
                                "<font color=DarkBlue><i> Soru: </i></font>"+k.getDescription()+"<br />");


                      for(int p=0;p<cevapSize;p++)
                      {
                          Knowledge ans=knowledgeFacade.find(cevaplar.get(p));
                          String gonderen=usersFacade.find(ans.getCreatedBy()).getName();
                          out.println(
                                  "<p><font color=\"DarkBlue\"><i> Cevap:</i></font><br />" +
                                  "<font color=\"DarkBlue\"><i>Gönderen: </i></font>"+gonderen+"("+newFormatter.format(ans.getCreatedIn())+")"+
                                  "<p>"+ans.getDescription()+"</p></p>"
                                  );
                      }

                      out.println("</div></dd></dl>");
                    }
                    else
                    {
                        out.println(
                                "<dl class=\"faq\">"+
                                "<dt>"+k.getTitle()+" ("+dateStringInNewFormat+") </dt>"+
                                "<dd class=\"answer\">"+
                                "<div>"+"<font color='DarkBlue'><i>Soruyu soran: </i></font> " + U.getUserName()+"<br />"+
                                "<font color=DarkBlue><i> Soru: </i></font>"+k.getDescription()+"<br />"+
                                "<span id=\"right\"><a href='#' class=\"AnswerBox\"> Cevapla </a> </span>");
                         for(int p=0;p<cevapSize;p++)
                      {
                          Knowledge ans=knowledgeFacade.find(cevaplar.get(p));
                          String gonderen=usersFacade.find(ans.getCreatedBy()).getName();
                          out.println(
                                  "<p><font color=\"DarkBlue\"><i> Cevap:</i></font><br />" +
                                  "<font color=\"DarkBlue\"><i>Gönderen: </i></font>"+gonderen+"("+newFormatter.format(ans.getCreatedIn())+")"+
                                  "<p>"+ans.getDescription()+"</p></p>"
                                  );
                      }



                                out.println(
                                "</div></dd>" +
                                "<dd class=\"cevapla\">"+
                                "<div>" +
                                "<form name=\"admCevap\" action=\"AddAnswer\" method=\"POST\">" +
                                "<input type=\"hidden\" value=\""+k.getId()+"\" name=\"postId\">"+
                                "<input type=\"hidden\" value=\""+topic+"\" name=\"topic\">"+
                                "<input type=\"hidden\" value=\""+section+"\" name=\"section\">"+
                                "<textarea name=\"adminCevap\" rows=\"3\" cols=\"80%\"> </textarea><br /> " +
                                "<input type='submit' value=\"Gönder\" id=\"btn\">" +
                                "</form>"+
                                "</div>" +
                                "</dd>"+
                                "</dl>");
                    }

               }
                    out.println("</div>");
            }
            else
            {
                out.println("Bu konu başlığında görüntülenecek madde bulunamadı");
            }
            out.println("</div></body></html>");
        }

    

    }
    
    // <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>

}





