<?php

namespace App\Http\Controllers\Web\Auth;

use App\Http\Controllers\Web\WebBaseController;
use App\Services\Auth\AuthService;
use App\Services\Common\EmailService;
use App\Services\Common\TaskService;
use App\Services\Company\CompanyService;
use App\Services\Organization\OrganizationService;
use App\Services\Person\MemberInfoService;

class EmailController extends WebBaseController
{
    /**
     * @var EmailService
     */
    protected $emailService;
    /**
     * @var CompanyService
     */
    private $companyService;

    private $memberInfoService;
    /**
     * @var AuthService
     */
    private $authService;
    private $taskService;

    private $organizationService;

    /**
     * EmailController constructor.
     * @param EmailService $emailService
     * @param CompanyService $companyService
     * @param MemberInfoService $memberInfoService
     * @param AuthService $authService
     * @param TaskService $taskService
     */
    public function __construct(EmailService $emailService, TaskService $taskService, CompanyService $companyService, MemberInfoService $memberInfoService, AuthService $authService, OrganizationService $organizationService)
    {
        $this->emailService = $emailService;
        $this->companyService = $companyService;
        $this->memberInfoService = $memberInfoService;
        $this->authService = $authService;
        $this->taskService = $taskService;
        $this->organizationService = $organizationService;
    }


    public function checkEmail($token, $tag = 'none')
    {
        $message = $this->emailService->checkAuthEmail($token);
        if (is_null($message)) {
            //  return "地址已失效";//token不对
            return $this->showMessage("地址已失效", route('home'), true, "首页");
        }
        //token验证是对的
        if ($message['alias']==EmailService::TEMPLATE_VALIDATION) {
            //验证邮箱逻辑
            if ($tag == 'company') {
                if ($this->companyService->verifyEmail($message['email'])) {
                    $this->taskService->doTask(23);
                    return $this->showMessage("邮箱认证成功!", route('home'), false, "首页");
                }
            }
            if ($tag == 'organization') {
                if ($this->organizationService->verifyEmail($message['email'])) {
                    //$this->taskService->doTask(23);
                    return $this->showMessage("邮箱认证成功!", route('home'), false, "首页");
                }
            }
            if ($tag == 'personal') {
                if ($this->memberInfoService->verifyEmail($message['email'])) {
                    return $this->showMessage("邮箱认证成功!", route('home'), false, "首页");
                }
            }
        }

        if ($message['alias']==EmailService::TEMPLATE_PASSWORD_RESET) {
            //重置密码逻辑
            $token=$this->authService->getTokenByEmail($message['email']);
            return view('app.auth.password_reset', ['token'=>$token]);
        }

        return $this->showMessage("地址已失效", route('home'), true, "首页");
    }
}