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

import com.alibaba.fastjson.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.HashMap;
import java.util.Map;


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

    @Autowired
    Esb2FeishuTokenUtil esb2FeishuTokenUtil;

    public String queryUserCalendarPrimary() {
        String calendar_id = "";
        log.error("Esb2FeishuGetCalendarPrimaryUtil start");
        String token = esb2FeishuTokenUtil.getToken();
        log.error("token:"+token);
        if(StringUtils.isNotBlank(token)){

            String message = doHttpPost(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");
                        JSONArray calendarsArray = dataJson.getJSONArray("calendars");
                        for(int i=0;i<calendarsArray.size();i++){
                            JSONObject calendarJson = calendarsArray.getJSONObject(i);

                            if(calendarJson.containsKey("calendar")){
                                JSONObject calendarObject = calendarJson.getJSONObject("calendar");
                                String type = calendarObject.getString("type");
                                if("primary".equals(type)){
                                    calendar_id = calendarObject.getString("calendar_id");
                                }
                            }
                        }
                    }
                }
            }
        }
        return calendar_id;
    }

    public String doHttpPost(String token){
        String msg = "";

        // https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=open_id
        // t-g1046ofDRKNCIEO3YKB42MQCDBZCYZGWAROROPGJ
//        bodyData = "{\n  \"receive_id\": \"ou_f2ee4ea6545e537c426fa058c31bab88\",\n  \"msg_type\": \"interactive\",\n  \"content\": \"{\\\"type\\\":\\\"template\\\",\\\"data\\\":{\\\"template_id\\\":\\\"AAqIMF4quXR02\\\",\\\"template_version_name\\\":\\\"1.0.1\\\",\\\"template_variable\\\":{\\\"name\\\":\\\"王文猛\\\",\\\"current_date\\\":\\\"06-24\\\"}}}\"\n}" ;

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{}");
        log.error("primaryUrl:{}",Constants.feishuHost+Constants.primaryUrl);
        try {
            Request request = new Request.Builder()
                    .url(Constants.feishuHost+Constants.primaryUrl)
                    .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;
    }
}
