<?php

namespace Thang.luu\Word\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Thang.luu\Word\Models\Words;

class WordsPolicy
{
    use HandlesAuthorization;

    /**
     * Determine if the given user can view the words.
     *
     * @param User $user
     * @param Words $words
     *
     * @return bool
     */
    public function view(User $user, Words $words)
    {
        if ($user->canDo('word.words.view') && $user->is('admin')) {
            return true;
        }

        return $user->id === $words->user_id;
    }

    /**
     * Determine if the given user can create a words.
     *
     * @param User $user
     * @param Words $words
     *
     * @return bool
     */
    public function create(User $user)
    {
        return  $user->canDo('word.words.create');
    }

    /**
     * Determine if the given user can update the given words.
     *
     * @param User $user
     * @param Words $words
     *
     * @return bool
     */
    public function update(User $user, Words $words)
    {
        if ($user->canDo('word.words.update') && $user->is('admin')) {
            return true;
        }

        return $user->id === $words->user_id;
    }

    /**
     * Determine if the given user can delete the given words.
     *
     * @param User $user
     * @param Words $words
     *
     * @return bool
     */
    public function delete(User $user, Words $words)
    {
        if ($user->canDo('word.words.delete') && $user->is('admin')) {
            return true;
        }

        return $user->id === $words->user_id;
    }

    /**
     * Determine if the user can perform a given action ve.
     *
     * @param [type] $user    [description]
     * @param [type] $ability [description]
     *
     * @return [type] [description]
     */
    public function before($user, $ability)
    {
        if ($user->isSuperUser()) {
            return true;
        }
    }
}