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.BeisenStaffDepartmentDto;
import com.weaver.seconddev.chapanda.beisen.entity.BeisenStaffDto;
import com.weaver.seconddev.chapanda.beisen.entity.BeisenStaffPositionDto;
import okhttp3.*;
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 BeisenStaffInfoByStaffCodeUtil {
    private final static Logger log = LoggerFactory.getLogger(BeisenStaffInfoByStaffCodeUtil.class);

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;

    public BeisenStaffDto getBeisenStaffInfoByStaffCode(String staffcode){
        BeisenStaffDto beisenStaffDto = new BeisenStaffDto();
        if(StringUtils.isNotEmpty(staffcode)){
            String token = token2BeiSenUtil.getToken();
            log.error("token:{}",token);
            if(StringUtils.isNotBlank(token)){
                String bodyData = queryUserInfoByStaffCode(token,staffcode);
                log.error("bodyData:{}",bodyData);
                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)){
                            String items = bodyJson.getString("items");
                            if(!"[]".equals(items)){
                                JSONArray itemsArray = bodyJson.getJSONArray("items");
                                log.error("itemsArray:{}",itemsArray.size());
                                if(itemsArray.size() > 0){

                                    JSONObject itemObject = itemsArray.getJSONObject(0);
                                    log.error("itemObject:{}",itemObject.size());

                                    if(itemObject.containsKey("staffDto")){
                                        JSONObject staffDto = itemObject.getJSONObject("staffDto");
                                        log.error("staffDto:{}",staffDto.size());
                                        beisenStaffDto.setDepartmentId(staffDto.getString("departmentId"));
                                        beisenStaffDto.setDepartmentName(staffDto.getString("departmentName"));
                                        String employedDate = staffDto.getString("employedDate");
                                        log.error("employedDate:{}",employedDate);
                                        if(!"null".equals(employedDate) && employedDate !=null ){
                                            if(employedDate.contains("T")){
                                                employedDate = employedDate.substring(0,employedDate.indexOf("T"));
                                            }
                                        }

                                        log.error("employedDate:{}",employedDate);

                                        beisenStaffDto.setEmployedDate(employedDate);

                                        beisenStaffDto.setJobCode(staffDto.getString("jobCode"));
                                        beisenStaffDto.setPositionName(staffDto.getString("positionName"));
                                        beisenStaffDto.setLineManagerId(staffDto.getString("lineManagerId"));
                                        beisenStaffDto.setStaffCode(staffDto.getString("staffCode"));
                                        beisenStaffDto.setStaffStatus(staffDto.getString("staffStatus"));
                                        beisenStaffDto.setIsDelete(staffDto.getString("isDelete"));
                                        String useridStr = staffDto.getString("userId");
                                        int userid = -1 ;
                                        if(StringUtils.isNotBlank(useridStr)){
                                            try{
                                                userid = Integer.valueOf(useridStr);
                                            }catch (Exception e){

                                            }
                                        }
                                        beisenStaffDto.setUserId(userid);

                                        beisenStaffDto.setTenantId(staffDto.getString("tenantId"));
                                        beisenStaffDto.setEmail(staffDto.getString("email"));
                                        beisenStaffDto.setUserType(staffDto.getString("userType"));
                                        beisenStaffDto.setUserStatus(staffDto.getString("userStatus"));
                                        beisenStaffDto.setOrigin(staffDto.getString("origin"));
                                        beisenStaffDto.setOriginalId(staffDto.getString("originalId"));
                                        beisenStaffDto.setName(staffDto.getString("name"));
                                        beisenStaffDto.setEnglishName(staffDto.getString("englishName"));
                                        beisenStaffDto.setSex(staffDto.getString("sex"));
                                        String birthday =  staffDto.getString("birthday");

                                        if(!"null".equals(birthday) && birthday !=null ){
                                            if(birthday.contains("T")){
                                                birthday = birthday.substring(0,birthday.indexOf("T"));
                                            }
                                        }

                                        beisenStaffDto.setBirthday(birthday);
                                        beisenStaffDto.setMobile(staffDto.getString("mobile"));
                                        beisenStaffDto.setIdCardType(staffDto.getString("idCardType"));

                                        String graduateDate = staffDto.getString("graduateDate");

                                        if(!"null".equals(graduateDate) && graduateDate !=null ){
                                            if(graduateDate.contains("T")){
                                                graduateDate = graduateDate.substring(0,graduateDate.indexOf("T"));
                                            }
                                        }


                                        log.error("graduateDate:{}",graduateDate);
                                        beisenStaffDto.setGraduateDate(graduateDate);

                                        beisenStaffDto.setNativePlace(staffDto.getString("nativePlace"));
                                        String beganWorkDate = staffDto.getString("beganWorkDate");

                                        if(!"null".equals(beganWorkDate) && beganWorkDate !=null ){
                                            if(beganWorkDate.contains("T")){
                                                beganWorkDate = beganWorkDate.substring(0,beganWorkDate.indexOf("T"));
                                            }
                                        }

                                        log.error("graduateDate:{}",graduateDate);
                                        beisenStaffDto.setBeganWorkDate(beganWorkDate);
                                        beisenStaffDto.setGraduateFrom(staffDto.getString("graduateFrom"));
                                        beisenStaffDto.setMajor(staffDto.getString("major"));
                                        beisenStaffDto.setPositionCategory(staffDto.getString("positionCategory"));
                                    }

                                    BeisenStaffDepartmentDto beisenStaffDepartmentDto = new BeisenStaffDepartmentDto();
                                    if(itemObject.containsKey("departmentDto")){
                                        JSONObject departmentDto = itemObject.getJSONObject("departmentDto");
                                        beisenStaffDepartmentDto.setId(departmentDto.getString("id"));
                                        beisenStaffDepartmentDto.setName(departmentDto.getString("name"));
                                        beisenStaffDepartmentDto.setCode(departmentDto.getString("code"));
                                        beisenStaffDepartmentDto.setAbbreviation(departmentDto.getString("abbreviation"));
                                        beisenStaffDepartmentDto.setParentId(departmentDto.getString("parentId"));
                                        beisenStaffDepartmentDto.setLevel(departmentDto.getString("level"));
                                        beisenStaffDepartmentDto.setStatus(departmentDto.getString("status"));
                                        beisenStaffDepartmentDto.setIsDelete(departmentDto.getString("isDelete"));
                                        beisenStaffDepartmentDto.setOriginalId(departmentDto.getString("originalId"));
                                    }
                                    beisenStaffDto.setBeisenStaffDepartmentDto(beisenStaffDepartmentDto);

                                    BeisenStaffPositionDto beisenStaffPositionDto = new BeisenStaffPositionDto();
                                    if(itemObject.containsKey("positionDto")){
                                        JSONObject positionDto = itemObject.getJSONObject("positionDto");

                                        beisenStaffPositionDto.setId(positionDto.getString("id"));
                                        beisenStaffPositionDto.setName(positionDto.getString("name"));
                                        beisenStaffPositionDto.setCode(positionDto.getString("code"));
                                        beisenStaffPositionDto.setStatus(positionDto.getString("status"));
                                        beisenStaffPositionDto.setIsDelete(positionDto.getString("isDelete"));
                                    }
                                    beisenStaffDto.setBeisenStaffPositionDto(beisenStaffPositionDto);
                                }
                            }
                        }
                    }
                }
            }
        }
        return beisenStaffDto;
    }


    public String queryUserInfoByStaffCode(String token,String staffcode){
        String bodyData = "";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        try {
            Request request = new Request.Builder()
                    .url(Constants.beishenHost+Constants.userInfoUrl+"?staffCode="+staffcode)
                    .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();
            }
        } catch (IOException e) {
            log.error("e:"+e);
            throw new RuntimeException(e);
        }
        return bodyData;
    }

}
