
package com.sinosig.web.module.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
import org.springframework.stereotype.Service;

import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;

import com.alibaba.fastjson.JSONObject;

import org.springframework.util.DigestUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.sinosig.util.feign.suport.SearchContextFeignEncode;
import com.sinosig.web.module.domain.DBD;
import com.sinosig.web.module.filter.*;
import com.sinosig.web.module.feign.DBDFeignClient;
import com.sinosig.web.WebApplication.WebClientProperties;
import com.sinosig.util.feign.FeignRequestInterceptor;

@Service
public class DBDService {

    DBDFeignClient client;

    @Autowired
    public DBDService(Decoder decoder, Encoder encoder, Client client, Contract contract, FeignRequestInterceptor feignRequestInterceptor,
                                    WebClientProperties webClientProperties) {
        if (webClientProperties.getServiceId()!=null) {
    		Feign.Builder nameBuilder = Feign.builder()
                    .client(client)
                    .encoder(new SearchContextFeignEncode(encoder))
                    .decoder(decoder)
                    .contract(contract)
                    .requestInterceptor(feignRequestInterceptor)
                    ;
        	this.client = nameBuilder.target(DBDFeignClient.class,"http://" + webClientProperties.getServiceId() + "/") ;
		} else if (webClientProperties.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.client = nameBuilder.target(DBDFeignClient.class, "http://" + webClientProperties.getServiceUrl() + "/") ;
		}
    }

    public DBD save(String dbd_id, DBD dbd) {
        return client.save(dbd_id, dbd);
    }

    public DBD update(String dbd_id, DBD dbd) {
        return client.update(dbd_id, dbd);
    }

    public DBD getDraft(String dbd_id, DBD dbd) {
        return client.getDraft(dbd_id, dbd);
    }

	public DBD create(DBD dbd) {
        return client.create(dbd);
    }

    public boolean checkKey(DBD dbd) {
        return client.checkKey(dbd);
    }

    public boolean remove( String dbd_id) {
        return client.remove( dbd_id);
    }

    public DBD get( String dbd_id) {
        return client.get( dbd_id);
    }

	public Page<DBD> fetchDefault(DBDSearchContext searchContext) {
        return client.fetchDefault(searchContext);
    }


}
