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

import com.google.common.base.Strings;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.neusoft.smart.pos.redis.bean.UserInfo;
import com.neusoft.smart.pos.utils.UserCacheUtils;

import java.util.Map;
import java.util.stream.Collectors;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVLET_30_WRAPPER_FILTER_ORDER;

//@Component
public class XAuthFilter extends ZuulFilter {

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

    @Override
    public String filterType() {
        return PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return SERVLET_30_WRAPPER_FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        final Map<String, String> zuulRequestHeaders = ctx.getZuulRequestHeaders();
        final String xAuthUsername = zuulRequestHeaders.get("x-auth-username");
        return !Strings.isNullOrEmpty(xAuthUsername) && !ctx.getRequest().getRequestURI().startsWith("/service-user/user/loadUserData2Redis");
    }

    @Override
    public Object run() {
        final RequestContext ctx = RequestContext.getCurrentContext();
        final Map<String, String> zuulRequestHeaders = ctx.getZuulRequestHeaders();
        final String xAuthUsername = zuulRequestHeaders.get("x-auth-username");
        final String xAuthUserid = zuulRequestHeaders.get("x-auth-userid");
        if (!Strings.isNullOrEmpty(xAuthUserid)) {
            ctx.addZuulRequestHeader("x-auth-userid", xAuthUserid);
            ctx.addZuulRequestHeader("x-auth-orgid", zuulRequestHeaders.get("x-auth-orgid"));
            ctx.addZuulRequestHeader("x-auth-storeids", zuulRequestHeaders.get("x-auth-storeids"));
        } else {
            UserInfo remoteUser = UserCacheUtils.getUserInfo(xAuthUsername);
            ctx.addZuulRequestHeader("x-auth-userid", String.valueOf(remoteUser.getId()));
            ctx.addZuulRequestHeader("x-auth-userid", String.valueOf(remoteUser.getId()));
            ctx.addZuulRequestHeader("x-auth-orgid", String.valueOf(remoteUser.getOrgId()));
            ctx.addZuulRequestHeader("x-auth-orgid", String.valueOf(remoteUser.getOrgId()));

            String shops = remoteUser.getStoreIds() == null ? ""
                    : remoteUser.getStoreIds().stream().map(String::valueOf).collect(Collectors.joining(","));
            ctx.addZuulRequestHeader("x-auth-storeids", shops);
        }
        return null;
    }

//    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;
//    }
}
