from fpdf import FPDF

import caldav as cal
from datetime import datetime, timedelta, time, date

# Create your views here.

from mycaldav.models import *
from mycaldav.settings import *

RED = (220, 50, 50)
BLACK = (0, 0, 0)




class PDF(FPDF):
    o_caldav = None
    caldavClient = None
    start = None
    end = None
    def init_date(self,date):
        self.today = date.date()  # dt = datetime.strptime(day, '%d/%m/%Y')
        self.start = self.today - timedelta(days=self.today.weekday())
        self.end = self.start + timedelta(days=6)

    def lines(self):
        self.rect(5.0, 5.0, 200.0,287.0)
        self.rect(8.0, 8.0, 194.0,282.0)

    def header(self):
        self.set_xy(0, 10)
        #self.image('logo_clerc_200.png', 10, 6, 200)
        self.set_font('Arial', 'B', 8)
        self.set_text_color(*BLACK)
        self.cell(w=0, align='R', txt="Ambulance Clerc SA", border=0)
        self.set_xy(0, 10)
        self.set_font('Arial', 'B', 16)
        self.set_text_color(*RED)
        self.cell(w=0,h=5, align='C', txt="Planning hebdomadaire", border=0, ln=1)
        self.set_font('Arial', 'B', 12)
        self.set_text_color(*BLACK)
        self.cell(w=0,h=5, align='C', txt=f"du {self.start.strftime('%d.%m.%Y')} au {self.end.strftime('%d.%m.%Y')}", border=0)
        self.ln()
        tmp_x = self.get_x()
        tmp_y = self.get_y()
        self.line(tmp_x,tmp_y,tmp_x + 210,tmp_y)
    def draw_week(self):
        self.set_font('Arial', '', 10)

        self.caldavClient.get_events_by_date(self.caldavClient.a_team, self.start)
        

        self.ln()
        days_fr = ["Lundi","Mardi","Mercredi","Jeudi", "Vendredi", "Samedi", "Dimanche"]
        self.o_caldav.get_caldav_data(periode=7, date=self.start)
        for d in range(7):
            tmp_x = self.get_x()
            tmp_y = self.get_y()
            max_y = tmp_y
            dt = self.start + timedelta(days=d)

            #self.o_caldav.get_caldav_data(periode=1, date=dt)

            #self.o_caldav.sort_array_by_key(reverse=True)
            self.cell(w=50, h=5, align="L", txt=f"{days_fr[d]} {dt.strftime('%d.%m.%Y')}", ln=0)
            self.cell(w=60, h=5, align="L", txt=f"Jour", ln=1, border="B")
            for item in self.o_caldav.day:
                if datetime.strptime(item.dtstart,"%d.%m.%Y %H:%M").date() != dt:
                    continue
                self.set_fill_color(255,143,0)
                if item.team_transfert:
                    item.key += " Transfert"
                self.cell(w=50, h=5, align="R", txt=f"{item.key}", ln=0, fill=item.team_transfert)
                self.set_fill_color(255, 241, 118)
                self.cell(w=30, h=5, align="L", txt=f"{item.team_1}", ln=0, fill=item.team_1_chef)
                self.cell(w=30, h=5, align="L", txt=f"{item.team_2}", ln=0, fill=item.team_2_chef)
                self.ln()
                max_y = self.get_y()
            self.set_xy(tmp_x, tmp_y)
            self.cell(w=115, h=5, align="L", txt=f"", ln=0)
            self.cell(w=75, h=5, align="L", txt=f"Nuit", ln=1, border="B")
            for item in self.o_caldav.night:
                if datetime.strptime(item.dtstart, "%d.%m.%Y %H:%M").date() != dt:
                    continue

                self.set_fill_color(255,143,0)
                if item.team_transfert:
                    item.key += " Transfert"
                self.cell(w=115, h=5, align="L", txt=f"", ln=0)
                self.cell(w=15, h=5, align="R", txt=f"{item.key}", ln=0, fill=item.team_transfert)
                self.set_fill_color(255, 241, 118)
                self.cell(w=30, h=5, align="L", txt=f"{item.team_1}", ln=0, fill=item.team_1_chef)
                self.cell(w=30, h=5, align="L", txt=f"{item.team_2}", ln=0, fill=item.team_2_chef)
                self.ln()
                tmp_y = self.get_y()
                if tmp_y > max_y:
                    max_y = tmp_y
            self.set_xy(tmp_x,max_y)
            self.ln()







def render_pdf(caldav_object,caldavClient, date=None):
    pdf = PDF(orientation='P', unit='mm', format='A4')
    pdf.o_caldav = caldav_object
    pdf.caldavClient = caldavClient
    pdf.init_date(date)
    pdf.set_margins(4,2,4)
    pdf.add_page()
    pdf.set_font('Arial', 'B', 12)
    pdf.draw_week()


    return pdf.output('mycaldav/export.pdf', 'F')