/*
  WeeklySummary.cpp

  This file is part of Charm, a task-based time tracking application.

  Copyright (C) 2014-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com

  Author: Frank Osterfeld <frank.osterfeld@kdab.com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "WeeklySummary.h"

#include "Core/CharmDataModel.h"
#include "Core/Event.h"
#include "Core/Task.h"

static const int DAYS_IN_WEEK = 7;

WeeklySummary::WeeklySummary()
    : durations(DAYS_IN_WEEK, 0)
{
}

QVector<WeeklySummary> WeeklySummary::summariesForTimespan(CharmDataModel *dataModel,
                                                           const TimeSpan &timespan)
{
    const EventIdList eventIds = dataModel->eventsThatStartInTimeFrame(timespan);
    // prepare a list of unique task ids used within the time span:
    TaskIdList taskIds, uniqueTaskIds; // the list of tasks to show
    EventList events;
    Q_FOREACH (EventId id, eventIds) {
        Event event = dataModel->eventForId(id);
        events << event;
        taskIds << event.taskId();
    }
    qSort(taskIds);
    std::unique_copy(taskIds.begin(), taskIds.end(), std::back_inserter(uniqueTaskIds));
    Q_ASSERT(events.size() == eventIds.size());
    // retrieve task information
    QVector<WeeklySummary> summaries(uniqueTaskIds.size());
    for (int i = 0; i < uniqueTaskIds.size(); ++i) {
        summaries[i].task = uniqueTaskIds.at(i);
        const Task &task = dataModel->getTask(uniqueTaskIds[i]);
        summaries[i].taskname = dataModel->fullTaskName(task);
    }
    // now add the times to the tasks:
    Q_FOREACH (const Event &event, events) {
        // find the index for this event:
        auto it = std::find(uniqueTaskIds.begin(), uniqueTaskIds.end(), event.taskId());
        if (it != uniqueTaskIds.end()) {
            const int index = std::distance(uniqueTaskIds.begin(), it);
            Q_ASSERT(index >= 0 && index < summaries.size());
            const int dayOfWeek = event.startDateTime().date().dayOfWeek() - 1;
            Q_ASSERT(dayOfWeek >= 0 && dayOfWeek < 7);
            summaries[index].durations[dayOfWeek] += event.duration();
        }
    }

    return summaries;
}
