package pdf;

import java.io.IOException;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

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 com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;

import fonctions.IdeaDAO;
import fonctions.MeetingDAO;
import fonctions.MemberDAO;
import retro.Idea;
import retro.Meeting;
import retro.Member;



@WebServlet( "/pdfexport" )
public class PdfCreator extends HttpServlet 
{

    private static final long serialVersionUID = 7609134248482865644L;

    
    @Override
    protected void doGet( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException 
    {
        
        String idMeeting = "0";
        try 
        { 
        	idMeeting = request.getParameter( "idMeeting" );
        } catch (Exception exception ) 
        {
            // Nothing to do
        }
      
        
        String masterPath = request.getServletContext().getRealPath( "/WEB-INF/FacMaster.pdf" );
        response.setContentType( "application/pdf" );
        
        try ( PdfReader reader = new PdfReader( masterPath );
              PdfWriter writer = new PdfWriter( response.getOutputStream() );
              PdfDocument document = new PdfDocument( reader, writer ) ) 
        {        	
            NumberFormat formatter = NumberFormat.getNumberInstance( new Locale( "fr", "FR" ) );
            
            PdfPage page = document.getFirstPage();
            PdfCanvas canvas = new PdfCanvas( page );
            
            FontProgram fontProgram = FontProgramFactory.createFont();
            PdfFont font = PdfFontFactory.createFont( fontProgram, "utf-8", true );
            canvas.setFontAndSize(font, 7);
            canvas.beginText();    
            
            
            
            MeetingDAO obj_ReadValues1 = new MeetingDAO();
			List<Meeting> list1 = obj_ReadValues1.get_MeetingByIDmeeting(idMeeting); 
			Iterator<Meeting> it_list1 = list1.iterator();
			
			
			while (it_list1.hasNext()) 
			{
				Meeting obj_Meeting1 = new Meeting();
				obj_Meeting1 = it_list1.next();
	
				canvas.setTextMatrix(60, 760);
	            canvas.showText("Meeting-Title : " + obj_Meeting1.getLibelle_sprint());
			}


			MemberDAO obj_ReadValues2 = new MemberDAO();
			List<Member> list2 = obj_ReadValues2.get_MemberInMeeting(idMeeting); 
			Iterator<Member> it_list2 = list2.iterator();
				
			canvas.setTextMatrix(60, 750);
			canvas.showText("Members : ");			 
				
				
			int y=750;
			
			while (it_list2.hasNext()) 
			{
				Member obj_Meeting2 = new Member();
				obj_Meeting2 = it_list2.next();
				
				canvas.setTextMatrix(60, y=y-8);
		        canvas.showText(" - " + obj_Meeting2.getNom_member() + " " + obj_Meeting2.getPrenom_member());
		    }      
							
			IdeaDAO obj_ReadValues3 = new IdeaDAO();
			List<Idea> list3 = obj_ReadValues3.get_IdeaLikedByIDmeeting(idMeeting); 
			Iterator<Idea> it_list3 = list3.iterator();
					
			canvas.setTextMatrix(60, y=y-12);
			canvas.showText("Ideas Liked : ");
			
			while (it_list3.hasNext()) 
			{
				Idea obj_Meeting3 = new Idea();
				obj_Meeting3 = it_list3.next();
				
				canvas.setTextMatrix(60, y=y-9);
			    canvas.showText(" - " + obj_Meeting3.getId_idea() + " | " + obj_Meeting3.getLibelle_idea()+ " | "  + obj_Meeting3.getDescription_idea() );
			}
							
            IdeaDAO obj_ReadValues4 = new IdeaDAO();
			List<Idea> list4 = obj_ReadValues4.get_IdeaLearnedByIDmeeting(idMeeting); 
			Iterator<Idea> it_list4 = list4.iterator();
				
			canvas.setTextMatrix(60, y=y-12);
			canvas.showText("Ideas Learned : ");
			
			while (it_list4.hasNext()) 
			{
				Idea obj_Meeting4 = new Idea();
				obj_Meeting4 = it_list4.next();
				
				canvas.setTextMatrix(60, y=y-9);
		        canvas.showText(" - " + obj_Meeting4.getId_idea() + " | "  + obj_Meeting4.getLibelle_idea()+ " | "  + obj_Meeting4.getDescription_idea() );
			}
						
            canvas.endText();
                        
            IdeaDAO obj_ReadValues5 = new IdeaDAO();
			List<Idea> list5 = obj_ReadValues5.get_IdeaLackedByIDmeeting(idMeeting); 
			Iterator<Idea> it_list5 = list5.iterator();
				
			canvas.setTextMatrix(60, y=y-12);
			canvas.showText("Ideas Lacked : ");
			
			while (it_list5.hasNext()) 
			{
				Idea obj_Meeting5 = new Idea();
				obj_Meeting5 = it_list5.next();
				
				canvas.setTextMatrix(60, y=y-9);
		        canvas.showText(" - " + obj_Meeting5.getId_idea() + " | "  + obj_Meeting5.getLibelle_idea()+ " | "  + obj_Meeting5.getDescription_idea() );
			}
						
            canvas.endText();
                        
            IdeaDAO obj_ReadValues6 = new IdeaDAO();
			List<Idea> list6 = obj_ReadValues6.get_IdeaLongedForByIDmeeting(idMeeting); 
			Iterator<Idea> it_list6 = list6.iterator();
			
			canvas.setTextMatrix(60, y=y-12);
			canvas.showText("Ideas Longed for : ");
			
			while (it_list6.hasNext()) 
			{
				Idea obj_Meeting6 = new Idea();
				obj_Meeting6 = it_list6.next();
				
				canvas.setTextMatrix(60, y=y-9);
		        canvas.showText(" - " + obj_Meeting6.getId_idea() + " | "  + obj_Meeting6.getLibelle_idea()+ " | "  + obj_Meeting6.getDescription_idea() );
			}
						
            canvas.endText();
                        
        }
        
    }
    
}