package cn.axzo.msg.center.service.pending.response;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 *
 * @author cold_blade
 * @date 2023/9/23
 * @version 1.0
 */
@Setter
@Getter
public class PendingMessageStatisticResponseV2 implements Serializable {

    private static final long serialVersionUID = 972200410809186003L;

    /**
     * 统计总数 (各分类相加)
     */
    private Stat totalStat;

    /**
     * 统计总数 (一个端分类配置重复了也没有关系)
     */
    private Stat uniqueTotalStat;

    /**
     * 分类情况
     */
    private List<GroupStat> groupStats = Collections.synchronizedList(new ArrayList<>());

    private Boolean cacheReloaded;

    public void addGroupStats(List<GroupStat> groupStats) {
        this.groupStats.addAll(groupStats);
    }

    public void addGroupStat(GroupStat groupStat) {
        this.groupStats.add(groupStat);
    }

    public void setTotalStat() {
        int totalPendingCount = groupStats.stream()
                .map(GroupStat::getStat)
                .mapToInt(Stat::getPendingCount)
                .sum();
        int totalUnreadCount = groupStats.stream()
                .map(GroupStat::getStat)
                .mapToInt(Stat::getUnreadCount)
                .sum();
        this.totalStat = new Stat(totalPendingCount, totalUnreadCount);
    }

    @Data
    public static class GroupStat {
        /**
         * 分类id
         */
        private Long nodeId;
        /**
         * 分类名称
         */
        private String groupName;
        /**
         * 分类编码
         */
        private String groupCode;
        /**
         * 图标
         */
        private String groupIcon;
        /**
         * 分类统计
         */
        private Stat stat;

        /**
         * 定制化内容: 工人待办相关的信息. 如果分类中只有一条待办数据, 就会返回这个字段. 如果分类中有很多待办, 这个字段为空
         * <p>REQ-2212: 工人APP首页改版
         * <p><a href="https://alidocs.dingtalk.com/i/nodes/Qnp9zOoBVBg2lOj1SQkMNlmdJ1DK0g6l?utm_scene=team_space&utm_medium=dingdoc_doc_plugin_card&utm_source=dingdoc_doc">工人APP首页改版</a>
         * <p><a href="https://alidocs.dingtalk.com/i/nodes/dpYLaezmVNnm1pxqhdkQONoAVrMqPxX6?corpId=ding509fc72d6685d56d4ac5d6980864d335&utm_medium=im_card&iframeQuery=utm_medium%3Dim_card%26utm_source%3Dim&utm_scene=team_space&utm_source=im">工人APP首页改版-消息/待办支持</a>
         */
        private PendingMessageResponse workerSingleTodo;

        /**
         * 其它额外的信息
         */
        private Map<String, Object> ext = new HashMap<>();

        /**
         * 添加额外的信息, 比如添加头像
         */
        public void addExt(String name, Object value) {
            if (!ext.containsKey(name))
                ext.put(name, value);
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Stat {
        /**
         * 待处理数量
         */
        private int pendingCount;
        /**
         * 抄送未读数量
         */
        private int unreadCount;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
