<?php

class ModuleTask extends Module
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Name = 'Task';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPLv3';
    $this->Description = 'Work and task management';
    $this->Dependencies = array(ModuleUser::GetName());
    $this->Models = array(TaskGroup::GetClassName(), Task::GetClassName(), Work::GetClassName());
  }

  function DoStart(): void
  {
    $this->System->FormManager->RegisterClass('Task', array(
      'Title' => 'Úkoly',
      'Table' => 'Task',
      'DefaultSortColumn' => 'TimeCreate',
      'DefaultSortOrder' => 1,
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => '', 'Required' => true),
        'TimeCreate' => array('Type' => 'Date', 'Caption' => 'Datum zadání', 'Default' => '', 'Required' => true),
        'TimeDue' => array('Type' => 'Date', 'Caption' => 'Termín', 'Default' => '', 'Null' => true),
        'TimeClose' => array('Type' => 'Date', 'Caption' => 'Datum uzavření', 'Default' => '', 'Null' => true),
        'Priority' => array('Type' => 'TPriority', 'Caption' => 'Důležitost', 'Default' => 1),
        'Public' => array('Type' => 'Boolean', 'Caption' => 'Veřejné', 'Default' => '0'),
        'Progress' => array('Type' => 'Integer', 'Caption' => 'Průběh', 'Default' => '0', 'Suffix' => '%'),
        'Group' => array('Type' => 'TTaskGroup', 'Caption' => 'Kategorie', 'Default' => '', 'Null' => true),
        'Description' => array('Type' => 'Text', 'Caption' => 'Popis', 'Default' => '', 'NotInList' => true),
        'Conclusion' => array('Type' => 'Text', 'Caption' => 'Vyhodnocení', 'Default' => '', 'NotInList' => true),
        'AssignedTo' => array('Type' => 'TUser', 'Caption' => 'Přiřazeno', 'Default' => '', 'Null' => true),
        'Work' => array('Type' => 'TWorkListTask', 'Caption' => 'Práce', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('TaskGroup', array(
      'Title' => 'Kategorie úkolu',
      'Table' => 'TaskGroup',
      'DefaultSortColumn' => 'Name',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
        'Description' => array('Type' => 'Text', 'Caption' => 'Popis', 'Default' => ''),
        'Parent' => array('Type' => 'TTaskGroup', 'Caption' => 'Kategorie', 'Default' => '', 'Null' => true),
        'Tasks' => array('Type' => 'TTaskList', 'Caption' => 'Úkoly', 'Default' => ''),
      ),
    ));
    $this->System->FormManager->RegisterClass('Work', array(
      'Title' => 'Práce',
      'Table' => 'Work',
      'DefaultSortColumn' => 'TimeStart',
      'Items' => array(
        'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
        'Description' => array('Type' => 'Text', 'Caption' => 'Popis', 'Default' => ''),
        'TimeStart' => array('Type' => 'DateTime', 'Caption' => 'Čas začátku', 'Default' => ''),
        'Duration' => array('Type' => 'Float', 'Caption' => 'Trvání', 'Default' => '1', 'Suffix' => 'hodin'),
        'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => '1', 'Null' => true),
        'Task' => array('Type' => 'TTask', 'Caption' => 'Úkol', 'Default' => '', 'Null' => true),
      ),
    ));
    $this->System->FormManager->RegisterFormType('TTask', array(
      'Type' => 'Reference',
      'Table' => 'Task',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TWorkListTask', array(
      'Type' => 'ManyToOne',
      'Table' => 'Work',
      'Id' => 'Id',
      'Ref' => 'Task',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TTaskList', array(
      'Type' => 'ManyToOne',
      'Table' => 'Task',
      'Id' => 'Id',
      'Ref' => 'Group',
      'Filter' => '1',
    ));
    $this->System->FormManager->RegisterFormType('TTaskGroup', array(
      'Type' => 'Reference',
      'Table' => 'TaskGroup',
      'Id' => 'Id',
      'Name' => 'Name',
      'Filter' => '1',
    ));

    ModuleIS::Cast($this->System->GetModule('IS'))->RegisterDashboardItem('Task',
      array($this, 'ShowDashboardItem'));
  }

  function ShowDashboardItem(): string
  {
    $DbResult = $this->Database->select('Task', 'COUNT(*)', '`Progress` < 100');
    $DbRow = $DbResult->fetch_row();
    $Output = 'Nedokončených úkolů: <a href="'.$this->System->Link('/is/?a=list&amp;t=Task&amp;filter=1&amp;FilterProgress=100&amp;FilterOpProgress=less').'">'.$DbRow['0'].'</a><br/>';
    return $Output;
  }
}

class Task extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddString('Name');
    $Desc->AddDate('TimeCreate');
    $Desc->AddDate('TimeDue');
    $Desc->AddDate('TimeClose');
    $Desc->AddEnum('Priority', array('Nízká', 'Střední', 'Vysoká'));
    $Desc->AddBoolean('Public');
    $Desc->AddInteger('Progress');
    $Desc->AddReference('Group', TaskGroup::GetClassName());
    $Desc->AddString('Description');
    $Desc->AddText('Conclusion');
    $Desc->AddReference('AssignedTo', User::GetClassName());
    return $Desc;
  }
}

class TaskGroup extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddString('Name');
    $Desc->AddText('Description');
    $Desc->AddReference('Parent', TaskGroup::GetClassName());
    return $Desc;
  }
}

class Work extends Model
{
  static function GetModelDesc(): ModelDesc
  {
    $Desc = new ModelDesc(self::GetClassName());
    $Desc->AddString('Name');
    $Desc->AddText('Description');
    $Desc->AddDateTime('TimeStart');
    $Desc->AddFloat('Duration');
    $Desc->AddReference('User', User::GetClassName());
    $Desc->AddReference('Task', Task::GetClassName());
    return $Desc;
  }
}
