package com.usoftchina.qywx.sdk;

import com.usoftchina.qywx.sdk.config.QywxProperties;
import com.usoftchina.qywx.sdk.dto.*;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 日程
 *
 * @author yingp
 */
public class ScheduleSdk extends BaseSdk {

    public final static String SCHEDULE_AGENT_CODE = "Schedule";

    public ScheduleSdk(QywxProperties properties) {
        super(properties);
    }

    /**
     * 是否启用
     *
     * @return
     */
    public boolean enabled() {
        return getAgentMap().containsKey(SCHEDULE_AGENT_CODE);
    }

    /**
     * 创建日历
     * 该接口用于通过应用在企业内创建一个日历。
     *
     * @param req
     * @return 日历ID
     */
    public String addCalendar(AddCalendarReq req) {
        ResponseEntity<AddCalendarResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/calendar/add?access_token={access_token}",
                req.build(), AddCalendarResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
        return resp.getBody().getCal_id();
    }

    /**
     * 更新日历
     * 该接口用于修改指定日历的信息。
     * <p>更新操作是覆盖式，而不是增量式</p>
     *
     * @param req
     */
    public void updateCalendar(UpdateCalendarReq req) {
        ResponseEntity<BaseResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/calendar/update?access_token={access_token}",
                req.build(), BaseResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
    }

    /**
     * 获取日历
     * 该接口用于获取应用在企业内创建的日历信息。
     *
     * @param calendarIdList
     * @return
     */
    public List<GetCalendarListResp.Calendar> getCalendarList(List<String> calendarIdList) {
        ResponseEntity<GetCalendarListResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/calendar/get?access_token={access_token}",
                new ModelMap("cal_id_list", calendarIdList), GetCalendarListResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
        return resp.getBody().getCalendar_list();
    }

    /**
     * 获取日历
     * 该接口用于获取应用在企业内创建的日历信息
     *
     * @param calendarId
     * @return
     */
    public GetCalendarListResp.Calendar getCalendar(String calendarId) {
        List<GetCalendarListResp.Calendar> calendars = getCalendarList(Arrays.asList(calendarId));
        return CollectionUtils.isEmpty(calendars) ? null : calendars.get(0);
    }

    /**
     * 删除日历
     * 该接口用于删除指定日历
     *
     * @param calendarId
     */
    public void deleteCalendar(String calendarId) {
        ResponseEntity<BaseResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/calendar/del?access_token={access_token}",
                new ModelMap("cal_id", calendarId), BaseResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
    }

    /**
     * 创建日程
     * 该接口用于在日历中创建一个日程。
     *
     * @param req
     * @return 日程ID
     */
    public String addSchedule(AddScheduleReq req) {
        ResponseEntity<AddScheduleResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/schedule/add?access_token={access_token}",
                req.build(), AddScheduleResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
        return resp.getBody().getSchedule_id();
    }

    /**
     * 更新日程
     * 该接口用于在日历中更新指定的日程。
     * <p>更新操作是覆盖式，而不是增量式</p>
     *
     * @param req
     */
    public void updateSchedule(UpdateScheduleReq req) {
        ResponseEntity<BaseResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/schedule/update?access_token={access_token}",
                req.build(), BaseResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
    }

    /**
     * 获取日程
     * 该接口用于获取指定的日程详情。
     *
     * @param scheduleIdList
     * @return
     */
    public List<GetScheduleListResp.Schedule> getScheduleList(List<String> scheduleIdList) {
        ResponseEntity<GetScheduleListResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/schedule/get?access_token={access_token}",
                new ModelMap("schedule_id_list", scheduleIdList), GetScheduleListResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
        return resp.getBody().getSchedule_list();
    }

    /**
     * 获取日程
     * 该接口用于获取指定的日程详情。
     *
     * @param scheduleId
     * @return
     */
    public GetScheduleListResp.Schedule getSchedule(String scheduleId) {
        List<GetScheduleListResp.Schedule> schedules = getScheduleList(Arrays.asList(scheduleId));
        return CollectionUtils.isEmpty(schedules) ? null : schedules.get(0);
    }

    /**
     * 取消日程
     * 该接口用于取消指定的日程。
     *
     * @param scheduleId
     */
    public void deleteSchedule(String scheduleId) {
        ResponseEntity<BaseResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/schedule/del?access_token={access_token}",
                new ModelMap("schedule_id", scheduleId), BaseResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
    }

    /**
     * 获取日历下的日程列表
     * 该接口用于获取指定的日历下的日程列表。
     *
     * @param req 日历ID
     * @return
     */
    public List<GetScheduleByCalendarResp.Schedule> getScheduleByCalendar(GetScheduleByCalendarReq req) {
        ResponseEntity<GetScheduleByCalendarResp> resp = restTemplate.postForEntity(baseUrl + "/cgi-bin/oa/schedule/get_by_calendar?access_token={access_token}",
                req.build(), GetScheduleByCalendarResp.class,
                new ModelMap("access_token", getAccessToken(SCHEDULE_AGENT_CODE)));
        assertOK(resp);
        return resp.getBody().getSchedule_list();
    }
}
