package cn.ibizlab.r7.runtime.client.base.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import cn.ibizlab.r7.runtime.core.client.model.IApplication;
import cn.ibizlab.r7.runtime.client.base.config.BaseClientProperties;
import cn.ibizlab.r7.runtime.core.client.service.IApplicationClientService;
import cn.ibizlab.r7.runtime.client.base.model.ApplicationImpl;
import cn.ibizlab.r7.runtime.client.base.feign.ApplicationFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import cn.ibizlab.r7.runtime.util.feign.FeignRequestInterceptor;
import cn.ibizlab.r7.runtime.util.SearchContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.r7.runtime.util.feign.suport.SearchContextFeignEncode;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;

/**
 * 实体[Application] 服务对象接口
 */
@Service
public class ApplicationClientServiceImpl implements IApplicationClientService {

    ApplicationFeignClient applicationFeignClient;

    @Autowired
    public ApplicationClientServiceImpl(Decoder decoder, Encoder encoder, Client client, Contract contract ,FeignRequestInterceptor feignRequestInterceptor,BaseClientProperties baseClientProperties) {
        if (baseClientProperties.getServiceId()!=null) {
    		Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.applicationFeignClient = nameBuilder.target(ApplicationFeignClient.class,"http://"+baseClientProperties.getServiceId()+"/") ;
		}else if (baseClientProperties.getServiceUrl()!=null) {
			if (client instanceof LoadBalancerFeignClient) {
				client = ((LoadBalancerFeignClient) client).getDelegate();
			}
			Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.applicationFeignClient = nameBuilder.target(ApplicationFeignClient.class,"http://"+baseClientProperties.getServiceUrl()+"/") ;
		}
    }
    @Override
    public IApplication createModel() {
		return new ApplicationImpl();
	}


    public void remove(IApplication application){
        applicationFeignClient.remove(application.getApplicationId()) ;
    }


    public void checkKey(IApplication application){
        IApplication clientModel = applicationFeignClient.checkKey(application.getApplicationId(),(ApplicationImpl)application) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), application.getClass(), false);
        copier.copy(clientModel, application, null);
    }


    public void save(IApplication application){
        IApplication clientModel = applicationFeignClient.save(application.getApplicationId(),(ApplicationImpl)application) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), application.getClass(), false);
        copier.copy(clientModel, application, null);
    }


    public void getDraft(IApplication application){
        IApplication clientModel = applicationFeignClient.getDraft(application.getApplicationId(),(ApplicationImpl)application) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), application.getClass(), false);
        copier.copy(clientModel, application, null);
    }


    public void update(IApplication application){
        IApplication clientModel = applicationFeignClient.update(application.getApplicationId(),(ApplicationImpl)application) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), application.getClass(), false);
        copier.copy(clientModel, application, null);
    }


    public void get(IApplication application){
        IApplication clientModel = applicationFeignClient.get(application.getApplicationId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), application.getClass(), false);
        copier.copy(clientModel, application, null);
    }


    public void create(IApplication application){
        IApplication clientModel = applicationFeignClient.create((ApplicationImpl)application) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), application.getClass(), false);
        copier.copy(clientModel, application, null);
    }


    public Page<IApplication> fetchDefault(SearchContext context){
        Page<ApplicationImpl> page = this.applicationFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

