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

package web;

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 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 sinem
 */
@WebServlet(name="ShowMessage", urlPatterns={"/ShowMessage"})
public class ShowMessage extends HttpServlet {

    @EJB
    private UsersFacade usersFacade;

    @EJB
    private KnowledgeFacade knowledgeFacade;



    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

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




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

        Long id=Long.parseLong(request.getParameter("id"));

        Knowledge K = knowledgeFacade.find(id);
        Users U = usersFacade.find(K.getCreatedBy());


        PrintWriter out = response.getWriter();
        try {
             //TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<link href=\"design/css/showmessage.css\" type=\"text/css\" rel=\"stylesheet\" />");
            out.println("<title>Soru</title>");
            out.println("</head>");
            out.println("<script type=\"text/javascript\" >" +
                    " function deneme(str){confirm(str);}"+
                    "</script>");
            out.println("<body>");
            out.println("<font id=\"header\">Başlık : "+K.getTitle()+"</font>");
            out.println("<table id=\"general\">");

            switch (K.getTopic())
            {
                case 1:
 out.println("<tr><td>Kategori:</td><td> Giriş Sayfası</td></tr>");
                    break;
                case 2:
                     out.println("<tr><td>Kategori:</td><td> Ana Sayfa</td></tr>");

                    break;
                case 3:
                     out.println("<tr><td>Kategori:</td><td>Tren Takip Sistemi</td></tr>");
                    break;
                case 4:
                     out.println("<tr><td>Kategori:</td><td>Haber Bağlantıları</td></tr>");
                    break;
                case 5:
                     out.println("<tr><td>Kategori:</td><td>Kayıt Olma</td></tr>");
                    break;
                case 6:
                     out.println("<tr><td>Kategori:</td><td> Kişisel Bilgiler</td></tr>");
                    break;
                case 7:
                     out.println("<tr><td>Kategori:</td><td> Bulunan Yolları Kaydetme</td></tr>");
                    break;
                case 8:
                     out.println("<tr><td>Kategori:</td><td> Kullanım Klavuzuna Ulaşma</td></tr>");
                    break;
                case 9:
                     out.println("<tr><td>Kategori:</td><td> Sorun Bildirme</td></tr>");
            break;
                    default:
                         out.println("<tr><td>Kategori:</td><td>Diğerleri</td></tr>");
                        break;
            }

            out.println("<tr><td colspan=\"2\">Tarih "+ K.getCreatedIn()+" soruyu soran " + U.getUserName()+ ": </td></tr>");


            //out.println("<tr><td colspan=\"2\" >This message is flagged as inappropriate by "+ news.getInappropriate() + " users!</td></tr>");
            //out.println("<tr><td colspan=\"2\">This message is flagged as having posted to wrong category by "+ news.getWrongTopic() + " users!</td></tr>");
        out.println("<tr><td align=\"center\">İçerik:</td><td> <textarea disabled name='body' rows=\"7\" cols=\"33\"> "+K.getDescription()+"</textarea></td></tr>");

        /*
        if(isRegistered)
            {
                int type = Integer.parseInt(session.getAttribute("type").toString());
                if(type ==2 && !session.getAttribute("userName").toString().equals(news.getSentBy())) {
                     out.println("<tr><td align=\"center\"><form name=\"flagforminapp\" action=\"UpdateFlag?type=1&id="+news.getId() +"\" method=\"POST\">");
                     out.println("Flag as: <input type=\"submit\" value=\"Inappropriate\"  />");
                     out.println("</form></td><td align=\"center\">");
                     out.println("<form name=\"flagformwrong\" action=\"UpdateFlag?type=2&id="+news.getId() +"\" method=\"POST\">");
                     out.println("Flag as: <input type=\"submit\" value=\"Wrong topic\" />");
                     out.println("</form></td></tr>");
                }
            }
*/
/*
            if(isRegistered){
                int type = Integer.parseInt(session.getAttribute("type").toString());
                if(type == 0 || type ==1 || (type==2 && session.getAttribute("userName").toString().equals(news.getSentBy()))) {
                        out.println("<tr><td align=\"center\"><a href=\"javascript: if(confirm('Are you sure you want to delete this message?')) { window.location.href='Delete?id="+news.getId()+"' } else { void('')}; \">Delete</a> ");
                        out.println("</td><td align=\"center\"><a href= \"Edit?id="+news.getId()+"\">Edit</a></td></tr>");}
                }
 *
 */
            out.println("</table>");
            out.println("<a href=\"ListNews\"><img id=\"home\" src=\"design/Pictures/home.png\"></a>");
            out.println("</body>");
            out.println("</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>

}
