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.IUser;
import cn.ibizlab.r7.runtime.client.base.config.BaseClientProperties;
import cn.ibizlab.r7.runtime.core.client.service.IUserClientService;
import cn.ibizlab.r7.runtime.client.base.model.UserImpl;
import cn.ibizlab.r7.runtime.client.base.feign.UserFeignClient;
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;

/**
 * 实体[User] 服务对象接口
 */
@Service
public class UserClientServiceImpl implements IUserClientService {

    UserFeignClient userFeignClient;

    @Autowired
    public UserClientServiceImpl(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.userFeignClient = nameBuilder.target(UserFeignClient.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.userFeignClient = nameBuilder.target(UserFeignClient.class,"http://"+baseClientProperties.getServiceUrl()+"/") ;
		}
    }
    @Override
    public IUser createModel() {
		return new UserImpl();
	}


    public void remove(IUser user){
        userFeignClient.remove(user.getUserId()) ;
    }


    public void create(IUser user){
        IUser clientModel = userFeignClient.create((UserImpl)user) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), user.getClass(), false);
        copier.copy(clientModel, user, null);
    }


    public void syncToUser(IUser user){
        IUser clientModel = userFeignClient.syncToUser(user.getUserId(),(UserImpl)user) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), user.getClass(), false);
        copier.copy(clientModel, user, null);
    }


    public void checkKey(IUser user){
        IUser clientModel = userFeignClient.checkKey(user.getUserId(),(UserImpl)user) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), user.getClass(), false);
        copier.copy(clientModel, user, null);
    }


    public void get(IUser user){
        IUser clientModel = userFeignClient.get(user.getUserId()) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), user.getClass(), false);
        copier.copy(clientModel, user, null);
    }


    public void save(IUser user){
        IUser clientModel = userFeignClient.save(user.getUserId(),(UserImpl)user) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), user.getClass(), false);
        copier.copy(clientModel, user, null);
    }


    public void getDraft(IUser user){
        IUser clientModel = userFeignClient.getDraft(user.getUserId(),(UserImpl)user) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), user.getClass(), false);
        copier.copy(clientModel, user, null);
    }


    public void update(IUser user){
        IUser clientModel = userFeignClient.update(user.getUserId(),(UserImpl)user) ;
        BeanCopier copier=BeanCopier.create(clientModel.getClass(), user.getClass(), false);
        copier.copy(clientModel, user, null);
    }


    public Page<IUser> fetchDefault(SearchContext context){
        Page<UserImpl> page = this.userFeignClient.fetchDefault(context) ;
        return new PageImpl(page.getContent(),context.getPageable(),page.getTotalElements());
    }



}

