/*
 * Copyright 2013-2018 the original author or authors.
 *
 * 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
 *
 *      https://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.
 */
package com.alibaba.cloud.seata.feign;

import feign.Client;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
import org.springframework.cloud.openfeign.loadbalancer.RetryableFeignBlockingLoadBalancerClient;

/**
 * 重写SeataFeignObjectWrapper以适配最新API
 *
 * @author Chill
 */
public class SeataFeignObjectWrapper {

	private final BeanFactory beanFactory;
	private final LoadBalancedRetryFactory loadBalancedRetryFactory;
	private final LoadBalancerProperties properties;
	private final LoadBalancerClientFactory loadBalancerClientFactory;

	private SpringClientFactory springClientFactory;

	SeataFeignObjectWrapper(BeanFactory beanFactory, LoadBalancedRetryFactory loadBalancedRetryFactory, LoadBalancerProperties properties, LoadBalancerClientFactory loadBalancerClientFactory) {
		this.beanFactory = beanFactory;
		this.loadBalancedRetryFactory = loadBalancedRetryFactory;
		this.properties = properties;
		this.loadBalancerClientFactory = loadBalancerClientFactory;
	}

	Object wrap(Object bean) {
		if (bean instanceof Client && !(bean instanceof SeataFeignClient)) {
			if (bean instanceof FeignBlockingLoadBalancerClient) {
				FeignBlockingLoadBalancerClient client = (FeignBlockingLoadBalancerClient) bean;
				return new SeataFeignBlockingLoadBalancerClient(client.getDelegate(),
					beanFactory.getBean(BlockingLoadBalancerClient.class), this, properties, loadBalancerClientFactory);
			}
			if (bean instanceof RetryableFeignBlockingLoadBalancerClient) {
				RetryableFeignBlockingLoadBalancerClient client = (RetryableFeignBlockingLoadBalancerClient) bean;
				return new SeataRetryableFeignBlockingLoadBalancerClient(client.getDelegate(),
					beanFactory.getBean(BlockingLoadBalancerClient.class), this, loadBalancedRetryFactory, properties, loadBalancerClientFactory);
			}
			return new SeataFeignClient(this.beanFactory, (Client) bean);
		}
		return bean;
	}

	SpringClientFactory clientFactory() {
		if (this.springClientFactory == null) {
			this.springClientFactory = this.beanFactory
				.getBean(SpringClientFactory.class);
		}
		return this.springClientFactory;
	}

}
