/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2012 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.web.users.dashboard;

import static org.libreplan.web.I18nHelper._;

import org.joda.time.LocalDate;
import org.libreplan.business.common.entities.PersonalTimesheetsPeriodicityEnum;
import org.libreplan.business.workingday.EffortDuration;
import org.libreplan.business.workreports.entities.WorkReport;

/**
 * Simple class to represent the personal timesheets to be shown in the list.
 * <br />
 * This is only a utility class for the UI, everything will be saved using {@link WorkReport} class.
 *
 * @author Manuel Rego Casasnovas <mrego@igalia.com>
 */
public class PersonalTimesheetDTO {

    private static final String MMMM_Y_PATTERN = "MMMM y";

    private LocalDate date;

    private WorkReport workReport;

    private EffortDuration resourceCapacity;

    private EffortDuration totalHours;

    private int tasksNumber;

    /**
     * @param date
     *            The date of the timesheet.
     * @param workReport
     *            The work report of the personal timesheet, it could be
     *            <code>null</code> if it doesn't exist yet.
     * @param resourceCapacity
     *            The capacity of the resource bound to current user in the
     *            period of this timesheet.
     * @param totalHours
     *            Total hours worked by the resource bound to the current user
     *            in the personal timesheet
     * @param tasksNumber
     *            Number of tasks in the personal timesheet
     */
    public PersonalTimesheetDTO(LocalDate date, WorkReport workReport, EffortDuration resourceCapacity,
                                EffortDuration totalHours, int tasksNumber) {
        this.date = date;
        this.workReport = workReport;
        this.resourceCapacity = resourceCapacity;
        this.totalHours = totalHours;
        this.tasksNumber = tasksNumber;
    }

    public LocalDate getDate() {
        return date;
    }

    public WorkReport getWorkReport() {
        return workReport;
    }

    public EffortDuration getResourceCapacity() {
        return resourceCapacity;
    }

    public EffortDuration getTotalHours() {
        return totalHours;
    }

    public int getTasksNumber() {
        return tasksNumber;
    }

    public String toString(PersonalTimesheetsPeriodicityEnum periodicity) {
        return toString(periodicity, date);
    }

    /**
     * Returns a string representing the personal timehseet in a given
     * <code>date</code> depending on the <code>periodicity</code>.
     */
    public static String toString(PersonalTimesheetsPeriodicityEnum periodicity, LocalDate date) {
        switch (periodicity) {

            case WEEKLY:
                LocalDate start = periodicity.getStart(date);
                LocalDate end = periodicity.getEnd(date);

                String string = date.toString("w");
                    if (start.getMonthOfYear() == end.getMonthOfYear()) {
                    string += " (" + date.toString(MMMM_Y_PATTERN) + ")";
                } else {
                    if (start.getYear() == end.getYear()) {
                        string += " (" + start.toString("MMMM") + " - " + end.toString(MMMM_Y_PATTERN) + ")";
                    } else {
                        string += " (" + start.toString(MMMM_Y_PATTERN) + " - " + end.toString(MMMM_Y_PATTERN) + ")";
                    }
                }
                return _("Week {0}", string);

            case TWICE_MONTHLY:
                return (date.getDayOfMonth() <= 15)
                        ? _("{0} 1st fortnight", date.toString(MMMM_Y_PATTERN))
                        : _("{0} 2nd fortnight", date.toString(MMMM_Y_PATTERN));

            case MONTHLY:
            default:
                return date.toString(MMMM_Y_PATTERN);
        }
    }

}

@@ -1394,30 +1394,208 @@
 ing 
-MMMM_Y_PATTERN = %22MMMM
+d_Y_PATTERN = %22d Y%22;%0A%0A    private static final String MMMM_d_PATTERN = %22MMMM d%22;%0A%0A    private static final String MMMM_Y_PATTERN = %22MMMM y%22;%0A%0A    private static final String MMMM_d_Y_PATTERN = %22MMMM d
  y%22;
@@ -3851,32 +3851,36 @@
                 
+    
 string += %22 (%22 +
@@ -3880,20 +3880,21 @@
  %22 (%22 + 
-date
+start
 .toStrin
@@ -3892,32 +3892,69 @@
 t.toString(MMMM_
+d_PATTERN)  + %22 - %22 + end.toString(d_
 Y_PATTERN) + %22)%22
@@ -4100,14 +4100,22 @@
 ing(
-%22
 MMMM
-%22
+_d_PATTERN
 ) + 
@@ -4132,32 +4132,34 @@
 d.toString(MMMM_
+d_
 Y_PATTERN) + %22)%22
@@ -4242,32 +4242,34 @@
 t.toString(MMMM_
+d_
 Y_PATTERN) + %22 -
@@ -4283,32 +4283,34 @@
 d.toString(MMMM_
+d_
 Y_PATTERN) + %22)%22
@@ -4542,32 +4542,34 @@
 e.toString(MMMM_
+d_
 Y_PATTERN))%0A    
@@ -4624,32 +4624,34 @@
 e.toString(MMMM_
+d_
 Y_PATTERN));%0A%0A  

Add day of week to personal timesheet date display