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 Esb2FeishuCalendarAttendeesUtil {
    private final static Logger log = LoggerFactory.getLogger(Esb2FeishuCalendarAttendeesUtil.class);

    @Autowired
    Esb2FeishuTokenUtil esb2FeishuTokenUtil;

    public String sendUserCalendarEvents(String user_id,String calendar_id,String event_id) {
        String attendee_id = "";
        Map<String, Object> messageMap = new HashMap<String, Object>();
        String token = esb2FeishuTokenUtil.getToken();
        log.error("token:"+token);
        if(StringUtils.isNotBlank(token)){

            JSONArray attendeesArray = new JSONArray();
            JSONObject attendeesJson = new JSONObject();
            attendeesJson.put("type","user");
            attendeesJson.put("user_id",user_id);
            attendeesArray.add(attendeesJson);
            JSONObject bodyJson = new JSONObject();
            bodyJson.put("attendees",attendeesArray);
            bodyJson.put("need_notification",Constants.need_notification);
            log.error("bodyJson:"+bodyJson.toJSONString());

            String calendarAttendeesUrl = String.format(Constants.calendarAttendeesUrl, calendar_id,event_id);
            log.error("calendarAttendeesUrl:"+calendarAttendeesUrl);

            String message = doHttpPost(bodyJson.toJSONString(),token,calendarAttendeesUrl);
            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 attendeesList = dataJson.getJSONArray("attendees");
                        for(int i=0;i<attendeesList.size();i++){
                            JSONObject attendeeJson = attendeesList.getJSONObject(i);
                            attendee_id = attendeeJson.getString("attendee_id");
                        }
                    }
                }
            }
        }

        return attendee_id;
    }

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

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

        try {
            Request request = new Request.Builder()
                    .url(Constants.feishuHost+calendarAttendeesUrl)
                    .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;
    }



}
