package edu.pdx.cs410J.chances;

import edu.pdx.cs410J.AbstractAppointmentBook;
import edu.pdx.cs410J.AppointmentBookDumper;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;

/**
 * @author chancesnow
 */
public class PrettyPrinter implements AppointmentBookDumper
{
    File file;

    /**
     * Construct a new pretty printer, printing to standard out
     */
    public PrettyPrinter()
    {
        this.file = null;
    }

    /**
     * Construct a new pretty printer given the file path to print to
     *
     * @param filePath The file path
     */
    public PrettyPrinter(String filePath)
    {
        file = new File(filePath);

        // Exceptional state when file is a directory
        if (file.isDirectory()) {
            file = null;
        }
    }

    /**
     * Dump (overwrite) an appointment book to a file in a pretty format
     *
     * @param appointmentBook The appointment book to dump (write)
     * @throws IOException
     */
    @Override
    public void dump(AbstractAppointmentBook appointmentBook) throws IOException
    {
        PrintWriter pw;

        if (file != null) {
            pw = new PrintWriter(file);
        } else {
            pw = new PrintWriter(System.out);
        }

        pw.println(appointmentBook.getOwnerName() + "'s Appointment Book\n");

        int count = 0;

        for (Appointment appointment :
                ((AppointmentBook) appointmentBook).getAppointments()) {
            if (count > 0) {
                pw.println("\n----------------------------------------------\n");
            }

            pw.println(appointment.getDescription() + "\n");

            pw.println("Beginning at: " +
                    Appointment.DATE_FORMAT.format(
                            appointment.getBeginTime()
                    )
            );
            pw.println("Ending at: " +
                    Appointment.DATE_FORMAT.format(
                            appointment.getEndTime()
                    ) + "\n"
            );

            // Calculate duration of appointment in minutes
            long difference = appointment.getEndTime().getTime() -
                    appointment.getBeginTime().getTime();
            long durationInMinutes =
                    TimeUnit.MILLISECONDS.toMinutes(difference);

            pw.println("Duration: " + durationInMinutes + " minutes");

            count += 1;
        }

        // If printing to a file, close it
        if (file != null) {
            pw.close();
        }
    }
}

@@ -2073,193 +2073,8 @@
 );%0A%0A
-            // Calculate duration of appointment in minutes%0A            long difference = appointment.getEndTime().getTime() -%0A                    appointment.getBeginTime().getTime();%0A
     
@@ -2162,22 +2162,36 @@
 tes(
-difference
+appointment.getDuration()
 );%0A
-%0A
     

Use Appointment's duration getter