/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolóxico de Galicia
 * Copyright (C) 2010-2011 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.business.planner.entities;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.libreplan.business.common.BaseEntity;

/**
 *
 * @author Diego Pino García <dpino@igalia.com>
 *
 *         This is a class, directly associated with a TaskGroup with no parent
 *         (TaskRoot element), which is used to store data about the whole
 *         scheduling
 *
 */
public class PlanningData extends BaseEntity {

    private static Log LOG = LogFactory.getLog(PlanningData.class);

    public static PlanningData create(TaskGroup rootTask) {
        return create(new PlanningData(rootTask));
    }

    private TaskGroup rootTask;

    private BigDecimal progressAllByNumHours;

    private BigDecimal progressByDuration;

    private BigDecimal progressByNumHours;

    private BigDecimal theoreticalProgressAllByNumHours;

    private BigDecimal theoreticalProgressByDuration;

    private BigDecimal theoreticalProgressByNumHours;

    public PlanningData() {

    }

    public BigDecimal getProgressAllByNumHours() {
        return progressAllByNumHours;
    }

    public BigDecimal getProgressByDuration() {
        return progressByDuration;
    }

    public BigDecimal getProgressByNumHours() {
        return progressByNumHours;
    }

    public BigDecimal getTheoreticalProgressAllByNumHours() {
        return theoreticalProgressAllByNumHours;
    }

    public BigDecimal getTheoreticalProgressByDuration() {
        return theoreticalProgressByDuration;
    }

    public BigDecimal getTheoreticalProgressByNumHours() {
        return theoreticalProgressByNumHours;
    }

    private PlanningData(TaskGroup rootTask) {
        this.rootTask = rootTask;
    }

    public void update(List<Task> criticalPath) {
        if (criticalPath.isEmpty()) {
            LOG.warn("it can't be updated because the critical path provided is empty");
            return;
        }
        progressAllByNumHours = rootTask.getOrderElement()
                .getAdvancePercentageChildren();
        progressByDuration = calculateByDuration(criticalPath);
        progressByNumHours = calculateByNumHours(criticalPath);

        Date now = new Date();
        theoreticalProgressAllByNumHours = rootTask.getTheoreticalAdvancePercentageUntilDate(now);
        theoreticalProgressByDuration = calculateTheoreticalAdvanceByDurationForCriticalPath(criticalPath, now);
        theoreticalProgressByNumHours = calculateTheoreticalAdvanceByNumHoursForCriticalPath(criticalPath, now);
    }

    private BigDecimal calculateByDuration(List<Task> criticalPath) {
        int totalDuration = 0;
        BigDecimal totalProgress = BigDecimal.ZERO;

        for (Task each : criticalPath) {
            int duration = each.getWorkableDays();
            BigDecimal progress = each.getAdvancePercentage().multiply(
                    BigDecimal.valueOf(duration));

            totalDuration = totalDuration + duration;
            totalProgress = totalProgress.add(progress);
        }
        return divide(totalProgress, totalDuration);
    }

    /**
     * Prevents division by zero
     *
     * @param numerator
     * @param denominator
     * @return
     */
    private BigDecimal divide(BigDecimal numerator, int denominator) {
        if (denominator == 0) {
            return BigDecimal.ZERO;
        }
        return numerator.divide(BigDecimal.valueOf(denominator), 8,
                BigDecimal.ROUND_HALF_EVEN);
    }

    private BigDecimal calculateByNumHours(List<Task> criticalPath) {
        int totalNumHours = 0, numHours;
        BigDecimal totalProgress = BigDecimal.ZERO, progress;

        for (Task each: criticalPath) {
            numHours = each.getAssignedHours();
            if (numHours == 0) {
                numHours = each.getTotalHours();
            }
            progress = each.getAdvancePercentage();
            progress = progress.multiply(BigDecimal.valueOf(numHours));

            totalNumHours += numHours;
            totalProgress = totalProgress.add(progress);
        }
        return divide(totalProgress, totalNumHours);
    }

    private BigDecimal calculateTheoreticalAdvanceByNumHoursForCriticalPath(
            List<Task> criticalPath, Date limit) {
        int theoreticalNumHours = 0, totalNumHours = 0;
        for (Task each: criticalPath) {
            theoreticalNumHours += each.getTheoreticalCompletedHoursUntilDate(limit);
            totalNumHours += each.getTotalHours();
        }
        return new BigDecimal(theoreticalNumHours).divide(new BigDecimal(totalNumHours));
    }

    private BigDecimal calculateTheoreticalAdvanceByDurationForCriticalPath(
            List<Task> criticalPath, Date limit) {
        // TODO Needs some extra code
        return null;
    }

}

@@ -5284,24 +5284,25 @@
 mHours = 0;%0A
+%0A
         for 
@@ -5491,16 +5491,23 @@
  return 
+divide(
 new BigD
@@ -5537,31 +5537,10 @@
 urs)
-.divide(new BigDecimal(
+, 
 tota
@@ -5549,17 +5549,16 @@
 umHours)
-)
 ;%0A    %7D%0A

Use private method for dividing.