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

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.hrm.util.HrmCommonUtil;
import com.weaver.ebuilder.form.client.entity.obj.Obj;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuCalendarAttendeesUtil;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuCalendarEventsUtil;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuCalendarPrimaryUtil;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuOpenIdUtil;
import com.weaver.teams.domain.user.SimpleEmployee;
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("Esb2FeishuCalendarsEventsAction")
public class Esb2FeishuCalendarsEventsAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2FeishuCalendarsEventsAction.class);

    @Autowired
    Esb2FeishuCalendarPrimaryUtil esb2FeishuCalendarPrimaryUtil;

    @Autowired
    Esb2FeishuCalendarEventsUtil esb2FeishuCalendarEventsUtil;

    @Autowired
    Esb2FeishuCalendarAttendeesUtil esb2FeishuCalendarAttendeesUtil;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    @Autowired
    Esb2FeishuOpenIdUtil esb2FeishuOpenIdUtil;

    @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 employee_id = (String) params.get("employee_id");
        String type = (String) params.get("type");
        String dataid = (String) params.get("dataid");

        String attendee_id_ = (String) params.get("attendee_id");
        if(StringUtils.isNotBlank(attendee_id_)){
            return WeaResult.fail(500,"日程已创建,无法新建日程");
        }

        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);

//        String mobile = "";
//        if(StringUtils.isNotBlank(employee_id)){
//            Long employeeId = Long.valueOf(employee_id);
//            SimpleEmployee simpleEmployee = hrmCommonUtil.getSimpleEmployee(employeeId);
//            mobile = simpleEmployee.getMobile();
//        }

//        if(StringUtils.isBlank(mobile)){
//            return WeaResult.fail(500,"获取手机号为空");
//        }

        String openId = esb2FeishuOpenIdUtil.queryFeishuOpenId(employee_id);
//        if(StringUtils.isNotBlank(mobile)){
//            Map<String, Object> userMap = esb2FeishuOpenIdUtil.queryFeishuOpenIdByMobiles(mobile);
//            if(userMap.containsKey(mobile)){
//                openId = userMap.get(mobile).toString();
//            }
//        }

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

        log.error("Esb2FeishuCalendarsEventsAction start");
        log.error("openId:{}",openId);
        String attendee_id = "";
        String calendarId = esb2FeishuCalendarPrimaryUtil.queryUserCalendarPrimary();

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

        log.error("calendarId:{}",calendarId);
        String event_id = "";
        if(StringUtils.isNotBlank(calendarId)){
            event_id = esb2FeishuCalendarEventsUtil.createCalendarEvents(calendarId,summary,description,start_date,end_date,location_name,location_address);
        }
        log.error("event_id:{}",event_id);
        if(StringUtils.isBlank(event_id)){
            return WeaResult.fail(500,"创建飞书日历失败");
        }

        if(StringUtils.isNotBlank(event_id)){
            attendee_id = esb2FeishuCalendarAttendeesUtil.sendUserCalendarEvents(openId,calendarId,event_id);
        }

        if(StringUtils.isBlank(attendee_id)){
            return WeaResult.fail(500,"飞书日历增加人员失败");
        }

        log.error("attendee_id:{}",attendee_id);
        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","成功");
        actionMap.put("attendeeId",attendee_id);
        actionMap.put("calendarId",calendarId);
        actionMap.put("event_id",event_id);
        actionMap.put("openId",openId);
        actionMap.put("dataid",dataid);
        return WeaResult.success(actionMap);
    }
}
