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.IUniRes;
import cn.ibizlab.r7.runtime.client.base.config.BaseClientProperties;
import cn.ibizlab.r7.runtime.core.client.service.IUniResClientService;
import cn.ibizlab.r7.runtime.client.base.model.UniResImpl;
import cn.ibizlab.r7.runtime.client.base.feign.UniResFeignClient;
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;

/**
 * 实体[UniRes] 服务对象接口
 */
@Service
public class UniResClientServiceImpl implements IUniResClientService {

    UniResFeignClient uniResFeignClient;

    @Autowired
    public UniResClientServiceImpl(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.uniResFeignClient = nameBuilder.target(UniResFeignClient.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.uniResFeignClient = nameBuilder.target(UniResFeignClient.class,"http://"+baseClientProperties.getServiceUrl()+"/") ;
		}
    }
    @Override
    public IUniRes createModel() {
		return new UniResImpl();
	}


    public void remove(IUniRes unires){
        uniResFeignClient.remove(unires.getUniResId()) ;
    }


    public void save(IUniRes unires){
        IUniRes clientModel = uniResFeignClient.save(unires.getUniResId(),(UniResImpl)unires) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), unires.getClass(), false);
        copier.copy(clientModel, unires, null);
    }


    public void update(IUniRes unires){
        IUniRes clientModel = uniResFeignClient.update(unires.getUniResId(),(UniResImpl)unires) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), unires.getClass(), false);
        copier.copy(clientModel, unires, null);
    }


    public void get(IUniRes unires){
        IUniRes clientModel = uniResFeignClient.get(unires.getUniResId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), unires.getClass(), false);
        copier.copy(clientModel, unires, null);
    }


    public void create(IUniRes unires){
        IUniRes clientModel = uniResFeignClient.create((UniResImpl)unires) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), unires.getClass(), false);
        copier.copy(clientModel, unires, null);
    }


    public void getDraft(IUniRes unires){
        IUniRes clientModel = uniResFeignClient.getDraft(unires.getUniResId(),(UniResImpl)unires) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), unires.getClass(), false);
        copier.copy(clientModel, unires, null);
    }


    public void getResource(IUniRes unires){
        IUniRes clientModel = uniResFeignClient.getResource(unires.getUniResId(),(UniResImpl)unires) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), unires.getClass(), false);
        copier.copy(clientModel, unires, null);
    }


    public void checkKey(IUniRes unires){
        IUniRes clientModel = uniResFeignClient.checkKey(unires.getUniResId(),(UniResImpl)unires) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), unires.getClass(), false);
        copier.copy(clientModel, unires, null);
    }


    public Page<IUniRes> fetchSelectUniresAll(SearchContext context){
        Page<UniResImpl> page = this.uniResFeignClient.fetchSelectUniresAll(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }


    public Page<IUniRes> fetchDefault(SearchContext context){
        Page<UniResImpl> page = this.uniResFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

