/*
 * Copyright 2014 Jonny Heggheim
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jonnyware.timetracker.cli;

import com.jonnyware.timetracker.*;
import org.joda.time.Interval;
import org.joda.time.Period;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;

public class PrintCommand {
    public void run(Map<String, Object> parsed, TimeEntryParser parser) {
        Integer year = parser.getYear();
        System.out.printf("Year: %d%n", year);
        System.out.println("+------+---------+-----------+");
        System.out.println("| Week |  Total  |    Diff   |");
        System.out.println("+------+---------+-----------+");

        Collection<NormalDay> entries = parser.listTimeEntries();
        Collection<SpecialDay> vacations = new LinkedList<SpecialDay>();
        vacations.addAll(parser.listVacations());
        vacations.addAll(parser.neutralDays());
        DateGroupByWeek vacationByWeek = new DateGroupByWeek(vacations);

        IntervalGroupBy groupBy = new IntervalGroupBy(entries);
        Map<Integer, Collection<NormalDay>> weeks = groupBy.weekOfYear();

        DefaultWeekdayDurationParser defaultDurationParser = new DefaultWeekdayDurationParser(parsed);
        Map<Integer, Period> hoursPerWeekday = defaultDurationParser.getSpecifiedMergedWithDefault();

        IgnoredDays ignoredDays = new IgnoredDays(parser.listVacations(), parser.neutralDays());
        DiffCalculator calculator = new DiffCalculator(hoursPerWeekday, ignoredDays.union());
        ExtraInformation extraInformation = new ExtraInformation(year);

        Period totalSummed = Period.ZERO;
        Period totalDiff = Period.ZERO;
        for (int week = 1; week <= 53; week++) {
            Map<String, Integer> vacationForThisWeek = vacationByWeek.getComments(week);
            if (!weeks.containsKey(week) && vacationForThisWeek.isEmpty()) {
                continue;
            }
            Collection<NormalDay> values = Collections.EMPTY_LIST;
            if (weeks.containsKey(week)) {
                values = weeks.get(week);
            }

            Period totalPerWeek = Period.ZERO;
            Period diffPerWeek = Period.ZERO;
            for (NormalDay interval : values) {
                Period period = interval.getTotal();
                Period diff = calculator.diff(interval);

                diffPerWeek = diffPerWeek.plus(diff);
                totalDiff = totalDiff.plus(diff);

                totalPerWeek = totalPerWeek.plus(period);
                totalSummed = totalSummed.plus(period);
            }

            System.out.printf("|   %2d | %7s | %9s |", week, Formatter.print(totalPerWeek), Formatter.print(diffPerWeek));
            if (!vacationForThisWeek.isEmpty()) {
                System.out.printf(" %s", vacationForThisWeek);
            }

            int daysInWeek = extraInformation.daysInWeek(week);
            if( daysInWeek != 7) {
                System.out.printf(" %d day(s) in week", daysInWeek);
            }
            System.out.println();
        }
        System.out.println("+------+---------+-----------+");
        System.out.printf("|  Sum | %7s | %9s |%n", Formatter.print(totalSummed), Formatter.print(totalDiff));
        System.out.println("+------+---------+-----------+");
    }
}

@@ -977,24 +977,157 @@
 .getYear();%0A
+        Collection%3CVacation%3E vacations = parser.listVacations();%0A        Collection%3CNeutralDay%3E neutralDays = parser.neutralDays();%0A%0A
         Syst
@@ -1146,16 +1146,25 @@
 f(%22Year:
+         
  %25d%25n%22, 
@@ -1170,16 +1170,154 @@
  year);%0A
+        System.out.printf(%22Vacation days: %25d%25n%22, vacations.size());%0A        System.out.printf(%22Neutral days:  %25d%25n%22, neutralDays.size());%0A
         
@@ -1592,24 +1592,26 @@
 ialDay%3E 
-vacation
+specialDay
 s = new 
@@ -1648,45 +1648,36 @@
     
-vacations.addAll(parser.listV
+specialDays.addAll(v
 acations
 ());
@@ -1672,18 +1672,16 @@
 acations
-()
 );%0A     
@@ -1683,24 +1683,26 @@
         
-vacation
+specialDay
 s.addAll
@@ -1702,23 +1702,16 @@
 .addAll(
-parser.
 neutralD
@@ -1705,34 +1705,32 @@
 dAll(neutralDays
-()
 );%0A        DateG
@@ -1777,24 +1777,26 @@
 pByWeek(
-vacation
+specialDay
 s);%0A%0A   
@@ -2191,39 +2191,19 @@
 ays(
-parser.listVacations(), parser.
+vacations, 
 neut
@@ -2209,18 +2209,16 @@
 tralDays
-()
 );%0A     

Include the total number of neutral days and vacations.