<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Clumsy\CMS\Models\BaseModel;
use Tymon\JWTAuth\Exceptions\JWTException;

class Voter extends BaseModel implements UserInterface, RemindableInterface {

	public $columns = array(
		'origin'             => 'Origem',
		'identifier'         => 'Identificador',
		'display_created_at' => 'Timestamp',
		'votes_count'        => 'Votos',
	);

	public $default_order = array(
		'created_at' => 'desc',
	);

	public $order_equivalence = array(
		'display_created_at' => 'created_at',
	);

	public $toggle_filters = array(
		'facebook' => 'Facebook',
		'email'    => 'Email',
	);

	public $suppress_when_toggled = array(
		'origin',
	);

	public $suppress_delete = 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 token value for the "remember me" session.
	 *
	 * @return string
	 */
	public function getRememberToken()
	{
		return $this->remember_token;
	}

	/**
	 * Set the token value for the "remember me" session.
	 *
	 * @param  string  $value
	 * @return void
	 */
	public function setRememberToken($value)
	{
		$this->remember_token = $value;
	}

	/**
	 * Get the column name for the "remember me" token.
	 *
	 * @return string
	 */
	public function getRememberTokenName()
	{
		return 'remember_token';
	}

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

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

	public function scopeOfType($query, $type)
	{
		switch ($type) {
			case 'facebook' :
				return $query->whereNotNull('facebook_id');

			case 'email' :
				return $query->whereNull('facebook_id')
							 ->whereNotNull('email');
		}

	}

	public function sendToken($band_id, $token)
	{
		$voter = $this;
		$edition = Contest::getEdition();

		$band = Band::findOrFail($band_id);

        $subject = trans('emails.start_voting.subject');

        $frontendConfig   = Config::get('api.frontend');

        $bandUrl = $frontendConfig['url'] . $frontendConfig['routes']['single-band'];
        $bandUrl = str_replace('{band_id}', $band->slug, $bandUrl);

        $bandUrl = str_replace('{token}', $token, $bandUrl);

		Mail::send("emails.{$edition}.welcome", compact('voter', 'band', 'edition', 'bandUrl'), function($message) use($voter,$subject) {
			$message
				->replyTo(Contest::email())
				->to($voter->email, 'EDP Tanto Fado')
				->subject($subject);
		});
	}

	public function getDisplayCreatedAtAttribute()
	{
		return display_date($this->created_at, 'short with time');
	}

	public function getOriginAttribute()
	{
		if ($this->facebook_id)
		{
			return 'Facebook';
		}

		if ($this->twitter_id)
		{
			return 'Twitter';
		}

		if ($this->email)
		{
			return 'Email';
		}

		return '--';
	}

	public function getIdentifierAttribute()
	{
		switch ($this->origin)
		{
			case 'Email':
				return $this->email;

			case 'Facebook':
				return $this->facebook_id;

			case 'Twitter':
				return $this->twitter_id;
		}
	}

	public function getVotesCountAttribute()
	{
		return $this->votes->count();
	}

	public function displayName()
	{
		return 'registo';
	}

	public function displayNamePlural()
	{
		return 'registos';
	}
}
