<?php

namespace KupShop\UserOauthBundle\Security;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;

class AccessDeniedHandler implements AccessDeniedHandlerInterface
{
    public function handle(Request $request, AccessDeniedException $accessDeniedException): ?Response
    {
        if ($accessDeniedException instanceof OauthBindFailedException) {
            addUserMessage(translate('oauth', 'login')['bind_failed']);

            return new RedirectResponse(path('settings'));
        } elseif ($accessDeniedException instanceof OauthBindRefusedException) {
            addUserMessage(translate('oauth', 'login')['bind_refused']);

            return new RedirectResponse(path('settings'));
        } elseif ($accessDeniedException instanceof OauthBindSuccessException) {
            return new RedirectResponse(path('settings', ['wpj_utm' => 'auth']));
        } elseif ($accessDeniedException instanceof ShouldLogOutException) {
            addUserMessage(translate('oauth', 'login')['should_logout']);

            return new RedirectResponse(path('account'));
        }

        return null;
    }
}
