import PDFKit from 'pdfkit';
import { Invoice } from '../../types/invoice.types';

export class FooterSection {
  private doc: PDFKit.PDFDocument;
  private currentY: number;
  private pageHeight: number = 842; // A4 Höhe in Punkten
  private footerY: number = 720; // Höher positioniert: 842 - 122 (Footer-Höhe)

  constructor(doc: PDFKit.PDFDocument, currentY: number) {
    this.doc = doc;
    this.currentY = currentY;
  }

  /**
   * Wiederkehrender Footer
   * DIN 5008: Mindestens 15mm vom unteren Rand
   * Fixe Position am unteren Seitenrand
   */
  public draw(invoice: Invoice): number {
    // Zeichne Footer auf jeder Seite
    this.drawFooterOnAllPages(invoice);
    
    // Aktualisiere currentY für den nächsten Bereich
    return this.currentY;
  }

  private drawFooterOnAllPages(invoice: Invoice): void {
    // Hole alle Seiten des Dokuments
    const pageRange = this.doc.bufferedPageRange();
    
    for (let pageIndex = pageRange.start; pageIndex < pageRange.start + pageRange.count; pageIndex++) {
      // Wechsle zur entsprechenden Seite
      this.doc.switchToPage(pageIndex);
      
      // Zeichne Footer auf dieser Seite
      this.drawFooterOnPage(invoice);
    }
  }

  private drawFooterOnPage(invoice: Invoice): void {
    // Keine Trennlinie vor Footer
    
    const footerStartY = this.footerY + 20;
    
    // Vier-Spalten-Footer mit gleichmäßigen Spalten
    const totalWidth = 500; // 550 - 50 (Rand)
    const colWidth = totalWidth / 4; // 125px pro Spalte
    const colSpacing = 0; // Kein zusätzlicher Abstand zwischen Spalten
    
    // Spalte 1 - Firmeninfo
    this.doc.font('Helvetica-Bold')
      .fontSize(7) // Kleinere Schrift
      .text(invoice.sender.company || invoice.sender.name, 50, footerStartY, { continued: false })
      .font('Helvetica')
      .text(invoice.sender.name, 50, footerStartY + 12, { continued: false })
      .text(invoice.sender.address.street, 50, footerStartY + 24, { continued: false })
      .text(`${invoice.sender.address.postalCode} ${invoice.sender.address.city}`, 50, footerStartY + 36, { continued: false });
    
    // Spalte 2 - Kontaktdaten
    this.doc.fontSize(7) // Kleinere Schrift
      .text(`Tel.: ${invoice.sender.contactInfo.phone}`, 50 + colWidth, footerStartY, { continued: false })
      .text(`Email: ${invoice.sender.contactInfo.email}`, 50 + colWidth, footerStartY + 12, { continued: false })
      .text(`Web: ${invoice.sender.contactInfo.website}`, 50 + colWidth, footerStartY + 24, { continued: false });
    
    // Spalte 3 - Geschäftsinhaber und USt-IdNr.
    this.doc.fontSize(7) // Kleinere Schrift
      .text('Geschäftsinhaber:', 50 + 2 * colWidth, footerStartY, { continued: false })
      .text(invoice.sender.businessOwner || '', 50 + 2 * colWidth, footerStartY + 12, { continued: false })
      .text('USt-IdNr.:', 50 + 2 * colWidth, footerStartY + 24, { continued: false })
      .text(invoice.sender.vatId || '', 50 + 2 * colWidth, footerStartY + 36, { continued: false });
    
    // Spalte 4 - Bankdaten
    this.doc.fontSize(7) // Kleinere Schrift
      .text(`Bank: ${invoice.paymentInfo.bank}`, 50 + 3 * colWidth, footerStartY, { continued: false })
      .text(`Kontoinhaber: ${invoice.paymentInfo.accountHolder}`, 50 + 3 * colWidth, footerStartY + 12, { continued: false })
      .text(`IBAN: ${invoice.paymentInfo.iban}`, 50 + 3 * colWidth, footerStartY + 24, { continued: false })
      .text(`BIC/SWIFT-Code: ${invoice.paymentInfo.bic}`, 50 + 3 * colWidth, footerStartY + 36, { continued: false });
  }
} 