<?php

namespace app\api\controller;
use app\model\User as UserModel;
use hg\apidoc\annotation as Apidoc;

/**
 * 验证接口
 */
class ValidateController extends BaseController
{
    public $noNeedLogin = '*';

    /**
     * 检测邮箱是否可用
     *
     * @Apidoc\Method ("POST")
     * @Apidoc\Param("email", type="string",require=true, desc="邮箱")
     * @Apidoc\Param("id", type="string",require=true, desc="排除会员ID")
     */
    public function check_email_available()
    {
        $email = input('email');
        $id = (int)input('id');
        $count = UserModel::where('email', '=', $email)->where('id', '<>', $id)->count();
        if ($count > 0) {
            return $this->error(__('The mailbox is already occupied'));
        }
        return $this->success(__('successful'));
    }

    /**
     * 检测用户名
     *
     * @Apidoc\Method ("POST")
     * @Apidoc\Param("username", type="string",require=true, desc="用户名")
     * @Apidoc\Param("id", type="string",require=true, desc="排除会员ID")
     */
    public function check_username_available()
    {
        $username = input('username');
        $id = (int)input('id');
        $count = UserModel::where('username', '=', $username)->where('id', '<>', $id)->count();
        if ($count > 0) {
            return $this->error(__('Username is already taken'));
        }
        return $this->success(__('successful'));
    }

    /**
     * 检测昵称
     *
     * @Apidoc\Method ("POST")
     * @Apidoc\Param("nickname", type="string",require=true, desc="昵称")
     * @Apidoc\Param("id", type="string",require=true, desc="排除会员ID")
     */
    public function check_nickname_available()
    {
        $nickname = input('nickname');
        $id = (int)input('id');
        $count = UserModel::where('nickname', '=', $nickname)->where('id', '<>', $id)->count();
        if ($count > 0) {
            return $this->error(__('Nickname is already taken'));
        }
        return $this->success(__('successful'));
    }

    /**
     * 检测手机
     *
     * @Apidoc\Method ("POST")
     * @Apidoc\Param("mobile", type="string",require=true, desc="手机号")
     * @Apidoc\Param("id", type="string",require=true, desc="排除会员ID")
     */
    public function check_mobile_available()
    {
        $mobile = input('mobile');
        $id = (int)input('id');
        $count = UserModel::where('mobile', '=', $mobile)->where('id', '<>', $id)->count();
        if ($count > 0) {
            return $this->error(__('Phone Number is already taken'));
        }
        return $this->success(__('successful'));
    }

    /**
     * 检测手机是否存在
     *
     * @Apidoc\Method ("POST")
     * @Apidoc\Param("mobile", type="string",require=true, desc="手机号")
     */
    public function check_mobile_exist()
    {
        $mobile = input('mobile');
        $count = UserModel::where('mobile', '=', $mobile)->count();
        if (!$count) {
            return $this->error(__('Mobile number does not exist'));
        }
        return $this->success(__('successful'));
    }

    /**
     * 检测邮箱是否存在
     *
     * @Apidoc\Method ("POST")
     * @Apidoc\Param("email", type="string",require=true, desc="邮箱")
     */
    public function check_email_exist()
    {
        $email = input('email');
        $count = UserModel::where('email', '=', $email)->count();
        if (!$count) {
            return $this->error(__('Email does not exist'));
        }
        return $this->success(__('successful'));
    }

    /**
     * 检测手机验证码（弃用）
     *
     * @Apidoc\Method ("POST")
     * @Apidoc\Param("mobile", type="string",require=true, desc="手机号")
     * @Apidoc\Param("code", type="string",require=true, desc="验证码")
     * @Apidoc\Param("event", type="string",require=true, desc="事件")
     */
    protected function check_sms_correct()
    {
        $mobile = input('mobile');
        $captcha = input('captcha');
        $event = input('event');
        // if (!\app\common\library\Sms::check($mobile, $captcha, $event)) {
        //     $this->error(__('Incorrect verification code'));
        // }
        return $this->success(__('successful'));
    }

    /**
     * 检测邮箱验证码
     *
     * @Apidoc\Method ("POST")
     * @Apidoc\Param("email", type="string",require=true, desc="邮箱")
     * @Apidoc\Param("code", type="string",require=true, desc="验证码")
     * @Apidoc\Param("event", type="string",require=true, desc="事件")
     */
    public function check_ems_correct()
    {
        $email = input('email');
        $captcha = input('code');
        $event = input('event');
        $cache_key = 'captcha_'.$event.'_'.$email;
        $list  = cache($cache_key);
        $list = $list?:[];
        if(!isset($list[$captcha])){
            return $this->error(__('Incorrect verification code'));
        }
        if($list[$captcha]+5*60 >= time()){
            unset($list[$captcha]);
            cache($cache_key,$list);
            return $this->error(__('Verification code has expired'));
        }
        return $this->success(__('successful'));
    }
}
