<?php

declare(strict_types=1);

namespace KupShop\UserOauthBundle\ResourceOwner;

use HWI\Bundle\OAuthBundle\OAuth\RequestDataStorageInterface;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\AppleResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class CustomAppleResourceOwner extends CustomResourceOwner
{
    public function __construct(
        HttpClientInterface $httpClient,
        HttpUtils $httpUtils,
        array $options,
        string $name,
        RequestDataStorageInterface $storage,
    ) {
        $options['client_id'] = $this->getClientId($options);
        $options['team_id'] = $this->getTeamId($options);
        $options['key_id'] = $this->getKeyId($options);
        $options['auth_key'] = $this->getAuthKey($options);

        parent::__construct($httpClient, $httpUtils, $options, $name, $storage);
    }

    public function createResourceOwner($httpClient, $httpUtils, $options, $name, $storage)
    {
        return new AppleResourceOwner($httpClient, $httpUtils, $options, $name, $storage);
    }

    protected function getClientId(array $options): string
    {
        if (isLocalDevelopment()) {
            return $options['client_id'];
        }

        $settings = \Settings::getDefault();

        return $settings['oauth']['apple']['client_id'] ?? '';
    }

    protected function getTeamId(array $options): string
    {
        if (isLocalDevelopment()) {
            return $options['options']['team_id'];
        }

        $settings = \Settings::getDefault();

        return $settings['oauth']['apple']['team_id'] ?? '';
    }

    protected function getKeyId(array $options): string
    {
        if (isLocalDevelopment()) {
            return $options['options']['key_id'];
        }

        $settings = \Settings::getDefault();

        return $settings['oauth']['apple']['key_id'] ?? '';
    }

    protected function getAuthKey(array $options): string
    {
        if (isLocalDevelopment()) {
            return $options['options']['auth_key'];
        }

        $settings = \Settings::getDefault();

        $path = $settings['oauth']['apple']['auth_key'] ?? '';

        return file_get_contents(substr($path, 1)) ?? '';
    }

    public function getAuthorizationUrl($redirectUri, array $extraParameters = [])
    {
        $extraParameters['skip_tolocalhost'] = true;

        return parent::getAuthorizationUrl($redirectUri, $extraParameters);
    }

    public function getAccessToken(HttpRequest $request, $redirectUri, array $extraParameters = [])
    {
        $extraParameters['skip_tolocalhost'] = true;

        return parent::getAccessToken($request, $redirectUri, $extraParameters);
    }

    public function updateUserData(\User $user, UserResponseInterface $response)
    {
        // TODO: Implement updateUserData() method.
    }
}
