package com.gic.haoban.manage.service.pojo.bo.content.context;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.gic.enterprise.api.constant.EnterpriseServiceEnum;
import com.gic.haoban.manage.api.dto.StaffClerkRelationDTO;
import com.gic.haoban.manage.api.enums.NoticeMessageTypeEnum;
import com.gic.haoban.manage.api.enums.content.MaterialReportType;
import com.gic.haoban.manage.service.entity.TabHaobanStaff;
import com.gic.haoban.manage.service.pojo.bo.NoticeMessageBO;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author MUSI
 * @Date 2023/3/24 4:08 PM
 * @Description
 * @Version 好办 周报/月报 上下文
 **/
@Slf4j
@Data
@Builder
public class MaterialReportContext {

    private static final String week_title = "%s周报";
    private static final String month_title = "%s月报";

    private static final String week_desc = "%s～%s的%s已生成,请查收";
    private static final String month_desc = "%s～%s的%s已生成,请查收";

    private String enterpriseId;

    /**
     * 素材周期
     *
     * @see com.gic.haoban.manage.api.enums.content.MaterialReportType
     */
    private Integer reportType;

    /**
     * 周报内容
     */
    private List<String> rightMenuCodeList;

    /**
     * 成员id
     */
    private String staffId;

    /**
     * 成员对象
     */
    private TabHaobanStaff staffInfo;

    /**
     * 成员(店长或导购角色)在主门店时的导购id
     * 区经时就是导购id
     */
    private String clerkId;

    /**
     * 是否是区经
     */
    private boolean isManager;

    /**
     * 主门店
     */
    private String mainStoreId;

    /**
     * 企业factoryCode
     */
    private String factoryCode;

    /**
     * 导购关联表
     */
    private List<StaffClerkRelationDTO> staffClerkRelations;


    /**
     * 素材报告 图片地址
     */
    private String materialReportUrl;

    /**
     * 周期事件
     */
    private String bizDate;

    /**
     * 构建好办小程序消息
     *
     * @return
     */
    public List<NoticeMessageBO> buildNotifyMessageBos() {
        if (CollectionUtils.isEmpty(this.staffClerkRelations)) {
            log.info("成员未绑定导购, staffId: {}", this.staffId);
            return Collections.emptyList();
        }
        if (this.isManager) {
            log.info("成员{}为区经角色, 不发送小程序消息通知 ", this.staffId);
            return Collections.emptyList();
        }

        final NoticeMessageTypeEnum noticeMessageTypeEnum = MaterialReportType.WEEK.getCode().equals(this.getReportType()) ? NoticeMessageTypeEnum.MATERIAL_REPORT_NOTIFY_WEEK : NoticeMessageTypeEnum.MATERIAL_REPORT_NOTIFY_MONTH;

        if (ObjectUtil.isNull(noticeMessageTypeEnum)){
            return new ArrayList<>();
        }
        return this.staffClerkRelations
                .stream()
                .map(item -> {
                    NoticeMessageBO noticeMessageBO = new NoticeMessageBO();
                    noticeMessageBO.setCategoryType(noticeMessageTypeEnum.getCategory());
                    noticeMessageBO.setMessageType(noticeMessageTypeEnum.getType());
                    noticeMessageBO.setClerkId(StringUtils.isNotBlank(item.getClerkId()) ? item.getClerkId() : "-1");
                    noticeMessageBO.setStoreId(StringUtils.isNotBlank(item.getStoreId()) ? item.getStoreId() : "-1");
                    noticeMessageBO.setTitle(this.getMessageTitle());
                    noticeMessageBO.setTemplateCode(noticeMessageTypeEnum.getTemplateCode());
                    noticeMessageBO.setDescription(this.getMessageDesc());
                    noticeMessageBO.setEnterpriseId(this.enterpriseId);
                    noticeMessageBO.setRelationId(this.clerkId);
                    noticeMessageBO.setMessageContent(this.extendParams(item.getStoreId()));
                    return noticeMessageBO;
                })
                .collect(Collectors.toList());
    }

    public String getMessageTitle() {
        String title = "素材使用";
        if (CollectionUtils.isNotEmpty(rightMenuCodeList)) {
            if (rightMenuCodeList.size() > 1) {
                title = "门店增长";
            }else if (rightMenuCodeList.contains(EnterpriseServiceEnum.POTENTIAL.getRightMenuCode())){
                title = "潜客雷达";
            }
        }
        if (MaterialReportType.WEEK.getCode().equals(this.reportType)) {
            return String.format(week_title, title);
        }
        return String.format(month_title, title);
    }

    public String getMessageDesc() {
        if (MaterialReportType.WEEK.getCode().equals(this.reportType)) {
            // 获取周报信息
            DateTime lastWeek = DateUtil.lastWeek();
            String weekBeginStr = DateUtil.beginOfWeek(lastWeek)
                    .toString("MM月dd日");
            String weekEndStr = DateUtil.endOfWeek(lastWeek)
                    .toString("MM月dd日");
            return String.format(week_desc, weekBeginStr, weekEndStr, getMessageTitle());
        }

        DateTime lastMonth = DateUtil.lastMonth();
        String monthBeginStr = DateUtil.beginOfMonth(lastMonth)
                .toString("MM月dd日");
        String monthEndStr = DateUtil.endOfMonth(lastMonth)
                .toString("MM月dd日");
        return String.format(month_desc, monthBeginStr, monthEndStr, getMessageTitle());
    }

    public String extendParams(String storeId) {
        JSONObject params = new JSONObject();
        // 此处页面链接 最长128个字节
        // storeId_reportType_bizDate
        String key = String.format("%s_%s_%s", StringUtils.isBlank(storeId) ? "" : storeId, this.reportType, this.bizDate);
        params.put("k", key);
//        params.put("t", this.reportType);
        return params.toJSONString();
    }


}
