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

import cn.hutool.json.JSONArray;
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.util.List;
import java.util.Map;

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

    @Autowired
    private Esb2FeishuTokenUtil esb2FeishuTokenUtil;

    public String updateBotTodoMessage(String status,String message_id) {
        String update_message_id = "";
        String token = esb2FeishuTokenUtil.getToken();
        log.error("token:"+token);
        if(StringUtils.isNotBlank(token)){
            JSONObject bodyJson = new JSONObject();
            bodyJson.put("message_id",message_id);
            bodyJson.put("status",status);

            JSONObject textsJson = new JSONObject();
            JSONObject i18nResourcesJson = new JSONObject();
            i18nResourcesJson.put("locale","zh-CN");
            i18nResourcesJson.put("texts",textsJson);
            i18nResourcesJson.put("is_default","true");
            JSONArray i18nResourcesArray = new JSONArray();
            i18nResourcesArray.add(i18nResourcesJson);
            bodyJson.put("i18n_resources",i18nResourcesArray);

            String message = doSendPost(bodyJson.toJSONString(),token);
            if(StringUtils.isNotBlank(message)){
                JSONObject returnData = JSONObject.parseObject(message);
                if(returnData.containsKey("code")){
                    String code = returnData.getString("code");
                    if("0".equals(code)){
                        JSONObject dataJson = returnData.getJSONObject("data");
                        if(dataJson.containsKey("message_id")){
                            update_message_id = dataJson.getString("message_id");
                        }
                    }
                }
            }
        }
        return update_message_id;
    }


    public String doSendPost(String bodyData, String token){
        String msg = "";

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, bodyData);

//        RequestBody body = RequestBody.create(mediaType, "{\n    \"message_id\":\"7530608640169000988\",\n    \"status\":\"PROCESSED\",\n    \"i18n_resources\":[\n        {\n            \"locale\":\"zh-CN\",\n            \"is_default\":true,\n            \"texts\":{\n                \n                \n            }\n        }\n    ]\n}");

        try {
            Request request = new Request.Builder()
                    .url(Constants.feishuHost+Constants.updateBotUrl)
                    .method("POST", body)
                    .addHeader("Authorization", "Bearer "+token)
                    .addHeader("Content-Type", "application/json")
                    .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;
    }
}
