<?php

namespace dominion\user;

use Yii;
use yii\base\BaseObject;
use yii\web\IdentityInterface;
use yii\filters\RateLimitInterface;
use \Lcobucci\JWT\Validation\Constraint\SignedWith;

class User extends BaseObject implements IdentityInterface , RateLimitInterface
{
    public $id;
    public $username;
    public $password;
    public $authKey;
    public $accessToken;
    public $role;
    public $priceType = 0;



    /**
     * {@inheritdoc}
     */
    public static function findIdentity($id)
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return new static([
            'id' =>(string)$token->claims()->get('uid'),
            'role' =>(string)$token->claims()->get('role'),
            'priceType' => (int)$token->claims()->get('priceType'),
        ]);
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return self::findIdentity((string) $username);
    }

    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return $this->id;
    }

    public function getRole()
    {
        return $this->role;
    }

    /**
     * {@inheritdoc}
     */
    public function getAuthKey()
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function validateAuthKey($authKey)
    {
        return false;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return bool if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return $this->password === hash('sha512',$password);
    }

    public function getRateLimit($request, $action)
    {
        return [1000, 1]; // $rateLimit requests per second
}

    public function loadAllowance($request, $action)
    {
        return [1000, time()];
}

    public function saveAllowance($request, $action, $allowance, $timestamp)
    {

    }

    public static function getPriceTypeId()
    {
        return isset(Yii::$app->user) ? Yii::$app->user->identity->priceType : 1;
    }

    public static function getAuthMemberId()
    {
        return isset(Yii::$app->user) ? Yii::$app->user->identity->id : 0;
    }
}