package cn.axzo.msg.center.message.domain.dto;

import cn.axzo.msg.center.api.custombutton.ProposedButtons;
import cn.axzo.msg.center.domain.entity.PendingMessageRecord;
import cn.axzo.msg.center.service.dto.IdentityDTO;
import cn.axzo.msg.center.service.dto.MessageCardContentItemDTO;
import cn.axzo.msg.center.service.dto.PersonDTO;
import cn.axzo.msg.center.service.enums.BizCategoryEnum;
import cn.axzo.msg.center.service.enums.BizFinalStateEnum;
import cn.axzo.msg.center.service.enums.MessageCategoryEnum;
import cn.axzo.msg.center.service.enums.OrganizationTypeEnum;
import cn.axzo.msg.center.service.enums.PendingMessageStateEnum;
import cn.axzo.msg.center.service.enums.TerminalTypeEnum;
import cn.axzo.msg.center.service.enums.TodoType;
import cn.axzo.msg.center.service.pending.response.PendingMessageResponse;
import cn.axzo.msg.center.service.template.response.MessageDetailStyle;
import cn.axzo.msg.center.utils.DateFormatUtil;
import cn.axzo.msg.center.utils.MessageRouterUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @description
 *   代办消息记录DTO
 * @author cold_blade
 * @date 2023/9/23
 * @version 1.0
 */
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PendingMessageDTO implements Serializable {

    private static final long serialVersionUID = 2207968284043321033L;

    /**
     * 消息的唯一标识
     */
    private String identityCode;
    /**
     * 模板编码
     */
    private String templateCode;
    /**
     * 消息标题
     */
    private String title;
    /**
     * 消息内容
     */
    private String content;
    /**
     * 待办状态
     */
    private PendingMessageStateEnum state;
    /**
     * 卡片信息
     */
    private List<MessageCardContentItemDTO> cardItems;
    /**
     * 代办发起者信息
     */
    private PersonDTO promoter;
    /**
     * 代办执行者信息
     */
    private PersonDTO executor;
    /**
     * 业务编码
     */
    private String bizCode;
    /**
     * 子业务编码
     */
    private String subBizCode;
    /**
     * 业务状态描述
     */
    private String bizDesc;
    /**
     * 业务标签
     */
    private String bizFlag;
    /**
     * 工作台id
     * <p>Deprecated, used {@link #executorWorkspaceId} instead
     */
    @Deprecated
    private Long workspaceId;
    /**
     * 工作台名称
     * <p>Deprecated, used {@link #executorWorkspaceName} instead
     */
    @Deprecated
    private String workspaceName;
    /**
     * 类型
     */
    private OrganizationTypeEnum organizationType;
    /**
     * 待办发起人的
     * <p>Deprecated, used {@link #executorOuId} instead
     */
    @Deprecated
    private Long ouId;
    /**
     * 业务类型
     */
    private BizCategoryEnum bizCategory;
    /**
     * 待办截至时间
     */
    private LocalDateTime deadline;
    /**
     * 申请时间戳
     */
    private LocalDateTime createTime;
    /**
     * 处理时间戳
     */
    private LocalDateTime updateTime;
    /**
     * 路由信息,可为空
     */
    private MessageTemplateRouterDTO msgTemplateRouter;
    /**
     * 业务终态
     */
    private BizFinalStateEnum bizFinalState;
    /**
     * 路由参数
     */
    private String routerParam;
    /**
     * 在这个时间之前不显示
     */
    private Date hideUntil;

    /**
     * 模版类型
     */
    private MessageCategoryEnum templateCategory;

    /**
     * 待办类型
     */
    private TodoType todoType;

    /**
     * 是否已读 (抄送)
     */
    private Boolean isRead;
    /**
     * 发起人工作台id
     */
    private Long promoterWorkspaceId;
    /**
     * 发起人工作台名称
     */
    private String promoterWorkspaceName;
    /**
     * 发起人企业类型
     */
    private OrganizationTypeEnum promoterWorkspaceType;
    /**
     * 发起人企业id
     */
    private Long promoterOuId;
    /**
     * 接收人工作台id
     */
    private Long executorWorkspaceId;
    /**
     * 接收人工作台名称
     */
    private String executorWorkspaceName;
    /**
     * 执行人企业类型
     */
    private OrganizationTypeEnum executorWorkspaceType;
    /**
     * 接收人企业id
     */
    private Long executorOuId;

    /**
     * 待办样式
     */
    private MessageDetailStyle detailStyle;

    /**
     * 自定义按钮
     */
    private ProposedButtons proposedButtons;

    private JSONObject bizExtParamObj;
    private JSONObject routerExtParamObj;

    @Deprecated
    public static PendingMessageDTO from(PendingMessageRecord pendingMessageRecord) {
        // 代办发起者信息
        PersonDTO promoter = PersonDTO.builder()
                .id(pendingMessageRecord.getPromoterPersonId())
                .identity(IdentityDTO.builder()
                        .id(pendingMessageRecord.getPromoterId())
                        .type(pendingMessageRecord.getPromoterType())
                        .build())
                .name(pendingMessageRecord.getPromoterName())
                .build();
        // 代办执行者信息
        PersonDTO executor = PersonDTO.builder()
                .id(pendingMessageRecord.getExecutorPersonId())
                .identity(IdentityDTO.builder()
                        .id(pendingMessageRecord.getExecutorId())
                        .type(pendingMessageRecord.getExecutorType())
                        .build())
                .name(pendingMessageRecord.getExecutorName())
                .build();
        return PendingMessageDTO.builder()
                .todoType(pendingMessageRecord.getTodoType())
                .identityCode(pendingMessageRecord.getIdentityCode())
                .templateCode(pendingMessageRecord.getTemplateCode())
                .title(pendingMessageRecord.getTitle())
                .content(pendingMessageRecord.getContent())
                .state(pendingMessageRecord.getState())
                .promoter(promoter)
                .executor(executor)
                .bizCode(pendingMessageRecord.getBizCode())
                .subBizCode(pendingMessageRecord.getSubBizCode())
                .bizDesc(pendingMessageRecord.getBizDesc())
                .bizFlag(pendingMessageRecord.getBizFlag())
                .workspaceId(pendingMessageRecord.getOrgId())
                .workspaceName(pendingMessageRecord.getOrgName())
                .organizationType(pendingMessageRecord.getOrgType())
                .ouId(pendingMessageRecord.getOuId())
                .bizCategory(pendingMessageRecord.getBizCategory())
                .deadline(DateFormatUtil.toLocalDateTime(pendingMessageRecord.getDeadline()))
                .createTime(DateFormatUtil.toLocalDateTime(pendingMessageRecord.getCreateAt()))
                .updateTime(DateFormatUtil.toLocalDateTime(pendingMessageRecord.getUpdateAt()))
                .routerParam(pendingMessageRecord.getRouterParams())
                .bizFinalState(pendingMessageRecord.getBizFinalState())
                .bizCode(pendingMessageRecord.getBizCode())
                .subBizCode(pendingMessageRecord.getSubBizCode())
                .hideUntil(pendingMessageRecord.getHideUntil())
                .build();
    }

    public PendingMessageResponse toResponse(TerminalTypeEnum terminalType) {
        boolean isCreatedToday = false;
        if (createTime != null) {
            Date createAt = DateFormatUtil.toDate(createTime);
            isCreatedToday = DateUtil.isSameDay(createAt, new Date());
        }
        return PendingMessageResponse.builder()
                // 发起人单位、项目信息
                .promoterOuId(promoterOuId)
                .promoterWorkspaceId(promoterWorkspaceId)
                .promoterWorkspaceName(promoterWorkspaceName)
                .promoterWorkspaceType(promoterWorkspaceType)
                // 接收者单位、项目信息
                .executorOuId(executorOuId)
                .executorWorkspaceId(executorWorkspaceId)
                .executorWorkspaceName(executorWorkspaceName)
                .executorWorkspaceType(executorWorkspaceType)
                // 其它信息
                .bizExtParamObj(bizExtParamObj)
                .routerExtParamObj(routerExtParamObj)
                //页面展示
                .ouId(this.ouId)
                .workspaceId(workspaceId)
                .workspaceName(workspaceName)
                // 样式
                .detailStyle(detailStyle)
                .isCreatedToday(isCreatedToday)
                .isRead(isRead)
                .todoType(todoType)
                .proposedButtons(proposedButtons)
                .templateCategory(templateCategory)
                .identityCode(this.identityCode)
                .templateCode(this.templateCode)
                .title(this.title)
                .content(this.content)
                .cardItems(this.cardItems)
                .promoterPersonId(Optional.ofNullable(this.promoter).map(PersonDTO::getId).orElse(null))
                .promoterName(Optional.ofNullable(this.promoter).map(PersonDTO::getName).orElse(null))
                .promoterIdentity(Optional.ofNullable(this.promoter).map(PersonDTO::getIdentity).orElse(null))
                .executorPersonId(Optional.ofNullable(this.executor).map(PersonDTO::getId).orElse(null))
                .executorName(Optional.ofNullable(this.executor).map(PersonDTO::getName).orElse(null))
                .executorIdentity(Optional.ofNullable(this.executor).map(PersonDTO::getIdentity).orElse(null))
                .bizCode(this.bizCode)
                .subBizCode(this.subBizCode)
                .bizDesc(this.bizDesc)
                .bizFlag(this.bizFlag)
                .state(this.getState())
                .bizCategory(this.bizCategory)
                .createTimestamp(DateFormatUtil.toTimestamp(this.createTime))
                .updateTimestamp(DateFormatUtil.toTimestamp(this.updateTime))
                .deadlineTimestamp(DateFormatUtil.toTimestamp(this.deadline))
                .detailRouter(MessageRouterUtil.fetchBizDetailRouter(this.msgTemplateRouter, terminalType).orElse(null))
                .buttonRouters(MessageRouterUtil.fetchMessageRouterButtons(this.msgTemplateRouter, terminalType))
                .bizFinalStateIcon(Optional.ofNullable(bizFinalState).map(BizFinalStateEnum::getIcon).orElse(null))
                .bizFinalState(bizFinalState)
                .build();
    }

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