package web;

import ejb.Users;
import ejb.UsersFacade;
import java.io.IOException;
import java.io.PrintWriter;
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;
import javax.servlet.http.HttpSession;

/**
 *
 * @author sinem
 */
@WebServlet(name="SuperUserPage", urlPatterns={"/SuperUserPage"})
public class SuperUserPage extends HttpServlet {
    @EJB
    private UsersFacade usersFacade;


    
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        List<Users> allUsers = usersFacade.findAll();
        HttpSession session = request.getSession(true);
        String userName = session.getAttribute("userName").toString();
        int type = Integer.parseInt(session.getAttribute("type").toString());

        if(type != 0) {
            response.sendRedirect("ListNews");
            return;
        }

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Super User Page</title>");
             out.println("<link href=\"design/css/superuserpage.css\" type=\"text/css\" rel=\"stylesheet\" />");
            out.println("</head>");
            out.println("<body>");

            out.println("<font id=\"header\">Edit User Types</font>");
            out.println("<form action=\"UpdateUserTypes\" method=\"POST\">");
            
            out.println("<table id=\"general\">");
            out.println("<tr><th>ID</th>");
            out.println("<th>USERNAME</th>");
            out.println("<th>TYPE</th>");
            out.print("</tr>");

            for(int i = 0; i<allUsers.size(); i++) {
                Users u = allUsers.get(i);
                if(!u.getUserName().equals(userName)){
                out.println("<tr>");
                out.print("<td>"+u.getId()+"</td>");
                out.print("<td align=\"center\">"+u.getUserName()+"</td>");
                out.println("<td><p><select name=\"type"+u.getId()+"\">");
                if(u.getType()==1)
                {
                    out.println("<option selected>1</option>");
                }
                else {
                    out.println("<option>1</option>");
                }
                if(u.getType()==2)
                {
                    out.println("<option selected>2</option>");
                }
                else {
                    out.println("<option>2</option>");
                }
                out.println("</select></p></td>");
                out.print("<td><input size=\"10\" type=\"hidden\" name=\""+u.getId()+"\" value = \""+u.getId()+"\"/></td>");
                out.println("</tr>");}

            }
            out.println("<tr><td align=\"center\" colspan=\"2\"><input type=\"submit\" value=\"OK\" name=\"okButton\" /></td></tr>");
            out.println("</table>");
            
           
            out.println("</form>");
            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>

}
