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

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.feishu.util.*;
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.Service;

import java.util.HashMap;
import java.util.Map;

@Service("Esb2FeishuCalendarsUpdateAction")
public class Esb2FeishuCalendarsUpdateAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2FeishuCalendarsUpdateAction.class);

    @Autowired
    Esb2FeishuCalendarUpdateUtil esb2FeishuCalendarUpdateUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        String summary = (String) params.get("summary");
        String description = (String) params.get("description");
        String start_date = (String) params.get("start_date");
        String end_date = (String) params.get("end_date");
        String location_name = (String) params.get("location_name");
        String location_address = (String) params.get("location_address");
        String type = (String) params.get("type");

        String dataid = (String) params.get("dataid");

        String openId = (String) params.get("openId");
        String calendarId = (String) params.get("calendarId");
        String eventId = (String) params.get("eventId");
        log.error("openId:{}",openId);
        log.error("calendarId:{}",calendarId);
        log.error("eventId:{}",eventId);
        log.error("dataid:{}",dataid);

        if("BusinessTrip".equals(type)){
            String start_time = "";
            if(params.containsKey("start_time")){
                start_time = String.valueOf(params.get("start_time"));
            }
            start_date = start_date+" "+start_time;
            String end_time = "";
            if(params.containsKey("end_time")){
                end_time = (String) params.get("end_time");
            }
            end_date = end_date+" "+end_time;
        }
        log.error("start_date:{}",start_date);
        log.error("end_date:{}",end_date);


        if(StringUtils.isBlank(openId)){
            return WeaResult.fail(500,"获取飞书的人员信息失败");
        }

        if(StringUtils.isBlank(calendarId)){
            return WeaResult.fail(500,"获取飞书主日历失败");
        }

        if(StringUtils.isBlank(eventId)){
            return WeaResult.fail(500,"获取飞书日历失败");
        }

        String update_event_id = "";
        if(StringUtils.isNotBlank(calendarId) && StringUtils.isNotBlank(eventId)){
            update_event_id = esb2FeishuCalendarUpdateUtil.updateCalendarEvents(calendarId,eventId,summary,description,start_date,end_date,location_name,location_address);
        }

        if(StringUtils.isBlank(update_event_id)){
            return WeaResult.fail(500,"更新飞书日历失败");
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","成功");
        actionMap.put("calendarId",calendarId);
        actionMap.put("eventId",eventId);
        actionMap.put("openId",openId);
        actionMap.put("dataid",dataid);
        actionMap.put("update_event_id",update_event_id);

        return WeaResult.success(actionMap);
    }
}
