package com.yonge.cooleshow.biz.dal.wrapper.im;

import com.yonge.cooleshow.biz.dal.enums.ImGroupType;
import com.alibaba.fastjson.JSON;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.Optional;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Data
public class ImGroupWrapper {

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel(" ImGroupQuery-即时通讯群组")
    public static class ImGroupQuery implements QueryInfo {

        @ApiModelProperty("当前页")
        private Integer page;

        @ApiModelProperty("分页行数")
        private Integer rows;

        @ApiModelProperty("关键字匹配")
        private String keyword;

        @ApiModelProperty("群类型")
        private String groupType;

        @ApiModelProperty("群成员ID")
        private Long userId;

        @ApiModelProperty("客户类型")
        private String clientType;

        @ApiModelProperty("群创建者")
        private Long createBy;

        @ApiModelProperty("群角色")
        private String roleType;

        public static ImGroupQuery from(String json) {
            return JSON.parseObject(json, ImGroupQuery.class);
        }

        public String getKeyword() {
            return Optional.ofNullable(keyword).filter(StringUtils::isNotBlank).orElse(null);
        }

        public ImGroupQuery userId(Long userId) {
            this.userId = userId;
            return this;
        }

        public ImGroupQuery clientType(String clientType) {
            this.clientType = clientType;
            return this;
        }
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel(" ImGroup-即时通讯群组")
    public static class ImGroup {


        @ApiModelProperty(value = "主键ID", hidden = true)
        private Long id;

        @ApiModelProperty("群组ID")
        private String groupId;

        @ApiModelProperty("群名称")
        private String name;

        @ApiModelProperty("群简介")
        private String introduce;

        @ApiModelProperty("群成员数")
        private Integer memberNum;

        @ApiModelProperty("群备注")
        private String memo;

        @ApiModelProperty("群头像")
        private String img;

        @ApiModelProperty("群类型 ")
        private String type;

        @ApiModelProperty("是否自动通过审核1是0否")
        private Boolean autoPassFlag;

        @ApiModelProperty("课程组id")
        private Long courseGroupId;

        @ApiModelProperty("修改时间;")
        private Date updateTime;

        @ApiModelProperty("创建人")
        private Long createBy;

        @ApiModelProperty("创建时间")
        private Date createTime;

        @ApiModelProperty("群聊配置")
        private String configJson;

        private Set<Long> studentIdList = new HashSet<>();

        private Set<String> imUserIdList = new HashSet<>();

        public static ImGroup from(String json) {
            return JSON.parseObject(json, ImGroup.class);
        }

        public String jsonString() {
            return JSON.toJSONString(this);
        }

        public ImGroup id(Long id) {
            this.id = id;
            return this;
        }

        public ImGroup groupId(String groupId) {
            this.groupId = MessageFormatter.format("{}:{}", groupId, getType()).getMessage();
            return this;
        }

        public ImGroup type(String type) {
            this.type = type;
            return this;
        }

    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ApiModel("IM用户信息")
    public static class ImUserInfo implements Serializable {

        @ApiModelProperty("用户ID")
        private String imUserId;

        @ApiModelProperty("授权Token")
        private String imToken;

        public ImUserInfo imUserId(String imUserId) {
            this.imUserId = imUserId;
            return this;
        }
    }

    /**
     * 群配置
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ImGroupConfigJson {

        // 群禁言
        private Boolean mute;

        public String toJson() {
            return JSON.toJSONString(this);
        }

    }

    /**
     * 群禁言
     */
    @ApiModel("群禁言")
    @Data
    public static class Mute {

        @ApiModelProperty("群ID")
        @NotNull(message = "群ID不能为空")
        private String groupId;

        @ApiModelProperty("true:禁言，false:解禁言")
        @NotNull
        private Boolean muteAll;
    }

    @Data
    @ApiModel("更换群主")
    public static class ChangeGroupOwner {

        @NotNull
        @ApiModelProperty("群ID")
        private String groupId;

        @NotBlank(message = "新群主不能为空")
        @ApiModelProperty("新群主")
        private String newOwner;

        @NotBlank(message = "旧群主不能为空")
        @ApiModelProperty("旧群主")
        private String oldOwner;
    }

}