<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "admins".
 *
 * @property int $id
 * @property string $email
 * @property string $username
 * @property string $password
 */
class Admins extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface {
    
    const SCENARIO_LOGIN = 'login';
    const SCENARIO_REGISTER = 'register';
    const SCENARIO_RESET_PASSWORD = 'reset_password';
    public $confirm_password;
    
    function generateRandomString($length) {
        return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@$%', ceil($length/strlen($x)) )),1,$length);
    }



    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'admins';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['email', 'username', 'password'], 'required'],
            [['confirm_password'], 'required', 'on' => self::SCENARIO_REGISTER],
            [['confirm_password'], 'required', 'on' => self::SCENARIO_RESET_PASSWORD],
            [['email'], 'email'],
            [['email', 'username'], 'unique'],
            [['confirm_password'], 'compare', 'compareAttribute' => 'password'],
            [['email', 'username'], 'string', 'max' => 30],
            [['password'], 'string', 'max' => 150],
            [['reset_password_code'],'string', 'max' => 7,],
            [['reset_password_token'],'string', 'max' => 60,],
        ];
    }

    public function scenarios(){
		$scenarios = parent::scenarios();
		$scenarios[self::SCENARIO_REGISTER] = ['email', 'username', 'password', 'confirm_password'];
        $scenarios[self::SCENARIO_LOGIN] = ['username', 'password'];
        $scenarios[self::SCENARIO_RESET_PASSWORD] = ['password', 'confirm_password'];
		return $scenarios;
	}
	
	public static function findIdentity($id)
	{
		return static::findOne($id);
	}
	
	public static function findIdentityByAccessToken($token, $type = null)
	{
		return static::findOne(['access_token' => $token]);
	}
	
	public function getId()
	{
		return $this->id;
	}
	
	public function getAuthKey()
	{
		return $this->authKey;
	}
	
	public function validateAuthKey($authKey)
	{
	    return $this->authKey === $authKey;
	}

	public function beforeSave($insert){
		if($this->scenario == self::SCENARIO_REGISTER || $this->scenario == self::SCENARIO_RESET_PASSWORD){
            $hash = \Yii::$app->getSecurity()->generatePasswordHash($this->password);
            $this->password = $hash;
        }
        return parent::beforeSave($insert);
	}

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'email' => Yii::t('app', 'Email'),
            'username' => Yii::t('app','Username'),
            'password' => Yii::t('app', 'Password'),
            'reset_password_code' => Yii::t('app', 'Reset code'),
            'reset_password_token' => Yii::t('app', 'Reset password token'),
            'confirm_password' => Yii::t('app', 'Confirm Password'),
        ];
    }
}
