from docx import Document
from pprint import pprint
import json
import re
import html


class export_to_pdf:
    def keep_only_paragraph_tags(input_text):
        # Remplacer les caractères HTML par leurs équivalents
        decoded_text = html.unescape(input_text)
        # Supprimer toutes les balises HTML et ajouter des retours à la ligne pour les balises fermantes
        cleaned_text = re.sub(r'</[^>]*>', '\n', decoded_text)
        cleaned_text = re.sub(r'<[^>]*>', '', cleaned_text)
        return cleaned_text
    
    # Function pour générer un word en fonction de valeurs
    def generate_word(
        actions,
        expertise_area,
        technical_data,
        activity_area,
        informations,
        administrative,
    ):
        document = Document("/var/www/dossier_candidat/app/template.docx")
        document.add_table(rows=1, cols=3)
        table = document.tables[0]
        hdr_cells = table.rows[0].cells
        hdr_cells[0].text = "Nom"
        hdr_cells[1].text = "Prénom"
        row_cells = table.add_row().cells
        row_cells[0].text = informations["Nom"]
        row_cells[1].text = informations["Prénom"]
        document.add_heading("Informations générales", level=1)
        document.add_paragraph(
            "Motivations pour changer de poste : "
            + actions["Motivation pour changer de poste"]
        )
        document.add_paragraph("Disponibilité : " + informations["Disponibilité"])
        document.add_paragraph(
            "Mobilité : "
            + informations["Mobilité"]
            + "     Agence : "
            + actions["Agence"]
        )
        if (
            actions["Statut actuel"] == "Salarie"
            if "Statut actuel" in actions
            else False
        ):
            document.add_paragraph(
                "Statut actuel : "
                + actions["Statut actuel"]
                + " - "
                + actions["Statut actuel_detail"]
            )
        else:
            document.add_paragraph(
                "Statut actuel : " + actions["Statut actuel"]
            ) if "Statut actuel" in actions else None
        if (
            "Detail statut actuel" in administrative
            and "salaire_brut_actuel" in administrative
        ):
            document.add_paragraph(
                "Salaire actuel : "
                + administrative["salaire_brut_actuel"]
                + "€     Détail du salaire : "
                + administrative["Detail salaire actuel"]
            )
        elif "salaire_brut_actuel" in administrative:
            document.add_paragraph(
                "Salaire actuel : " + administrative["salaire_brut_actuel"] + "€"
            )
        if informations["Statut souhaité"] == "Freelance":
            document.add_paragraph("TJM souhaité : " + administrative["TJM_max"])
        else:
            document.add_paragraph(
                "Prétentions salariales : "
                + administrative["salaire_souhaite_min"]
                + "€ - "
                + administrative["salaire_souhaite_max"]
                + "€"
            )
        document.add_paragraph(
            "Nationalité : "
            + administrative["Nationalité"]
            + "     Permis de travail : "
            + administrative["Permis de travail"]
        )
        document.add_paragraph(
            "Permis de conduire : "
            + actions["Permis de conduire"]
            + "     Véhicule : "
            + actions["Véhicule"]
        ) if ("Permis de conduire" in actions) else None

        document.add_heading("Recherche d'emploi", level=1)
        document.add_paragraph("Recherche  : " + actions["Recherche"]) if (
            "Recherche" in actions
        ) else None
        document.add_paragraph(
            "Avancement de la recherche : " + actions["Avancement recherche"]
        ) if ("Avancement recherche" in actions) else None
        document.add_paragraph(
            "Postes recherchés : " + actions["Postes recherches"]
        ) if ("Postes recherches" in actions) else None
        document.add_paragraph(
            "Secteurs d'activités souhaités : "
            + actions["Secteurs activites souhaites"]
        ) if ("Secteurs activites souhaites" in actions) else None
        document.add_heading("Compétences Linguistiques", level=1)
        document.add_paragraph(
            "Langue 1 : "
            + technical_data["Langue 1"]
            + "     Niveau : "
            + technical_data["Niveau Langue 1"]
        )
        document.add_paragraph(
            "Langue 2 : "
            + technical_data["Langue 2"]
            + "     Niveau : "
            + technical_data["Niveau Langue 2"]
        )
        document.add_paragraph(
            "Commentaires : " + actions["Commentaire sur les langues"]
        )
        try :
            document.add_heading("REFERENCES PROFESSIONNELLES", level=1)
            document.add_table(rows=5, cols=2)
            table = document.tables[1]
            contact_1_cell = table.cell(0, 0)
            contact_1_cell.text = "Contact : " + actions["Reference contact 1"]
            contact_2_cell = table.cell(0, 1)
            contact_2_cell.text = "Contact : " + actions["Reference contact 2"]
            contact_1_cell = table.cell(1, 0)
            contact_1_cell.text = "Société : " + actions["Reference Societe 1"]
            contact_2_cell = table.cell(1, 1)
            contact_2_cell.text = "Société : " + actions["Reference Societe 2"]
            contact_1_cell = table.cell(2, 0)
            contact_1_cell.text = "Fonction : " + actions["Reference fonction 1"]
            contact_2_cell = table.cell(2, 1)
            contact_2_cell.text = "Fonction : " + actions["Reference fonction 2"]
            contact_1_cell = table.cell(3, 0)
            contact_1_cell.text = "Téléphone : " + actions["Reference telephone 1"]
            contact_2_cell = table.cell(3, 1)
            contact_2_cell.text = "Téléphone : " + actions["Reference telephone 2"]
            contact_1_cell = table.cell(4, 0)
            contact_1_cell.text = "@ : " + actions["Reference email 1"]
            contact_2_cell = table.cell(4, 1)
            contact_2_cell.text = "@ : " + actions["Reference email 2"]
        except:
            pass
        document.add_page_break()
        document.add_heading("Prise de notes", level=1)
        #remove the html tags from actions["Prise de notes"]
        actions["Prise de notes"] = export_to_pdf.keep_only_paragraph_tags(actions["Prise de notes"])
        document.add_paragraph(actions["Prise de notes"])
        document.add_page_break()

        document.add_table(rows=8, cols=2)
        table = document.tables[2]
        cell_1_1 = table.cell(0, 0)
        cell_1_1.text = "Présentation"
        cell_1_2 = table.cell(0, 1)
        cell_1_2.text = informations["0"] if ("0" in informations) else ""
        cell_2_1 = table.cell(1, 0)
        cell_2_1.text = "Elocution"
        cell_2_2 = table.cell(1, 1)
        cell_2_2.text = informations["1"] if ("1" in informations) else ""
        cell_3_1 = table.cell(2, 0)
        cell_3_1.text = "Esprit de synthèse"
        cell_3_2 = table.cell(2, 1)
        cell_3_2.text = informations["2"] if ("2" in informations) else ""
        cell_4_1 = table.cell(3, 0)
        cell_4_1.text = "Communication/Relationnel"
        cell_4_2 = table.cell(3, 1)
        cell_4_2.text = informations["3"] if ("3" in informations) else ""
        cell_5_1 = table.cell(4, 0)
        cell_5_1.text = "Motivation pour le consulting"
        cell_5_2 = table.cell(4, 1)
        cell_5_2.text = informations["4"] if ("4" in informations) else ""
        cell_6_1 = table.cell(5, 0)
        cell_6_1.text = "Dynamisme"
        cell_6_2 = table.cell(5, 1)
        cell_6_2.text = informations["5"] if ("5" in informations) else ""
        cell_7_1 = table.cell(6, 0)
        cell_7_1.text = "Compétences techniques"
        cell_7_2 = table.cell(6, 1)
        cell_7_2.text = informations["6"] if ("6" in informations) else ""
        cell_8_1 = table.cell(7, 0)
        cell_8_1.text = "Esprit de service"
        cell_8_2 = table.cell(7, 1)
        cell_8_2.text = informations["7"] if ("7" in informations) else ""

        document.add_paragraph(
            "Leviers de motivation : " + informations["Levier de motivation"]
        )
        document.add_heading("Mots Clés Boond", level=1)
        if expertise_area != []:
            document.add_paragraph("Secteurs d'activités : " + expertise_area)
        document.add_paragraph("Métier(s) : " + technical_data["Metiers"])
        document.add_paragraph("Logiciel(s) / Outil(s) : " + technical_data["Outils"])
        document.add_paragraph(
            "Entreprise(s) : " + technical_data["Entreprise precedentes"]
        )
        if activity_area != []:
            document.add_paragraph("Domaines : " + activity_area)
        document.add_paragraph(
            "Commentaires suite à l'entretien : "
            + informations["Commentaires suite à l EC"]
        )
        document.add_paragraph("Décision : " + informations["Décision"])
        document.add_paragraph(
            "Positionnements potentiels : " + informations["Positionnements potentiels"]
        ) if ("Postionnements potentiels" in informations) else None
        document.add_paragraph(
            "Disponibilités éventuelles pour RT : "
            + informations["Disponibilites eventuelles pour RT"]
        ) if ("Disponibilites eventuelles pour RT" in informations) else None
        nom_document = (
            "Dossier candidat_"
            + informations["Nom"]
            + " "
            + informations["Prénom"]
            + ".docx"
        )
        nom_document = nom_document.encode("ascii", "ignore")
        nom_document = nom_document.decode()
        print(nom_document)
        document.save('/var/www/dossier_candidat/app/word/'+nom_document)
        return True
