package com.weaver.seconddev.chapanda.beisen.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;
import com.weaver.seconddev.chapanda.beisen.entity.BeisenDepartmentDto;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
public class BeisenDeptInfoByOriginalIdUtil {
    private final static Logger log = LoggerFactory.getLogger(BeisenDeptInfoByOriginalIdUtil.class);

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;

    public BeisenDepartmentDto getBeisenDeptDataByOriginalId(String originalId){
        BeisenDepartmentDto beisenDepartmentDto = new BeisenDepartmentDto();
        if(StringUtils.isNotEmpty(originalId)){
            String token = token2BeiSenUtil.getToken();
            log.error("token:{}",token);
            if(StringUtils.isNotBlank(token)){
                String bodyData = queryDeptDataByOriginalId(token,originalId);
                if(StringUtils.isNotBlank(bodyData)){
                    JSONObject bodyJson = JSONObject.parseObject(bodyData);
                    if(bodyJson.containsKey("code")){
                        String code = bodyJson.getString("code");
                        log.error("code:{}",code);
                        if("200".equals(code)){
                            if(bodyJson.containsKey("items")){
                                String items = bodyJson.getString("items");
                                if(!"[]".equals(items)){
                                    JSONArray itemsArray = bodyJson.getJSONArray("items");
                                    if(itemsArray.size() > 0){
                                        JSONObject itemObject = itemsArray.getJSONObject(0);
                                        log.error("itemsArray:{}",itemsArray.size());
                                        log.error("itemObject:{}",itemObject.toJSONString());

                                        log.error("orgId:{}",itemObject.getString("orgId"));
                                        int orgId = -1;
                                        String orgIdStr = itemObject.getString("orgId");
                                        if(StringUtils.isNotBlank(orgIdStr)){
                                            orgId = Integer.valueOf(orgIdStr);
                                        }
                                        beisenDepartmentDto.setOrgId(orgId);

                                        beisenDepartmentDto.setName(itemObject.getString("name"));
                                        beisenDepartmentDto.setCode(itemObject.getString("code"));

                                        beisenDepartmentDto.setAbbreviation(itemObject.getString("abbreviation"));
                                        beisenDepartmentDto.setParentOriginalId(itemObject.getString("parentOriginalId"));
                                        beisenDepartmentDto.setOriginalId(itemObject.getString("originalId"));
                                        beisenDepartmentDto.setOrderId(itemObject.getString("orderId"));
                                        beisenDepartmentDto.setStatus(itemObject.getString("status"));
                                        beisenDepartmentDto.setIsDelete(itemObject.getString("isDelete"));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return beisenDepartmentDto;
    }


    public String queryDeptDataByOriginalId(String token,String originalId){
        String bodyData = "";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+Constants.deppartmentUrl+"?originalId="+originalId)
                    .method("GET", null)
                    .addHeader("Authorization", "Bearer "+token)
                    .build();
            Response response = client.newCall(request).execute();

            int code = response.code();
            log.error("code:{}",code);

            if(code == 200) {
                bodyData = response.body().string();
            }
            log.error("bodyData:{}",bodyData);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return bodyData;
    }

}
