package com.usoftchina.dingtalk.sdk.dto;

import com.dingtalk.api.request.OapiCalendarCreateRequest;
import org.springframework.ui.ModelMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author yingp
 */
public class AddScheduleReq {
    /**
     * 组织者userId
     */
    private String organizer;
    /**
     * 业务方自己的主键
     */
    private String bizId;
    /**
     * 日程开始时间，Unix时间戳
     */
    private Long startTime;
    /**
     * 日程结束时间，Unix时间戳
     */
    private Long endTime;
    /**
     * 日程参与者列表。最多支持2000人
     */
    private List<String> attendees;
    /**
     * 日程标题。0 ~ 128 字符。不填会默认显示为“新建事件”
     */
    private String summary;
    /**
     * 日程描述。0 ~ 512 字符
     */
    private String description;
    /**
     * 日程开始（start_time）前多少分提醒
     */
    private Long remindBeforeEventMins;
    /**
     * 日程地址。0 ~ 128 字符
     */
    private String location;
    /**
     * 日程来源标题
     */
    private String sourceTitle;
    /**
     * 日程来源链接，点击后跳转
     */
    private String sourceUrl;

    public AddScheduleReq organizer(String organizer) {
        this.organizer = organizer;
        return this;
    }

    public AddScheduleReq bizId(String bizId) {
        this.bizId = bizId;
        return this;
    }

    public AddScheduleReq during(long startTime, long endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
        return this;
    }

    public AddScheduleReq attendees(List<String> attendees) {
        this.attendees = attendees;
        return this;
    }

    public AddScheduleReq summary(String summary) {
        this.summary = summary;
        return this;
    }

    public AddScheduleReq description(String description) {
        this.description = description;
        return this;
    }

    public AddScheduleReq source(String title, String url) {
        this.sourceTitle = title;
        this.sourceUrl = url;
        return this;
    }

    public AddScheduleReq remindBefore(long minutes) {
        this.remindBeforeEventMins = minutes;
        return this;
    }

    public AddScheduleReq location(String location) {
        this.location = location;
        return this;
    }

    public OapiCalendarCreateRequest build() {
        OapiCalendarCreateRequest req = new OapiCalendarCreateRequest();
        OapiCalendarCreateRequest.OpenCalendarCreateVo creatVo = new OapiCalendarCreateRequest.OpenCalendarCreateVo();
        creatVo.setUuid(UUID.randomUUID().toString());
        creatVo.setBizId(bizId);
        creatVo.setCreatorUserid(organizer);
        creatVo.setSummary(summary);
        creatVo.setDescription(description);
        creatVo.setLocation(location);
        creatVo.setReceiverUserids(attendees);
        OapiCalendarCreateRequest.DatetimeVo start = new OapiCalendarCreateRequest.DatetimeVo();
        start.setUnixTimestamp(startTime);
        creatVo.setStartTime(start);
        OapiCalendarCreateRequest.DatetimeVo end = new OapiCalendarCreateRequest.DatetimeVo();
        end.setUnixTimestamp(endTime);
        creatVo.setEndTime(end);
        creatVo.setCalendarType("notification");
        if (null != sourceTitle) {
            OapiCalendarCreateRequest.OpenCalendarSourceVo source = new OapiCalendarCreateRequest.OpenCalendarSourceVo();
            source.setTitle(sourceTitle);
            source.setUrl(sourceUrl);
            creatVo.setSource(source);
        }
        if (null != remindBeforeEventMins) {
            OapiCalendarCreateRequest.OpenCalendarReminderVo reminderVo = new OapiCalendarCreateRequest.OpenCalendarReminderVo();
            reminderVo.setMinutes(remindBeforeEventMins);
            creatVo.setReminder(reminderVo);
        }
        req.setCreateVo(creatVo);
        return req;
    }
}
