from datetime import datetime
from django.conf import settings


def generate_tex(title, date, render_date, start_slide_image, slides, id_to_name):
    log_start = "% !TEX program = xelatex\n\\documentclass[aspectratio=169]{beamer}\n\\usepackage{graphicx}\n\\usepackage{tikz}\n\\usepackage[absolute,overlay]{textpos}\n\\definecolor{gray1}{rgb}{0.85,0.85,0.85}\n\\definecolor{gray2}{rgb}{0.95,0.95,0.95}\n\\setbeamertemplate{background canvas}{%\n\\begin{tikzpicture}[remember picture,overlay]\n\\begin{scope}[xshift=0.55\\textwidth, yshift=-4.5cm]\n\\fill[even odd rule,inner color=gray2,outer color=gray1] (0,0) circle (10);\n\\node[inner sep=0pt] at (0.26,-4.02) {\\includegraphics[width=1.16\\textwidth]{wood_floor}};\n\\end{scope}\n\\end{tikzpicture}%\n}\n\\setbeamertemplate{navigation symbols}{}\n\\vfuzz=1000pt\n\\hfuzz=1000pt\n\\usepackage{fontspec}\n\\newfontfamily{\\gill} [ Path = "+str(settings.BASE_DIR)+"/static/photolog_assets/,UprightFont = * Medium,BoldFont = * Bold] {Gill Sans MT}\n\\definecolor{title_line_red}{rgb}{0.8,0.2,0.2}\n\\makeatletter\n\\let\\old@rule\\@rule\n\\def\\@rule[#1]#2#3{\\textcolor{title_line_red}{\\old@rule[#1]{#2}{#3}}}\n\\makeatother\n\\title{Fotoprotokoll}\n\\author{Antje Zierle-Kohlmorgen}\n\\institute{Zierle-Training}\n\\date{2022}\n\\begin{document}\n\\gill"
    log_end = "\\begin{frame}\n\\begin{columns}\n\\begin{column}{0.6\\textwidth}\n\\begin{center}\n\\includegraphics[width=0.8\\textwidth]{smile}\n\\end{center}\n\\end{column}\n\\begin{column}{0.4\\textwidth}\n\\Huge{Viel Erfolg!}\n\\end{column}\n\\end{columns}\n\\end{frame}\n\\end{document}"

    title_slide_start = "\\begin{frame}\n\\begin{textblock*}{13.0cm}(3cm,1.75cm)\n\\huge{"
    title_slide_end_w_photo = "}\\\\[-0.25cm]\n\\rule{14cm}{0.05cm}\n\\begin{tikzpicture}[remember picture, overlay]\n\\node[xshift=0.8\\textwidth,yshift=-0.5cm]{\\includegraphics[height=4.5cm]{%s}};\n\\end{tikzpicture}\n\\end{textblock*}\n\\end{frame}"
    title_slide_end_wo_photo = "}\\\\[-0.25cm]\n\\rule{14cm}{0.05cm}\n\n\\end{textblock*}\n\\end{frame}"

    slide_start = "\\begin{frame}\n\\begin{columns}"
    slide_end = "\\end{columns}\n\\hfill\\\\[1.5cm]\n\\end{frame}"

    photo_start = "\\begin{column}{%.3f\\textwidth}\n\\begin{center}\n\\includegraphics[height=6.5cm]{"
    photo_end = "}\\end{center}\n\\end{column}"

    assets_path = "{%s}" % (str(settings.BASE_DIR) + "/static/photolog_assets/")
    cropped_path = "{%s}" % (str(settings.BASE_DIR) + "/static/cropped_images/")
    original_path = "{%s}" % (str(settings.BASE_DIR) + "/static/original_images/")
    include_paths = "\\graphicspath{"+assets_path+","+cropped_path+","+original_path+"}\n"

    tex_file_content = log_start
    tex_file_content += include_paths

    tex_file_content += title_slide_start + title
    if date and render_date:
        date = datetime.strptime(date, '%Y-%m-%d')
        tex_file_content += "\\\\" + date.strftime("%d.%m.%Y")

    if start_slide_image:
        tex_file_content += title_slide_end_w_photo % (id_to_name[start_slide_image])
    else:
        tex_file_content += title_slide_end_wo_photo

    for slide in slides:
        tex_file_content += slide_start
        num_none_photos = sum([1 for p in slide if p==None])

        for photo_id in slide:
            if not photo_id:
                continue
            if num_none_photos == 2: # slides are saved as [2, None, None] for only 1 image
                tex_file_content += photo_start % (3.0/len(slide))
            else:
                tex_file_content += photo_start % (1.0/len(slide))
            tex_file_content += id_to_name[photo_id]
            tex_file_content += photo_end
        tex_file_content += slide_end

    tex_file_content += log_end

    return tex_file_content
