import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';

class PDFGenerator {
    constructor(margin = 20, columnGap = 10) {
        this.pdf = new jsPDF('p', 'mm', 'a4');
        this.margin = margin;
        this.columnGap = columnGap;
        this.pageHeight = 297 - margin * 2; 
        this.columnWidth = (210 - margin * 2 - columnGap) / 2;
        this.position = margin;
        this.yPos = this.position;
        this.xPos = margin;
        this.isLeftColumn = true;
        this.COLUMN_START_TIME = margin;
        this.COLUMN_ACTIVITY = margin + 30;
        this.COLUMN_GROUP = margin + 100;
        this.COLUMN_DURATION = margin + 150;
        this.LINE_HEIGHT = 7;
    }

    async addSchedule(element) {
        const canvas = await html2canvas(element, { scale: 2 });
        const imgData = canvas.toDataURL('image/png');
        const imgWidth = 210 - this.margin * 2;
        const imgHeight = (canvas.height * imgWidth) / canvas.width;
        let heightLeft = imgHeight;
        let position = this.margin;
        this.pdf.addImage(imgData, 'PNG', this.margin, position, imgWidth, imgHeight);
        heightLeft -= this.pageHeight;
        while (heightLeft >= 0) {
            position = heightLeft - imgHeight + this.margin;
            this.pdf.addPage();
            this.pdf.addImage(imgData, 'PNG', this.margin, position, imgWidth, imgHeight);
            heightLeft -= this.pageHeight;
        }
    }

    addNewPage() {
        this.pdf.addPage();
        this.xPos = this.margin;
        this.yPos = this.position;
        this.isLeftColumn = true;
    }

    addHeader(clubName, formattedDate, formattedStartTime, formattedEndTime) {
        this.pdf.setFontSize(14);
        this.pdf.setFont('helvetica', 'bold');
        this.pdf.text(`${clubName} - Trainingsplan`, this.margin, this.yPos);
        this.yPos += 10;
        this.pdf.setFontSize(12);
        this.pdf.setFont('helvetica', 'normal');
        this.pdf.text(`Datum: ${formattedDate}`, this.margin, this.yPos);
        this.yPos += 7;
        this.pdf.text(`Uhrzeit: ${formattedStartTime} - ${formattedEndTime}`, this.margin, this.yPos);
        this.yPos += 10;
    }

    addTableHeaders() {
        this.pdf.setFont('helvetica', 'bold');
        this.pdf.text('Startzeit', this.COLUMN_START_TIME, this.yPos);
        this.pdf.text('Aktivität / Zeitblock', this.COLUMN_ACTIVITY, this.yPos);
        this.pdf.text('Gruppe', this.COLUMN_GROUP, this.yPos);
        this.pdf.text('Dauer (Min)', this.COLUMN_DURATION, this.yPos);
        this.yPos += this.LINE_HEIGHT;
        this.pdf.setFont('helvetica', 'normal');
    }

    addTrainingPlan(clubName, trainingDate, trainingStart, trainingEnd, trainingPlan) {
        const formattedDate = new Date(trainingDate).toLocaleDateString('de-DE');
        const formattedStartTime = trainingStart ? trainingStart.slice(0, 5) : '';
        const formattedEndTime = trainingEnd ? trainingEnd.slice(0, 5) : '';
        this.addHeader(clubName, formattedDate, formattedStartTime, formattedEndTime);
        this.addTableHeaders();
        for (const item of trainingPlan) {
            if (item.isTimeblock) {
                this.addTimeBlock(item);
            } else {
                this.addActivity(item);
            }
            this.checkPageOverflow();
        }
    }

    addTimeBlock(item) {
        this.pdf.setFont('helvetica');
        this.pdf.text(item.startTime, this.COLUMN_START_TIME, this.yPos);
        this.pdf.text('Zeitblock', this.COLUMN_ACTIVITY, this.yPos);
        this.yPos += this.LINE_HEIGHT;
        this.pdf.setFont('helvetica', 'normal');
        if (item.groupActivities && item.groupActivities.length > 0) {
            this.addGroupActivities(item.groupActivities);
        }
    }

    addActivity(item) {
        const startTime = item.startTime;
        const activityName = item.predefinedActivity
            ? item.predefinedActivity.name
            : item.activity;
        const groupName = item.groupActivity ? item.groupActivity.name : '';
        const duration = item.duration ? item.duration.toString() : '';
        let text = '';
        if (item.durationText) {
            text = item.durationText + ' / ';
        }
        text += duration;
        this.pdf.text(startTime, this.COLUMN_START_TIME, this.yPos);
        this.pdf.text(activityName, this.COLUMN_ACTIVITY, this.yPos);
        this.pdf.text(groupName, this.COLUMN_GROUP, this.yPos);
        this.pdf.text(text, this.COLUMN_DURATION, this.yPos);
        this.yPos += this.LINE_HEIGHT;
        if (item.groupActivities && item.groupActivities.length > 0) {
            this.addGroupActivities(item.groupActivities);
        }
    }

    addGroupActivities(groupActivities) {
        for (const groupItem of groupActivities) {
            const activityName = groupItem.groupPredefinedActivity
                ? groupItem.groupPredefinedActivity.name
                : '';
            const groupName = groupItem.groupsGroupActivity
                ? groupItem.groupsGroupActivity.name
                : '';
            this.pdf.text('', this.COLUMN_START_TIME, this.yPos); // Leere Startzeit
            this.pdf.text(' - ' + activityName, this.COLUMN_ACTIVITY + 5, this.yPos); // Einrückung
            this.pdf.text(groupName, this.COLUMN_GROUP, this.yPos);
            this.pdf.text('', this.COLUMN_DURATION, this.yPos); // Leere Dauer
            this.yPos += this.LINE_HEIGHT;
            this.checkPageOverflow();
        }
    }

    checkPageOverflow() {
        if (this.yPos > this.pageHeight) {
            this.addNewPage();
        }
    }

    save(filename) {
        this.pdf.save(filename);
    }

    addPhoneList(members) {
        this.pdf.setFontSize(14);
        this.pdf.setFont('helvetica', 'bold');
        this.pdf.text('Telefonliste - Aktive Mitglieder', this.margin, this.yPos);
        this.yPos += 10;
        this.addPhoneListHeaders();

        this.pdf.setFont('helvetica', 'normal');
        for (const member of members) {
            this.addPhoneListRow(member);
            this.checkPageOverflow();
        }
    }

    addPhoneListHeaders() {
        this.pdf.setFont('helvetica', 'bold');
        this.pdf.text('Name, Vorname', this.margin, this.yPos);
        this.pdf.text('Geburtsdatum', this.margin + 60, this.yPos);
        this.pdf.text('Telefon-Nr.', this.margin + 120, this.yPos);
        this.yPos += this.LINE_HEIGHT;
        this.pdf.setFont('helvetica', 'normal');
    }

    addPhoneListRow(member) {
        const fullName = `${member.lastName}, ${member.firstName}`;
        const birthDate = member.birthDate ? new Date(member.birthDate).toLocaleDateString('de-DE') : '';
        const phoneNumber = member.phone || '';

        this.pdf.text(fullName, this.margin, this.yPos);
        this.pdf.text(birthDate, this.margin + 60, this.yPos);
        this.pdf.text(phoneNumber, this.margin + 120, this.yPos);
        this.yPos += this.LINE_HEIGHT;
    }
}

export default PDFGenerator;
