package com.neusoft.smart.pos.gateway.filters;

import com.netflix.zuul.context.RequestContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

@Component
public class FeignHeaderRequestInterceptor implements RequestInterceptor {

//    @Autowired
//    RemoteUserService remoteUserService;
//    @Autowired
//    RemoteOrgService remoteOrgService;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        final RequestContext ctx = RequestContext.getCurrentContext();
        final Map<String, String> zuulRequestHeaders = ctx.getZuulRequestHeaders();
        zuulRequestHeaders.forEach((key, value) -> {
            final Map<String, Collection<String>> headers = requestTemplate.headers();
            if (headers.containsKey(key)) headers.remove(key);
            requestTemplate.header(key, value);
        });
    }

//    private String getShopStream(User user) {
//        String shopStream;
//        switch (user.getRole()) {
//            case "user":
//                List<Shop> result = remoteOrgService.getMyShops().getResult();
//                shopStream = result == null ? ""
//                        : result.stream().mapToInt(Shop::getId)
//                        .mapToObj(String::valueOf).collect(Collectors.joining(","));
//                break;
//            case "admin":
//                List<PostShopResponse> result1 = remoteOrgService.queryShopListByOrgId(user.getOrg(), 0).getResult();
//                shopStream = result1 == null ? ""
//                        : result1.stream().mapToInt(PostShopResponse::getId).mapToObj(String::valueOf)
//                        .collect(Collectors.joining(","));
//                break;
//            default:
//                shopStream = "";
//                break;
//        }
//        return shopStream;
//    }
}
