<?php

namespace MGModule\DNSManager2\mgLibs\custom;

use MGModule\DNSManager2\models\custom\task;

class TaskManager {
    //TODO: removeTask + obsługa dla tasków z błędami
    
    public static function addTask($name, array $params = array(), $parentid = 0, $clientId = 0, $status = false) {
        $task = new task\Task();
        $task->name = $name;
        $task->nextrun = 0;
        $task->lastrun = 0;
        $task->date = date('Y-m-d H:i:s');
        $task->params = $params;
        $task->parentid = $parentid;
        $task->repeats = 0;
        $task->clientid = $clientId;
        $task->status = $status ? $status : task\TaskStatusEnum::START;
        $task->save();
        return $task;
    }
    
    public static function addTaskResult($taskid, array $data = array()) {
        $task_result = new task\result\TaskResult();
        $task_result->taskid = $taskid;
        $task_result->data = $data;
        $task_result->date = date('Y-m-d H:i:s');
        $task_result->save();
        return $task_result;
    }
    
    public static function getTaskResults($taskid) {
        $rep = new task\result\Repository();
        $rep->setFilter('taskid', $taskid);
        return $rep->get();
    }
    
    public static function removeTask($id) {
        self::getTaskObjectByID($id)->remove();
    }
    
    /** @return task\TaskAbstract */
    public static function getTaskObjectByID($taskid) {

        return self::getTaskObject(new task\Task($taskid));
    }

    public static function getTaskObject(task\Task $task, $parent = false) {
        list($class_name, $method) = explode(':',$task->name);
        $class_name = __NAMESPACE__ . '\\task\\' . $class_name;

        return new $class_name($task, $method, $parent);
    }
    
    public static function getXTasks($x, $name = false, $parentid = false, $status = task\TaskStatusEnum::IN_PROGRESS, $clientId = false) {
        $rep = new task\Repository();
        
        $rep->setFilter(0, array(
            'customQuery' => '`nextrun` <= NOW()',
            'params' => array(),
        ));
        if($status !== false) 
            $rep->setFilter('status', $status);
        $rep->setFilter('parentid', $parentid===false?0:$parentid);
        if($name!==false)
            $rep->setFilter('name', $name);
        if($clientId!==false)
            $rep->setFilter('clientid', $clientId);
        
        $rep->sortBy('repeats', 'ASC'); //TODO: mozna by dodać priority
        if($x > 0)
            $rep->limit($x);
        return $rep->get();
    }
    
    
    public static function countTasks( $name = false, $parentid = false, $status = false )
    {
        $rep = new task\Repository();
        
        $rep->setFilter(0, array(
            'customQuery' => '`nextrun` <= NOW()',
            'params' => array(),
        ));
        if($status !== false) 
            $rep->setFilter('status', $status);
        $rep->setFilter('parentid', $parentid===false?0:$parentid);
        if($name!==false)
            $rep->setFilter('name', $name);
        
        return $rep->count();
    }
    
    
    
    /** @return Array|task\TaskAbstract */
    public static function getXTasksObjects($x, $name = false, $parentid = false, $status = task\TaskStatusEnum::IN_PROGRESS, $clientId = false) {
        return array_map(array(self,'getTaskObject'), self::getXTasks($x, $name, $parentid, $status, $clientId));
    }
    
    public static function getParamsRecursive($taskid) {
        $task = new task\Task($taskid);
        $params = $task->params;
        if($task->parentid > 0) {
            $params['parent'] = self::getParamsRecursive($task->parentid);
        }
        return $params;

    }
    
    public static function getTaskResultByID($resultid) {
        return new task\result\TaskResult($resultid);
    }

    public static function getTasksByNameAndStatus($taskName, $taskStatus)
    {
        $rep = new task\Repository();
        $rep->byName($taskName);
        $rep->byStatus($taskStatus);
        return $rep->get();
    }
}