<?php

namespace App\Listeners;

use App\Events\Event;
use App\Events\TaskStatusChange;
use App\Http\Middleware\isTeamLeader;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Eventlog;
use Illuminate\Support\Facades\Auth;
use App\Unit;
use App\Notifications;
use App\Profile;
use Illuminate\Support\Facades\DB;
use App\Tasktag;


class TaskStatusChangeListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     * @param  Event $task
     * @return void
     */
    public function handle(TaskStatusChange $event)
    {

        $task = $event->task;
        $eventlog = new Eventlog();
        $eventlog->user_id = Auth::user()->id;
        $eventlog->type = 'TaskStatusChange';
        $eventlog->relation_id = $task->id;

        if ($event->content) {
            $eventlog->content = $event->content;
        }

        $eventlog->save();

        $pipeline = $task->pipeline->first(); //primera unidad del pipeline

        $user_array = array();

        $adminOSupervisor = DB::table('user_has_roles')//administrador y supervisor
        ->whereIn('user_has_roles.role_id', [1, 4])->get();

        foreach ($adminOSupervisor as $user) {

            if ($eventlog->user_id != $user->user_id) {

                array_push($user_array, $user->user_id);
            }
        }

        $userClient = $task->profile->users; //trae los usuarios clientes

        foreach ($userClient as $index => $user) {
            switch ($pipeline->system) {
                // pipeline comun
                case 0:
                    if ($user->id != $eventlog->user_id) {
                        array_push($user_array, $user->id);
                    }
                    
                    break;

                // pipeline de pre ingreso
                case 1:
                    // usuario no es exteno (cliente)
                    if (!$user->isApplicant()) {
                        if ($user->id != $eventlog->user_id) {
                            array_push($user_array, $user->id);
                        }
                    }
                    
                    break;
                
                default:
                    if ($user->id != $eventlog->user_id) {
                        array_push($user_array, $user->id);
                    }
                     
                    break;
            }
        }

        $user_tags = Tasktag::where('relation_id', '=', $task->id)//etiquetados
        ->where('unit_id', '=', $task->unit_id)
            ->where('type', '=', 'task')
            ->where('active', '=', 1)->get();



        if (is_countable($user_tags)){

            foreach ($user_tags as $tag_index => $user) {

                if($eventlog->user_id != $user->user_id) {

                    array_push($user_array, $user->user_id);
                }
            }
        }



        $unitFirst = $pipeline->unitsByRevision($task->pipelineRevision);


        foreach ($unitFirst as $unit) {//usuarios que se encuentran en la misma unidad del pipeline

            if ($unit->teamleaders) {

                foreach ($unit->teamleaders as $tl) {
                    if ($eventlog->user_id != $tl->id) {
                        array_push($user_array, $tl->id);
                    }

                }

            }



        }

        $array_unique = array_unique($user_array, SORT_REGULAR); //guarda usuarios a notificar sin repetir

        foreach ($array_unique as $user_id) {

            $notification = new Notifications();
            $notification->eventlogs_id = $eventlog->id;
            $notification->type = $eventlog->type;
            $notification->user_to = $user_id;
            $notification->is_delivered = 0;
            $notification->is_read = 0;
            $notification->is_archived = 0;
            $notification->save();

        }

    }
}