<?php
namespace App\Http\Controllers\Jkq\Auth;

use Aix\Sms\Contracts\Smser;
use App\Exceptions\ResponseException;
use App\Http\Controllers\Jkq\JkqBaseController;
use App\Services\Auth\AuthService;
use App\Services\Common\EmailService;
use App\Services\Common\SmsService;
use App\Validators\ResetPasswordValidatorRequest;

class ResetPasswordController extends JkqBaseController
{
    /**
     * @var SmsService
     */
    private $smsService;
    /**
     * @var AuthService
     */
    private $authService;
    /**
     * @var EmailService
     */
    private $emailService;


    /**
     * ResetPasswordController constructor.
     * @param SmsService $smsService
     * @param AuthService $authService
     * @param EmailService $emailService
     */
    public function __construct(SmsService $smsService, AuthService $authService, EmailService $emailService)
    {
        $this->smsService = $smsService;
        $this->authService = $authService;
        $this->emailService = $emailService;
    }

    public function passwordRequest()
    {
        return view('jkq.auth.password_request', ['title'=>'重置密码']);
    }

    public function passwordRequestPost(ResetPasswordValidatorRequest $request)
    {
        if ($request->type == 'mobile') {
            if (!$this->smsService->checkAuthSms($request->mobile, Smser::TEMPLATE_AUTH_CHECK, $request->mobile_vcode)) {
                return $this->sendErrorResponse("短信验证码不对");
            }
            $token=$this->authService->resetPasswordToken($request->all());
            return $this->sendSuccessResponse(['url'=>route('jkq.password.reset', ['token'=>$token])]);
        } elseif ($request->type == 'email') {
            $token=$this->authService->resetPasswordToken($request->all());
            $this->emailService->setCallback('App\Services\Auth\AuthService', 'sendEmailHook', [$request->email, $token])
            ->sendAuthMail($request->email, EmailService::TEMPLATE_PASSWORD_RESET);
            return $this->sendSuccessResponse(['url'=>route('jkq.password.reset', ['token'=>$request->email])]);
        }
    }

    public function passwordReset($token)
    {
        $data['token']=$token;
        $data['title']="重置密码";
        if (validator_check($token, 'email')) {
            return view('jkq.auth.password_reset_email', $data);
        }
        if (!$data=$this->authService->checkResetPasswordToken($token)) {
            throw new ResponseException("who are you?", [], 404);
        }
        $data['token']=$token;
        $data['title']="重置密码";
        return view('jkq.auth.password_reset', $data);
    }

    public function passwordResetPost(ResetPasswordValidatorRequest $request, $token)
    {
        if (!$data=$this->authService->checkResetPasswordToken($token)) {
            throw new ResponseException("who are you?", [], 404);
        }
        $utype=$this->authService->resetPassword($token, $request->password);
        $login_url =$utype==1?route('login.company'):route('login');
        return view('jkq.auth.password_reset_success', ['login_url'=>$login_url]);
    }
}