package org.sky.auth.config;

import org.sky.auth.handler.KdAuthorizationHandler;
import org.sky.auth.handler.KdPasswordHandler;
import org.sky.auth.handler.KdTokenHandler;
import org.sky.auth.service.KdClientDetailService;
import org.sky.auth.service.KdUserDetailService;
import org.sky.core.jwt.props.JwtProperties;
import org.sky.core.launch.props.KdProperties;
import org.sky.core.oauth2.config.OAuth2AutoConfiguration;
import org.sky.core.oauth2.handler.AuthorizationHandler;
import org.sky.core.oauth2.handler.PasswordHandler;
import org.sky.core.oauth2.handler.TokenHandler;
import org.sky.core.oauth2.props.OAuth2Properties;
import org.sky.core.oauth2.service.OAuth2ClientService;
import org.sky.core.oauth2.service.OAuth2UserService;
import org.sky.core.redis.cache.KdRedis;
import org.sky.core.tenant.KdTenantProperties;
import org.sky.system.feign.IUserClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * KdAuthConfiguration
 *
 */
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(OAuth2AutoConfiguration.class)
public class KdAuthConfiguration {
	@Bean
	public AuthorizationHandler authorizationHandler(KdRedis kdRedis,
													 KdProperties kdProperties,
													 KdTenantProperties tenantProperties,
													 OAuth2Properties oAuth2Properties) {
		return new KdAuthorizationHandler(kdRedis, kdProperties, tenantProperties, oAuth2Properties);
	}

	@Bean
	public PasswordHandler passwordHandler(OAuth2Properties properties) {
		return new KdPasswordHandler(properties);
	}

	@Bean
	public TokenHandler tokenHandler(JwtProperties jwtProperties) {
		return new KdTokenHandler(jwtProperties);
	}

	@Bean
	public OAuth2ClientService oAuth2ClientService(JdbcTemplate jdbcTemplate) {
		return new KdClientDetailService(jdbcTemplate);
	}

	@Bean
	public OAuth2UserService oAuth2UserService(IUserClient userClient) {
		return new KdUserDetailService(userClient);
	}

}
