package com.alexanderlogistics;

import java.util.Calendar;
import java.util.Date;
import java.util.logging.Logger;

/**
 * Speichert Payment statistik
 */
public class DebitorStatistikData {
    private static Logger logger = Logger.getLogger(DebitorStatistikData.class.getName());

    int month;
    int year;

    long count = 0;
    long averageDueDays; // Zahlungsziel
    long averagePaymentDays; // Tatsächliche Zahlung
    long totalDueDays;
    long totalPaymentDays;

    public DebitorStatistikData(Date date) {
        // Create a Calendar instance and set the date
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        // Get the year from the Calendar object
        this.year = calendar.get(Calendar.YEAR);
        // Get the month from the Calendar object
        this.month = calendar.get(Calendar.MONTH);
        // Increment the month by 1 since Calendar months are zero-based
        this.month++;

    }

    public DebitorStatistikData(int year, int month) {
        this.month = month;
        this.year = year;
    }

    /**
     * Aktualisiert die statistischen werte
     * 
     * @param invoiceDate
     * @param paymentDate
     */
    public void compute(Date invoiceDate, Date invoiceDueDate, Date paymentDate) {
        // und was nun?
        count++;

        logger.fine("...invoiceDate= " + invoiceDate);
        logger.fine("...invoiceDueDate= " + invoiceDueDate);
        logger.fine("...paymentDate= " + paymentDate);

        // Fälligkeit in Tagen
        long milliseconds1 = invoiceDate.getTime();
        long milliseconds2 = invoiceDueDate.getTime();
        // Berechnen Sie die Differenz in Millisekunden
        long diffMilliseconds = milliseconds2 - milliseconds1;
        long dueTime = diffMilliseconds / (24 * 60 * 60 * 1000);
        logger.fine("...Fälligkeit in tagen= " + dueTime);

        totalDueDays = totalDueDays + dueTime;
        // averageDueDays = (long) (totalDueDays / count);
        averageDueDays = Math.round((double) totalDueDays / count);

        // Bezahlt nach Tagen
        milliseconds1 = invoiceDate.getTime();
        milliseconds2 = paymentDate.getTime();
        // Berechnen Sie die Differenz in Millisekunden
        diffMilliseconds = milliseconds2 - milliseconds1;
        long paymentTime = diffMilliseconds / (24 * 60 * 60 * 1000);

        // Fix differenz
        // paymentTime = paymentTime - dueTime;
        logger.fine("...Zahlung erfolgte nach tagen= " + paymentTime);
        totalPaymentDays = totalPaymentDays + paymentTime;
        // averagePaymentDays = (long) (totalPaymentDays / count);
        averagePaymentDays = Math.round((double) totalPaymentDays / count);

    }

    public long getAverageDueDays() {
        return averageDueDays;
    }

    public long getAveragePaymentDays() {
        return averagePaymentDays;
    }

    /**
     * returns
     * 
     * 202304
     */
    public String toString() {
        return "" + year + (month < 10 ? "0" + month : "" + month);
    }

}
