package com.zd.forward.config;


import com.zd.common.core.exception.ServiceException;
import com.zd.forward.serivce.CheckResultValid;
import com.zd.forward.serivce.ImageCheckResultValidImpl;
import com.zd.forward.serivce.VideoCheckResultValid;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;

@ConfigurationProperties("sys.config")
@RefreshScope
@Validated
public class AlgorithmYml {

    /**
     * 视频算法
     */
    Map<Integer,VideoCheckValid> videoAidMap = Collections.emptyMap();

    /**
     * 算法集合
     */
    Map<Integer,CheckValid> algorithmMap = Collections.emptyMap();

    /**
     * 警报配置
     */
    Map<Integer,AlarmConfig> alarmConfigMap = Collections.emptyMap();

    /**
     * 中转服务图片提交地址
     */
    @NotBlank(message = "中转服务图片提交地址")
    String imgPostUrl;


    String imgTemp;

    /**
     * save log  Url
     */
    @NotBlank(message = "检查日志提交url地址不能为空")
    String checkLogUrl;

    /**
    * 跳过次数 -1 为不开启跳过 默认为3
     */
    Integer jumpThreshold=2;


    @NotBlank(message = "项目地址不能为空")
    String loginUri;

    /**
     * 转发目标地址
     */
    String targetUrl;

    /**
     * 1号喇叭IP
     */
    String loudspeakerIp1;
    /**
     * 2号喇叭IP
     */
    String loudspeakerIp2;

    /**
     * 喇叭开关
     */
    boolean loudspeakerSwitch=false;

    public boolean isLoudspeakerSwitch() {
        return loudspeakerSwitch;
    }

    public void setLoudspeakerSwitch(boolean loudspeakerSwitch) {
        this.loudspeakerSwitch = loudspeakerSwitch;
    }

    public String getLoudspeakerIp1() {
        return loudspeakerIp1;
    }

    public void setLoudspeakerIp1(String loudspeakerIp1) {
        this.loudspeakerIp1 = loudspeakerIp1;
    }

    public String getLoudspeakerIp2() {
        return loudspeakerIp2;
    }

    public void setLoudspeakerIp2(String loudspeakerIp2) {
        this.loudspeakerIp2 = loudspeakerIp2;
    }

    public String getImgTemp() {
        return imgTemp;
    }

    public void setImgTemp(String imgTemp) {
        this.imgTemp = imgTemp;
    }

    public Integer getJumpThreshold() {
        return jumpThreshold;
    }

    public void setJumpThreshold(Integer jumpThreshold) {
        this.jumpThreshold = jumpThreshold;
    }

    public Map<Integer, VideoCheckValid> getVideoAidMap() {
        return Collections.unmodifiableMap(videoAidMap);
    }

    public void setVideoAidMap(Map<Integer, VideoCheckValid> videoAidMap) {
        this.videoAidMap = videoAidMap;
    }

    public Map<Integer, AlarmConfig> getAlarmConfigMap() {
        return alarmConfigMap;
    }

    public void setAlarmConfigMap(Map<Integer, AlarmConfig> alarmConfigMap) {
        this.alarmConfigMap = alarmConfigMap;
    }

    public String getLoginUri() {
        return loginUri;
    }

    public void setLoginUri(String loginUri) {
        this.loginUri = loginUri;
    }

    public String getTargetUrl() {
        return targetUrl;
    }

    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }

    public String getCheckLogUrl() {
        return checkLogUrl;
    }

    public void setCheckLogUrl(String checkLogUrl) {
        this.checkLogUrl = checkLogUrl;
    }

    public static class VideoCheckValid{
        @NotNull (message = "描述不能为空")
        String tips;

        @NotBlank (message = "算法结构解析实现类不能为空")
        private String name = "videoCheckResultValidImpl";

        private VideoCheckResultValid videoCheckResultValid;

        public String getTips() {
            return tips;
        }

        public void setTips(String tips) {
            this.tips = tips;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public VideoCheckResultValid getVideoCheckResultValid() {
            return videoCheckResultValid;
        }

        public void setVideoCheckResultValid(VideoCheckResultValid videoCheckResultValid) {
            this.videoCheckResultValid = videoCheckResultValid;
        }
    }

    public static class AlarmConfig{
        private String funNum;
        private String describe;

        public String getFunNum() {
            return funNum;
        }

        public void setFunNum(String funNum) {
            this.funNum = funNum;
        }

        public String getDescribe() {
            return describe;
        }

        public void setDescribe(String describe) {
            this.describe = describe;
        }
    }

    public static class CheckValid {
        //算法ID
        @NotNull (message = "算法ID不能为空")
        private Integer algoId;
        //cid
        @NotNull (message = "数据ID不能为空")
        private String did;

        private String algorithmName;

        //默认使用图片post 推送结果解析类
        @NotNull (message = "算法结构解析实现类不能为空")
        private Class<?> classPath = ImageCheckResultValidImpl.class;
        //检查结果检查实现类
        private CheckResultValid checkResultValid;

        public String getAlgorithmName() {
            return algorithmName;
        }

        public void setAlgorithmName(String algorithmName) {
            this.algorithmName = algorithmName;
        }

        public Integer getAlgoId() {
            return algoId;
        }

        public void setAlgoId(Integer algoId) {
            this.algoId = algoId;
        }

        public String getDid() {
            return did;
        }

        public void setDid(String did) {
            this.did = did;
        }

        public CheckResultValid getCheckResultValid() {
            return checkResultValid;
        }

        public void setCheckResultValid(CheckResultValid checkResultValid) {
            this.checkResultValid = checkResultValid;
        }

        public Class<?> getClassPath() {
            return classPath;
        }

        public void setClassPath(Class<?> classPath) {
            this.classPath = classPath;
        }
    }

    public String getImgPostUrl() {
        return imgPostUrl;
    }

    public void setImgPostUrl(String imgPostUrl) {
        this.imgPostUrl = imgPostUrl;
    }

    public Map<Integer, CheckValid> getAlgorithmMap() {
        return Collections.unmodifiableMap(algorithmMap);
    }

    public void setAlgorithmMap(Map<Integer,CheckValid> algorithmMap) {
        this.algorithmMap = algorithmMap;
    }

    public CheckValid getCheckValid(Integer code) {
        return Optional.ofNullable(getAlgorithmMap().get(code))
                .orElseThrow(() -> new ServiceException("未配置对应算法code:" + code));
    }
}
