/*
 * Copyright 2020-2030 码匠君<herodotus@aliyun.com>
 *
 * Dante Engine licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Dante Engine 是 Dante Cloud 系统核心组件库，采用 APACHE LICENSE 2.0 开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1. 请不要删除和修改根目录下的LICENSE文件。
 * 2. 请不要删除和修改 Dante Engine 源码头部的版权声明。
 * 3. 请保留源码和相关描述文件的项目出处，作者声明等。
 * 4. 分发源码时候，请注明软件出处 <https://gitee.com/dromara/dante-cloud>
 * 5. 在修改包名，模块名称，项目代码等时，请注明软件出处 <https://gitee.com/dromara/dante-cloud>
 * 6. 若您的项目无法满足以上几点，可申请商业授权
 */

package org.dromara.dante.persistence.sas.jpa.specification;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.dromara.dante.persistence.commons.definition.EnhanceOAuth2AuthorizationService;
import org.dromara.dante.persistence.commons.domain.HerodotusAuthorizationDetails;
import org.dromara.dante.persistence.commons.jackson.OAuth2JacksonProcessor;
import org.dromara.dante.persistence.sas.jpa.converter.AuthorizationToDetailsConverter;
import org.dromara.dante.persistence.sas.jpa.converter.HerodotusToOAuth2AuthorizationConverter;
import org.dromara.dante.persistence.sas.jpa.converter.OAuth2ToHerodotusAuthorizationConverter;
import org.dromara.dante.persistence.sas.jpa.entity.HerodotusAuthorization;
import org.dromara.dante.persistence.sas.jpa.service.HerodotusAuthorizationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>Description: 基于 JPA 的 OAuth2 认证服务 </p>
 *
 * @author : gengwei.zheng
 * @date : 2022/2/25 22:16
 */
public class JpaOAuth2AuthorizationService implements EnhanceOAuth2AuthorizationService {

    private static final Logger log = LoggerFactory.getLogger(JpaOAuth2AuthorizationService.class);

    private final HerodotusAuthorizationService herodotusAuthorizationService;
    private final Converter<HerodotusAuthorization, OAuth2Authorization> toOAuth2Converter;
    private final Converter<OAuth2Authorization, HerodotusAuthorization> toHerodotusConverter;
    private final Converter<HerodotusAuthorization, HerodotusAuthorizationDetails> toDetailsConverter;

    public JpaOAuth2AuthorizationService(HerodotusAuthorizationService herodotusAuthorizationService, RegisteredClientRepository registeredClientRepository) {
        this.herodotusAuthorizationService = herodotusAuthorizationService;

        OAuth2JacksonProcessor jacksonProcessor = OAuth2JacksonProcessor.builder().build();
        this.toOAuth2Converter = new HerodotusToOAuth2AuthorizationConverter(jacksonProcessor, registeredClientRepository);
        this.toHerodotusConverter = new OAuth2ToHerodotusAuthorizationConverter(jacksonProcessor);
        this.toDetailsConverter = new AuthorizationToDetailsConverter();
    }

    @Override
    public void save(OAuth2Authorization authorization) {
        this.herodotusAuthorizationService.save(toEntity(authorization));
    }

    @Transactional
    @Override
    public void remove(OAuth2Authorization authorization) {
        Assert.notNull(authorization, "authorization cannot be null");
        this.herodotusAuthorizationService.deleteById(authorization.getId());
        log.debug("[Herodotus] |- Jpa OAuth2 Authorization Service remove entity.");
        // TODO： 后期还是考虑改为异步任务的形式，先临时放在这里。
        this.herodotusAuthorizationService.clearHistoryToken();
        log.debug("[Herodotus] |- Jpa OAuth2 Authorization Service clear history token.");
    }

    @Override
    public OAuth2Authorization findById(String id) {
        Optional<HerodotusAuthorization> herodotusAuthorization = this.herodotusAuthorizationService.findById(id);
        return herodotusAuthorization.map(this::toObject).orElse(null);
    }

    @Override
    public int findAuthorizationCount(String registeredClientId, String principalName) {
        int count = this.herodotusAuthorizationService.findAuthorizationCount(registeredClientId, principalName);
        log.debug("[Herodotus] |- Jpa OAuth2 Authorization Service findAuthorizationCount.");
        return count;
    }

    @Override
    public List<OAuth2Authorization> findAvailableAuthorizations(String registeredClientId, String principalName) {
        List<HerodotusAuthorization> authorizations = this.herodotusAuthorizationService.findAvailableAuthorizations(registeredClientId, principalName);
        if (CollectionUtils.isNotEmpty(authorizations)) {
            return authorizations.stream().map(this::toObject).collect(Collectors.toList());
        }

        return new ArrayList<>();
    }

    @Override
    public void deleteById(String id) {
        herodotusAuthorizationService.deleteById(id);
    }

    @Override
    public Page<HerodotusAuthorizationDetails> findByPage(int pageNumber, int pageSize, Sort.Direction direction, String... properties) {
        Page<HerodotusAuthorization> pages = this.herodotusAuthorizationService.findByPage(pageNumber, pageSize, direction, properties);
        return pages.map(toDetailsConverter::convert);
    }

    @Override
    public Page<HerodotusAuthorizationDetails> findByPage(int pageNumber, int pageSize) {
        Page<HerodotusAuthorization> pages = this.herodotusAuthorizationService.findByPage(pageNumber, pageSize);
        return pages.map(toDetailsConverter::convert);
    }

    @Override
    public OAuth2Authorization findByToken(String token, OAuth2TokenType tokenType) {
        Assert.hasText(token, "token cannot be empty");

        Optional<HerodotusAuthorization> result;

        if (ObjectUtils.isEmpty(tokenType)) {
            result = this.herodotusAuthorizationService.findByStateOrAuthorizationCodeValueOrAccessTokenValueOrRefreshTokenValueOrOidcIdTokenValueOrUserCodeValueOrDeviceCodeValue(token);
        } else {
            result = switch (tokenType.getValue()) {
                case OAuth2ParameterNames.STATE -> this.herodotusAuthorizationService.findByState(token);
                case OAuth2ParameterNames.CODE -> this.herodotusAuthorizationService.findByAuthorizationCode(token);
                case OAuth2ParameterNames.ACCESS_TOKEN -> this.herodotusAuthorizationService.findByAccessToken(token);
                case OAuth2ParameterNames.REFRESH_TOKEN -> this.herodotusAuthorizationService.findByRefreshToken(token);
                case OidcParameterNames.ID_TOKEN -> this.herodotusAuthorizationService.findByOidcIdTokenValue(token);
                case OAuth2ParameterNames.USER_CODE -> this.herodotusAuthorizationService.findByUserCodeValue(token);
                case OAuth2ParameterNames.DEVICE_CODE ->
                        this.herodotusAuthorizationService.findByDeviceCodeValue(token);
                default -> Optional.empty();
            };
        }

        return result.map(this::toObject).orElse(null);
    }

    private OAuth2Authorization toObject(HerodotusAuthorization entity) {
        return toOAuth2Converter.convert(entity);
    }

    private HerodotusAuthorization toEntity(OAuth2Authorization authorization) {
        return toHerodotusConverter.convert(authorization);
    }
}
