<?php

namespace Model;

/**
 * Task Filter
 *
 * @package  model
 * @author   Frederic Guillot
 */
class TaskFilter extends Base
{
    private $query;

    public function create()
    {
        $this->query = $this->db->table(Task::TABLE);
        return $this;
    }

    public function excludeTasks(array $task_ids)
    {
        $this->query->notin('id', $task_ids);
        return $this;
    }

    public function filterById($task_id)
    {
        if ($task_id > 0) {
            $this->query->eq('id', $task_id);
        }

        return $this;
    }

    public function filterByTitle($title)
    {
        $this->query->ilike('title', '%'.$title.'%');
        return $this;
    }

    public function filterByProjects(array $project_ids)
    {
        $this->query->in('project_id', $project_ids);
        return $this;
    }

    public function filterByProject($project_id)
    {
        if ($project_id > 0) {
            $this->query->eq('project_id', $project_id);
        }

        return $this;
    }

    public function filterByCategory($category_id)
    {
        if ($category_id >= 0) {
            $this->query->eq('category_id', $category_id);
        }

        return $this;
    }

    public function filterByOwner($owner_id)
    {
        if ($owner_id >= 0) {
            $this->query->eq('owner_id', $owner_id);
        }

        return $this;
    }

    public function filterByColor($color_id)
    {
        if ($color_id !== '') {
            $this->query->eq('color_id', $color_id);
        }

        return $this;
    }

    public function filterByColumn($column_id)
    {
        if ($column_id >= 0) {
            $this->query->eq('column_id', $column_id);
        }

        return $this;
    }

    public function filterBySwimlane($swimlane_id)
    {
        if ($swimlane_id >= 0) {
            $this->query->eq('swimlane_id', $swimlane_id);
        }

        return $this;
    }

    public function filterByStatus($is_active)
    {
        if ($is_active >= 0) {
            $this->query->eq('is_active', $is_active);
        }

        return $this;
    }

    public function filterByDueDateRange($start, $end)
    {
        $this->query->gte('date_due', $this->dateParser->getTimestampFromIsoFormat($start));
        $this->query->lte('date_due', $this->dateParser->getTimestampFromIsoFormat($end));

        return $this;
    }

    public function findAll()
    {
        return $this->query->findAll();
    }

    public function toAutoCompletion()
    {
        return $this->query->columns('id', 'title')->filter(function(array $results) {

            foreach ($results as &$result) {
                $result['value'] = $result['title'];
                $result['label'] = '#'.$result['id'].' - '.$result['title'];
            }

            return $results;

        })->findAll();
    }

    public function toCalendarEvents()
    {
        $events = array();

        foreach ($this->query->findAll() as $task) {
            $events[] = array(
                'timezoneParam' => $this->config->getCurrentTimezone(),
                'id' => $task['id'],
                'title' => t('#%d', $task['id']).' '.$task['title'],
                'start' => date('Y-m-d', $task['date_due']),
                'end' => date('Y-m-d', $task['date_due']),
                'allday' => true,
                'backgroundColor' => $this->color->getBackgroundColor($task['color_id']),
                'borderColor' => $this->color->getBorderColor($task['color_id']),
                'textColor' => 'black',
                'url' => $this->helper->url('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])),
            );
        }

        return $events;
    }
}
