<?php
namespace Controllers;

class TasksController
{
  private const DEFAULT_NEW_TASK_STATUS = 5; // do zrobienia

  public static function workTime()
  {
    $work_time_repository = new \Domain\Tasks\WorkTimeRepository();

    $view_model = self::workTimeViewModel(
      $work_time_repository -> getClientsWithUnsettledTasks(),
      \factory\Crm::settings()
    );

    return \Tpl::view( 'tasks/work-time', $view_model );
  }

  public static function workTimeViewModel( array $work_time_clients, array $settings )
  {
    return [
      'work_time_clients' => $work_time_clients,
      'settings' => $settings
    ];
  }

  public static function resolveTaskStatusForForm( array $task )
  {
    $is_new_task = !isset( $task['id'] ) or !(int)$task['id'];

    if ( $is_new_task and ( !isset( $task['status'] ) or $task['status'] === null or $task['status'] === '' ) )
      return self::DEFAULT_NEW_TASK_STATUS;

    return isset( $task['status'] ) ? (int)$task['status'] : 0;
  }

  public static function resolveTaskStatusForSave( array $values )
  {
    $is_new_task = !isset( $values['id'] ) or !(int)$values['id'];

    if ( $is_new_task and ( !isset( $values['status'] ) or $values['status'] === null or $values['status'] === '' ) )
      return self::DEFAULT_NEW_TASK_STATUS;

    return isset( $values['status'] ) ? (int)$values['status'] : 0;
  }

  public static function taskChangeStatus()
  {
    global $mdb, $user;

    $task_id = (int)\S::get( 'task_id' );
    $status = (int)\S::get( 'status' );

    if ( $mdb -> update( 'tasks', [ 'status' => $status ], [ 'id' => $task_id ] ) )
    {
      if ( $user and self::shouldStopTimerOnStatus( $status ) )
        \factory\Tasks::task_end( $task_id, $user['id'] );

      if ( self::shouldSendStatusChangeEmail( $status ) )
        self::sendEmailTaskChangeStatus( $task_id );

      if ( $status === 2 )
        $mdb -> update( 'tasks', [ 'date_complete' => date( 'Y-m-d H:i:s' ) ], [ 'id' => $task_id ] );

      echo json_encode( [ 'status' => 'success' ] );
    }
    else
      echo json_encode( [ 'status' => 'error' ] );

    exit;
  }

  public static function shouldStopTimerOnStatus( $status )
  {
    return in_array( (int)$status, [ 1, 2, 3 ], true );
  }

  public static function shouldSendStatusChangeEmail( $status )
  {
    return in_array( (int)$status, [ 1, 3 ], true );
  }

  public static function sendEmailTaskChangeStatus( $task_id )
  {
    $task = \factory\Tasks::task_details( $task_id );
    $statuses = \factory\Tasks::get_statuses();

    if ( $task['status_change_mail'] )
    {
      \S::send_email(
        'biuro@project-pro.pl',
        'crmPRO - zmieniono status zadania',
        '<p>Witaj<br/>zmieniono status zadania <b>' . $task['name'] . ' - ' . \factory\Crm::get_client_name( (int)$task['client_id'] ) . '</b> na <b>' . $statuses[ $task['status'] ] . '</b>.</p>' .
        '<p>' . html_entity_decode( $task['text'] ) . '</p>' .
        '<p>Pozdrawiamy<br/>Zespół crmPRO.pl</p>'
      );
    }
  }
}
