package com.fs.qw.vo;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
* 企微sop附件的消息内容
*/
@Data
public class QwSopTempSetting implements Serializable{
    //模板名称
    private String name;
    private List<Content> content;

    @Data
    public static class Content implements Cloneable {

        //普通/课程/订单/ai
        private Long id;
        private int type;
        private int index;

        private String contentType;

        private List<Setting> setting;

        //时间
        private String time;

        private String isOfficial;//是否企微官方接口
        //课程
        private Long courseId;
        //课节
        private Long videoId;

        //消息类型 -普通0/待看课1/看课中断2/已完课3/待看和中断4
        private Integer courseType;

        private String aiTouch;

        private String addTag;

        private String delTag;

        private Integer isAtAll;

        private Long liveId;

        @Override
        public Content clone() {
            try {
                Content clone = (Content) super.clone();
                // 深克隆 setting 列表
                if (this.setting != null) {
                    clone.setting = new ArrayList<>();
                    for (Setting originalSetting : this.setting) {
                        clone.setting.add(originalSetting.clone());
                    }
                } else {
                    clone.setting = new ArrayList<>(); // 初始化为空列表
                }
                return clone;
            } catch (CloneNotSupportedException e) {
                throw new AssertionError();
            }
        }

        @Data
        public static class Setting  implements Serializable,Cloneable {
            /**
             * QwSopLog的主键(用于发送)
             */
            private Long id;
            private Long sopLogId;
            private Integer intervalTime;
            private String talkType;
            /**
             * 员工的id(用于发送)
             */
            private String qwUserid;
            /**
             * 客户的id(用于发送)
             */
            private String externalUserId;
            //文本-图片-链接-小程序-文件-视频-语音-视频号-app-自定义小程序
            private String contentType;
            //文本
            private String value;

            /** 是否开启随机发送：1-开启，0-关闭 */
            private Integer isRandomSend;

            /** 随机发送备选文案列表 */
            private List<String> randomValueList;
            //图片
            private String imgUrl;
            //链接标题
            private String linkTitle;
            //链接描述
            private String linkDescribe;
            //链接封面
            private String linkImageUrl;
            //链接地址
            private String linkUrl;
            //发送app消息的链接
            private String appLinkUrl;

            /** 小程序appid */
            private String miniprogramAppid;
            private Integer miniType;

            /** 小程序消息标题 */
            private String miniprogramTitle;

            /** 小程序封面地址 */
            private String miniprogramPicUrl;


            /** 小程序page路径 */
            private String miniprogramPage;

            /** 直播间Id*/
            private Long liveId;

            //文件地址
            private String fileUrl;
            //文件名称
            private String fileName;
            //视频地址
            private String videoUrl;
            //语音地址
            private String voiceUrl;
            // 封面标题
            private String nickname;
            //头像
            private String avatar;
            //封面图片地址
            private String coverUrl;
            //封面图片地址
            private String thumbUrl;
            //简介
            private String desc;
            //视频地址
            private String url;
            //标识
            private String extras;
            //时长
            private String voiceDuration;
            //是否绑定课程链接
            private String isBindUrl;
            //链接过期时间
            private Integer expiresDays;

            //视频ID
            private Long videoId;

            //封面图片地址 app用的参数
            private String courseUrl;

            //app显示标题 app用的参数
            private String title;

            //福袋id
            private Long luckyBagId;
            /**
             * 业务id
             */
            private String businessId;

            /** 订阅号文章链接（contentType=13时使用，透传给小程序端用于跳转） */
            private String articleUrl;

            /** 飞书应用账号 id（contentType=17 时使用） */
            private Long feishuAccountId;

            /** 飞书看课是否需要客户授权：contentType=17 固定为 0（免授权）；历史 1 仅作兼容字段 */
            private Integer feishuNeedAuth;

            public boolean isRandomSendEnabled() {
                return Objects.equals(isRandomSend, 1) || "1".equals(String.valueOf(isRandomSend));
            }

            /**
             * 随机发送：从备选文案中随机选取一条写入 value
             */
            public void applyRandomTextValue() {
                if (!"1".equals(contentType) && !"15".equals(contentType)) {
                    return;
                }
                if (!isRandomSendEnabled() || randomValueList == null || randomValueList.isEmpty()) {
                    return;
                }
                List<String> validList = randomValueList.stream()
                        .filter(StringUtils::isNotBlank)
                        .collect(Collectors.toList());
                if (!validList.isEmpty()) {
                    value = validList.get(ThreadLocalRandom.current().nextInt(validList.size()));
                }
            }

            @Override
            public Setting clone() {
                try {
                    Setting clone = (Setting) super.clone();
                    clone.setContentType(this.getContentType());
                    clone.setValue(this.getValue());
                    clone.setLinkUrl(this.getLinkUrl());
                    clone.setAppLinkUrl(this.getAppLinkUrl());
                    clone.setArticleUrl(this.getArticleUrl());
                    clone.setFeishuAccountId(this.getFeishuAccountId());
                    // 小程序相关字段（contentType=4/12/13 必须）
                    clone.setMiniprogramAppid(this.getMiniprogramAppid());
                    clone.setMiniType(this.getMiniType());
                    clone.setMiniprogramTitle(this.getMiniprogramTitle());
                    clone.setMiniprogramPicUrl(this.getMiniprogramPicUrl());
                    clone.setMiniprogramPage(this.getMiniprogramPage());
                    clone.setFileUrl(this.getFileUrl());
                    clone.setFileName(this.getFileName());
                    clone.setIsRandomSend(this.getIsRandomSend());
                    if (this.randomValueList != null) {
                        clone.randomValueList = new ArrayList<>(this.randomValueList);
                    }
                    return clone;
                } catch (CloneNotSupportedException e) {
                    throw new AssertionError();
                }
            }

        }

    }

}


