<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	protected $softDelete = true;

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array('password');

	/**
	 * Get the unique identifier for the user.
	 *
	 * @return mixed
	 */
	public function getAuthIdentifier()
	{
		return $this->getKey();
	}

	/**
	 * Get the password for the user.
	 *
	 * @return string
	 */
	public function getAuthPassword()
	{
		return $this->password;
	}

	/**
	 * Get the e-mail address where password reminders are sent.
	 *
	 * @return string
	 */
	public function getReminderEmail()
	{
		return $this->email;
	}

    public function getAvatar() {
        if($avatar = $this->getSetting('avatar')) {
            return asset('img/avatars/'.$avatar);
        } else {
            return asset('img/no-avatar.jpg');
        }
    }

    public function getSetting($key, $false = false) {
        $settings = json_decode($this->settings);
        if(!is_null($settings) && property_exists($settings, $key))
            return $settings->$key;
        else
            return $false;
    }

    public function setSetting($key, $value) {
        $settings = json_decode($this->settings);
        if(is_null($settings)) {
            $settings = new stdClass();
        }
        $settings->$key = $value;
        $this->settings = json_encode($settings);
    }

	public function films() {
		return $this->hasMany('Film', 'user');
	}

	public function comments() {
		return $this->hasMany('Comment', 'user');
	}

	public function votes() {
		return $this->hasMany('Vote', 'user');
	}

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