<?php
namespace frontend\models;

use yii\base\Model;
use yii\web\IdentityInterface;

class AdminIdentity extends Model implements IdentityInterface
{
    public $id;
    public $create_date;
    public $modify_date;
    public $department;
    public $email;
    public $is_account_enabled;
    public $is_account_expired;
    public $is_account_locked;
    public $is_credentials_expired;
    public $locked_date;
    public $login_date;
    public $login_failure_count;
    public $login_ip;
    public $name;
    public $username;
    public $password;
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'password'], 'required'],
            [['username', 'password'], 'string'],
            [['password'], 'string', 'min' => 6, 'max' => 20],
            [['id', 'create_date', 'modify_date', 'department', 'email', 'is_account_enabled', 'is_account_expired', 'is_account_locked', 'is_credentials_expired', 'locked_date', 'login_date', 'login_failure_count', 'login_ip', 'name'], 'safe'],
        ];
    }

    /**
     * Finds an identity by the given ID.
     * @param string|int $id the ID to be looked for
     * @return IdentityInterface the identity object that matches the given ID.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentity($id)
    {
        $api = new AdminApi();
        $result = $api->getAdminById($id);
        if ($result['code'] == 1) {
            $identity = new static();
            $identity->setAttributes($result['data']);
            return $identity;
        } else {
            return null;
        }
    }

    /**
     * Finds an identity by the given token.
     * @param mixed $token the token to be looked for
     * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
     * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
     * @return IdentityInterface the identity object that matches the given token.
     * Null should be returned if such an identity cannot be found
     * or the identity is not in an active state (disabled, deleted, etc.)
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return null;
    }

    /**
     * Returns an ID that can uniquely identify a user identity.
     * @return string|int an ID that uniquely identifies a user identity.
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Returns a key that can be used to check the validity of a given identity ID.
     *
     * The key should be unique for each individual user, and should be persistent
     * so that it can be used to check the validity of the user identity.
     *
     * The space of such keys should be big enough to defeat potential identity attacks.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @return string a key that is used to check the validity of a given identity ID.
     * @see validateAuthKey()
     */
    public function getAuthKey()
    {
        return '';
    }

    /**
     * Validates the given auth key.
     *
     * This is required if [[User::enableAutoLogin]] is enabled.
     * @param string $authKey the given auth key
     * @return bool whether the given auth key is valid.
     * @see getAuthKey()
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    


}