package cn.axzo.msg.center.domain.entity;

import cn.axzo.foundation.dao.support.mysql.type.BaseListTypeHandler;
import cn.axzo.im.center.api.feign.SendPriority;
import cn.axzo.msg.center.domain.utils.IgnorePropsJsonTypeHandler;
import cn.axzo.msg.center.push.PushData;
import cn.axzo.msg.center.service.domain.CardUrlConfig;
import cn.axzo.msg.center.service.domain.card.AppVersionConfig;
import cn.axzo.msg.center.service.domain.card.StateImageConfig;
import cn.axzo.msg.center.service.enums.CardUrlOpenStrategy;
import cn.axzo.msg.center.service.enums.MessageCategoryEnum;
import cn.axzo.msg.center.service.enums.MessageChannel;
import cn.axzo.msg.center.service.enums.PushTerminalEnum;
import cn.axzo.msg.center.service.enums.StatusEnum;
import cn.axzo.msg.center.service.enums.YesOrNo;
import cn.axzo.msg.center.service.pending.card.domain.CardElementConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author yanglin
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName(value = "message_template_v3", autoResultMap = true)
public class MessageTemplateV3 extends BaseEntityWithOperator<MessageTemplateV3> {

    /**
     * 模板名称
     */
    private String name;

    /**
     * 系统自动生成的模板code
     */
    private String code;

    /**
     * 所属消息类型
     */
    private MessageCategoryEnum msgCategory;

    /**
     * 模板标题
     */
    private String title;

    /**
     * 模板类容
     */
    private String content;

    /**
     * 消息详情样式. BIZ_COMMON: 业务待办能用, FLOW_COMMON: 审批待办能用, 其它动态配置的
     */
    private String detailStyleCode;

    /**
     * 模板icon
     */
    private String icon;

    /**
     * APP最小版本支持，可不配
     */
    private String minAppVersion;

    /**
     * 模板状态
     */
    private StatusEnum status;

    /**
     * 推送终端配置 JSON字串
     */
    private String pushTerminal;

    /**
     * 卡片跳转方式. NONE: 无跳转, OPEN_TODO_DETAIL: 打开待办详情, OPEN_CUSTOM_PAGE: 打开指定页面
     */
    private CardUrlOpenStrategy cardUrlOpenStrategy;

    /**
     * 卡片跳转配置. 在links的基础上再包一层, 避免以后卡片增加自己的配置
     */
    @TableField(typeHandler = IgnorePropsJsonTypeHandler.class)
    private CardUrlConfig cardUrlConfig;

    /**
     * IM发送优先级
     */
    private Integer imSendPriority;

    /**
     * push配置
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private JSONObject pushData;

    /**
     * 是否显示在列表中. YES: 是, NO: 否
     */
    private YesOrNo displayOnList;

    /**
     * app最低版本控制
     */
    @TableField(typeHandler = AppVersionConfigsTypeHandler.class)
    private List<AppVersionConfig> appVersionConfigs;

    /**
     * 卡片样式编码
     */
    private String cardStyleCode;

    /**
     * 卡片样式扩展字段
     */
    @TableField(typeHandler = CardElementConfigTypeHandler.class)
    private List<CardElementConfig> cardExtFields;

    /**
     * 卡片状态图片配置
     */
    @TableField(typeHandler = StateImageConfigTypeHandler.class)
    private List<StateImageConfig> stateImageConfigs;

    /**
     * 通道
     */
    @TableField(typeHandler = MessageChannelTypeHandler.class)
    private List<MessageChannel> channels;

    /**
     * 扩展字段
     */
    @TableField(typeHandler = IgnorePropsJsonTypeHandler.class)
    private RecordExt recordExt;

    public List<MessageChannel> determineChannels() {
        return channels == null ? Collections.emptyList() : channels;
    }

    /**
     * 是否包含IM通道
     */
    public boolean isContainImChannel() {
        return this.determineChannels().contains(MessageChannel.IM);
    }

    public PushData parsePushData() {
        JSONObject pushData = this.pushData;
        if (pushData == null)
            pushData = new JSONObject();
        return pushData.toJavaObject(PushData.class);
    }

    public Integer determineImSendPriority() {
        return imSendPriority == null ? SendPriority.TEMPLATE_MESSAGE.getPriority() : imSendPriority;
    }

    public List<PushTerminalEnum> determinePushTerminals() {
        if (StringUtils.isBlank(pushTerminal))
            return Collections.emptyList();
        return JSON.parseArray(pushTerminal, PushTerminalEnum.class);
    }

    public RecordExt getOrCreateRecordExt() {
        if (recordExt == null) recordExt = new RecordExt();
        return recordExt;
    }

    @Setter
    @Getter
    public static class RecordExt {
        private Boolean isMigrated;
        private Boolean isSyncedForTest;
    }

    // @formatter:off
    public static class AppVersionConfigsTypeHandler
            extends BaseListTypeHandler<AppVersionConfig> {}
    public static class StateImageConfigTypeHandler
            extends BaseListTypeHandler<StateImageConfig> {}
    public static class CardElementConfigTypeHandler
            extends BaseListTypeHandler<CardElementConfig> {}
    public static class MessageChannelTypeHandler
            extends BaseListTypeHandler<MessageChannel> {}
    // @formatter:on

}