<?php
namespace App\Models;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Auth;

class Issue{

  public static function tipologiaRichieste(){
    return [
      'Assistenza sistemistica' => 'Assistenza sistemistica',
      'Assistenza su Firewall' => 'Assistenza su Firewall',
      'Assistenza su connettività e centralini telefonici' => 'Assistenza su connettività e centralini telefonici',
      'Assistenza su servizi internet' => 'Assistenza su servizi internet',
      'Assistenza su servizi Cloud' => 'Assistenza su servizi Cloud'
    ];
  }

  public static function getCollection(){
    $redmineUser = session('redmine_user');
    $collection = new Collection();

    $project_ids = json_decode(Auth::user()->progetti);
    $status = json_decode(Config::getValue(Config::STATI_SEGNALAZIONI_VALIDE));

    // Issue assegnati direttamente all'utente

    foreach($project_ids as $project_id){
      $issueResponse = Http::get(config('redmine.url').'/issues.json', [
        'key' => $redmineUser->api_key,
        'project_id' => $project_id,
        'status_id' => ($status!=null && count($status)>0)?implode('|', $status):'open',
        // 'limit' => 100,
      ]);


      try{
        foreach($issueResponse->object()->issues as $issue){
          $collection->push($issue);
        }
      }catch(\Exception $e){
      }
    }


    return $collection;
  }

  public static function getTempoImpiegato($issue_id, $attivita, $from = null, $to = null, $fatturare = 'Sì|Fatturato'){
    $redmineUser = session('redmine_user');
    $collection = new Collection();

    $params = [
      'key' => $redmineUser->api_key,
      'issue_id' => $issue_id,
      'cf_'.Config::getValue(Config::CAMPO_FATTURARE) => $fatturare, // Custom fields "fatturare"
    ];

    if(is_array($attivita)){
      $params['activity_id'] = implode('|', $attivita);
    }else{
      $params['activity_id'] = $attivita;
    }

    // dd($params);

    if($from != null){
      $params['from'] = $from;
    }

    if($to != null){
      $params['to'] = $to;
    }


    $issueResponse = Http::get(config('redmine.url').'/time_entries.json', $params);

    $last_date = null;
    try{
      foreach($issueResponse->object()->time_entries as $time){
        // $collection->push($time);
        if($last_date == null){
          $last_date = $time;
        }else{
          if($last_date->spent_on == $time->spent_on){
            $last_date->hours += $time->hours;
          }else{
            $collection->push($last_date);
            $last_date = $time;
          }
        }
      }
      if($last_date != null){
        $collection->push($last_date);
      }
    }catch(\Exception $e){
    }

    return $collection;
  }

  public static function getStati(){
    $redmineUser = session('redmine_user');

    // issueStatus
    $response = Http::get(config('redmine.url').'/issue_statuses.json', [
      'key' => $redmineUser->api_key,
    ]);

    $issueStatus = array();
    foreach($response->object()->issue_statuses as $issue_status){
      $issueStatus[$issue_status->id] = $issue_status->name;
    }

    return $issueStatus;
  }

  public static function getAttivita(){
    $redmineUser = session('redmine_user');

    $response = Http::get(config('redmine.url').'/enumerations/time_entry_activities.json', [
      'key' => $redmineUser->api_key,
    ]);

    $attivita = array();
    foreach($response->object()->time_entry_activities as $time_entry_activities){
      if(!$time_entry_activities->active) continue;
      $attivita[$time_entry_activities->id] = $time_entry_activities->name;
    }

    return $attivita;
  }

  public static function getCustomFields(){
    $redmineUser = session('redmine_user');

    $response = Http::get(config('redmine.url').'/custom_fields.json', [
      'key' => $redmineUser->api_key,
      'customized_type' => 'time_entry'
    ]);

    // dd($response->object()->custom_fields);

    $custom = array();
    foreach($response->object()->custom_fields as $custom_field){
      $custom[$custom_field->id] = $custom_field->name;
    }

    return $custom;
  }

}
