<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function logs()
    {
        return $this->hasMany(Log::class);
    }

    public function updateAPI()
    {
        $this->api_token = str_random(60);

        $this->save();
    }

    public function fullname()
    {
        $fullname = $this->lastname.' '.$this->firstname;
        return $fullname;
    }

    public function messages()
    {
        return $this->hasMany(Message::class);
    }

    public function events()
    {
        return $this->hasMany('App\Event');
    }

    public function bookings()
    {
        return $this->hasMany('App\Booking');
    }

    public function courses()
    {
        return $this->hasMany('App\Course');
    }

    public function routeNotificationForNexmo($notification)
    {
        return $this->telephone;
    }

    public function getPerm($perm)
    {
        $rank = \App\Rank::find($this->rank);
        $job = \App\Job::find($this->job);

        $rank_perm_value = $rank->$perm;
        $job_perm_value = $job->$perm;
        $user_perm_value = $this->$perm;

        if ($user_perm_value == 1 ) {
            $perm_value = true;
        } else if ($job_perm_value== 1)  {
            $perm_value = true;
        } else if ($rank_perm_value == 1) {
            $perm_value = true;
        } else {
            $perm_value = false;
        }

        return $perm_value;
    }

    public function getAcces($level)
    {
        $rank = \App\Rank::find($this->rank);
        $job = \App\Job::find($this->job);

        $rank_perm_value = $rank->acces_level;
        $job_perm_value = $job->acces_level;
        $user_perm_value = $this->acces_level;

        if ($user_perm_value >= $level ) {
            $perm_value = true;
        } else if ($job_perm_value >= $level)  {
            $perm_value = true;
        } else if ($rank_perm_value >= $level) {
            $perm_value = true;
        } else {
            $perm_value = false;
        }
        return $perm_value;
    }

    public function getClasse()
    {
        $schedules = Schedule::all()->where('type','regular')->sortBy('date');
        $currentUser = \Auth::User();

        $filtered_schedule = collect();
        $user_classes = collect();

        foreach ($schedules as $day) {
            if ($day->date >= date('Y-m-d')) {
                $filtered_schedule->push($day);
            }
        }

        foreach ($filtered_schedule as $schedule) {
            if ($schedule->data['n1_p1_instructor'] == $currentUser->id) {
                $user_classes->push(collect([
                    'date' => $schedule->date,
                    'periode' => '1',
                    'niveau' => '1',
                    'name' => $schedule->data['n1_p1_name'],
                    'ocom' => $schedule->data['n1_p1_ocom'],
                    'local' => $schedule->data['n1_p1_local'],
                    'plan_done' => $schedule->data['n1_p1_plandone'],
                    'material' => $schedule->n1_p1_item,
                ]));
            }

            if ($schedule->data['n1_p2_instructor'] == $currentUser->id) {
                $user_classes->push(collect([
                    'date' => $schedule->date,
                    'periode' => '2',
                    'niveau' => '1',
                    'name' => $schedule->data['n1_p2_name'],
                    'ocom' => $schedule->data['n1_p2_ocom'],
                    'local' => $schedule->data['n1_p2_local'],
                    'plan_done' => $schedule->data['n1_p2_plandone'],
                    'material' => $schedule->n1_p2_item,
                ]));
            }

            if ($schedule->data['n2_p1_instructor'] == $currentUser->id) {
                $user_classes->push(collect([
                    'date' => $schedule->date,
                    'periode' => '1',
                    'niveau' => '2',
                    'name' => $schedule->data['n2_p1_name'],
                    'ocom' => $schedule->data['n2_p1_ocom'],
                    'local' => $schedule->data['n2_p1_local'],
                    'plan_done' => $schedule->data['n2_p1_plandone'],
                    'material' => $schedule->n2_p1_item,
                ]));
            }

            if ($schedule->data['n2_p2_instructor'] == $currentUser->id) {
                $user_classes->push(collect([
                    'date' => $schedule->date,
                    'periode' => '2',
                    'niveau' => '2',
                    'name' => $schedule->data['n2_p2_name'],
                    'ocom' => $schedule->data['n2_p2_ocom'],
                    'local' => $schedule->data['n2_p2_local'],
                    'plan_done' => $schedule->data['n2_p2_plandone'],
                    'material' => $schedule->n2_p2_item,
                ]));
            }

            if ($schedule->data['n3_p1_instructor'] == $currentUser->id) {
                $user_classes->push(collect([
                    'date' => $schedule->date,
                    'periode' => '1',
                    'niveau' => '3',
                    'name' => $schedule->data['n3_p1_name'],
                    'ocom' => $schedule->data['n3_p1_ocom'],
                    'local' => $schedule->data['n3_p1_local'],
                    'plan_done' => $schedule->data['n3_p1_plandone'],
                    'material' => $schedule->n3_p1_item,
                ]));
            }

            if ($schedule->data['n3_p2_instructor'] == $currentUser->id) {
                $user_classes->push(collect([
                    'date' => $schedule->date,
                    'periode' => '2',
                    'niveau' => '3',
                    'name' => $schedule->data['n3_p2_name'],
                    'ocom' => $schedule->data['n3_p2_ocom'],
                    'local' => $schedule->data['n3_p2_local'],
                    'plan_done' => $schedule->data['n3_p2_plandone'],
                    'material' => $schedule->n3_p2_item,
                ]));
            }
        }

        return $user_classes;
    }

    public function countActivity()
    {
        return count(\App\Log::all()->where('user_id',$this->id));
    }

    public function countClasse()
    {
        $nbClasse = 0;

        $schedules = Schedule::all()->where('type','regular')->sortBy('date');

        $filtered_schedule = collect();

        foreach ($schedules as $day) {
            if ($day->date >= date('Y-m-d')) {
                $filtered_schedule->push($day);
            }
        }

        foreach ($filtered_schedule as $schedule) {
            if ($schedule->data['n1_p1_instructor'] == $this->id) {
                $nbClasse = ++$nbClasse;
            }
            if ($schedule->data['n1_p2_instructor'] == $this->id) {
                $nbClasse = ++$nbClasse;
            }
            if ($schedule->data['n2_p1_instructor'] == $this->id) {
                $nbClasse = ++$nbClasse;
            }
            if ($schedule->data['n2_p2_instructor'] == $this->id) {
                $nbClasse = ++$nbClasse;
            }
            if ($schedule->data['n3_p1_instructor'] == $this->id) {
                $nbClasse = ++$nbClasse;
            }
            if ($schedule->data['n3_p2_instructor'] == $this->id) {
                $nbClasse = ++$nbClasse;
            }
        }
        return $nbClasse;
    }

    public function seenMessage($id)
    {
        $this_msg = Message::find($id);

        if(isset($this_msg->data['as_seen']))
        {
            $as_seen = explode("-",$this_msg->data['as_seen']);
            if (array_search(strval($this->id),$as_seen) === false) {
                array_push($as_seen,$this->id);
            }
            $as_seen = array_filter($as_seen);
            $as_seen_str = implode('-',$as_seen);
            $data = [
                'as_seen' => $as_seen_str,
                'parameter' => $this_msg->data['parameter']
            ];
            
            $this_msg->data = $data;

            $this_msg->save();
        }
    }

    public function news()
    {
        return $this->hasMany('App\News');
    }
}
