package projet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/Export")
public class Export extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Export() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
		
		String idStr = request.getParameter("id");
		String type = request.getParameter("type");
		
		if(u != null && idStr != null && type != null) {
			u.chargerPages();
			u.chargerPagesPartagees();
			ArrayList<Page> listePages = u.getListePages();
			listePages.addAll(u.getListePagesPartagees());
			Page pageToExport;
			try {
				int id = Integer.parseInt(idStr);
				Boolean estDans = false;
			    for (Page page : listePages) {
			        if (page.getId() == id) {
			        	estDans = true;
			        	break;
			        }
			    }					
				
	            if (!estDans) {
	                response.sendError(HttpServletResponse.SC_NOT_FOUND, "Page introuvable.");
	                return;
	            }
				
	            pageToExport = Page.getPageById(u.getId(), id);
	            BlocRenderer blocRenderer;
	            PageRender pageRender;
	            String filename;
	            switch (type.toLowerCase()) {
		            case "html":
		            	blocRenderer = new HtmlBlocRenderer();
		            	pageRender = new HtmlPageRender();
		            	response.setContentType("text/html");
		            	filename = pageToExport.getTitre() + ".html";
		            	break;
	                case "latex":
	                	blocRenderer = new LatexBlocRenderer();
	                	pageRender = new LatexPageRender();
	                    response.setContentType("application/x-latex");
	                    filename = pageToExport.getTitre() + ".tex";
	                    break;
	                case "markdown":
	                	blocRenderer = new MarkdownBlocRenderer();
	                	pageRender = new MarkdownPageRender();
	                    response.setContentType("text/markdown");
	                    filename = pageToExport.getTitre() + ".md";
	                    break;
	                default:
	                    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Type de format non pris en charge.");
	                    return;
	            }
	            
	            String exportContent = pageRender.render(blocRenderer, pageToExport.getListeBlocs(), pageToExport.getTitre());
	            response.setCharacterEncoding("UTF-8");
	            response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
	            response.getWriter().write(exportContent);
	            return;
					                
			} catch (NumberFormatException e) {
	            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "ID invalide");
	            return;
	        }
		} else {
			response.sendRedirect("/Projet/");
		}
	}

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

}
