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

import com.alibaba.fastjson.JSONObject;
import com.weaver.seconddev.chapanda.feishu.constant.Constants;
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;
import java.text.ParseException;
import java.text.SimpleDateFormat;


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

    @Autowired
    Esb2FeishuTokenUtil esb2FeishuTokenUtil;

    public String deleteCalendarTimeOff(String timeoff_event_id) {

        String code = "";
        String token = esb2FeishuTokenUtil.getToken();
        log.error("token:"+token);
        if(StringUtils.isNotBlank(token)){
            String message = doHttpPost(timeoff_event_id,token);
            if(StringUtils.isNotBlank(message)){
                JSONObject returnData = JSONObject.parseObject(message);
                if(returnData.containsKey("code")){
                    code = returnData.getString("code");
                }
            }
        }
        return code;
    }

    public String doHttpPost(String timeoff_event_id,String token){
        String msg = "";
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
//        MediaType mediaType = MediaType.parse("text/plain");
//        RequestBody body = RequestBody.create(mediaType, "");
        try {

            Request request = new Request.Builder()
                    .url(Constants.feishuHost+Constants.deleteTimeoffUrl+timeoff_event_id)
                    .method("DELETE", null)
                    .addHeader("Authorization", "Bearer "+token)
                    .build();
            Response response = client.newCall(request).execute();
            int code = response.code();
            log.error("code:{}",code);
            msg = response.body().string();
            log.error("msg:"+msg);
        } catch (IOException e) {
            log.error("e:"+e);
            throw new RuntimeException(e);
        }
        return msg;
    }



}
