package jtznyj.engineer.service.dataDiagnosticService.data;

import cn.hutool.core.date.DateUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 监控预警区间集
 */
public class MonitorAlarmRanges{
    /**
     * 模型名称
     */
    public String groupName = "";

    /**
     * 模型展示颜色
     */
    public String modelColor= "";

    /**
     * 子区间
     */
    public ArrayList<MonitorAlarmRange> children;

    public MonitorAlarmRanges(String groupName){
        this.groupName = groupName;
        this.children = new ArrayList<>();
    }

    public boolean isEmpty(){
        return this.children.isEmpty();
    }

    /**
     * 合并监控预警信息（按照时间倒序合并数据）--descriptions隶属同一组报警
     * @param currentDate 当前时间--小
     * @param preSiblingDate 上一个时间点（大）
     */
    public boolean tryAddMonitorDescription(Date preSiblingDate, Date currentDate, AlarmPoint description) {
        if(description == null || null == currentDate){
            return false;
        }
        MonitorAlarmRange iFind = null;
        if(preSiblingDate!=null){
            for (MonitorAlarmRange range: this.children) {
                if(preSiblingDate.getTime() >= range.startDate.getTime() && preSiblingDate.getTime()<= range.endDate.getTime()){
                    iFind = range;
                    break;
                }
            }
        }

        //判断时间点是否在已有区间
        if(iFind==null){
            iFind = new MonitorAlarmRange(currentDate,currentDate);
            iFind.startDepth = description.depth;
            iFind.startZtDepth = description.ztDepth;
            iFind.endDepth = description.depth;
            iFind.endZtDepth = description.ztDepth;
            iFind.details.add(description);
            this.children.add(0,iFind);
            return true;
        }
        else{
            //相同的起点的时候
            iFind.startDate.setTime(currentDate.getTime());
            iFind.startDepth = description.depth;
            iFind.startZtDepth = description.ztDepth;
            if(!iFind.details.contains(description)){
                iFind.details.add(description);
            }
        }
        return true;
    }

    /**
     * 合并监控预警信息（按照时间倒序合并数据）--descriptions隶属同一组报警
     * @param currentDate 当前时间--小
     * @param preSiblingDate 上一个时间点（大）
     */
    public boolean tryAddMonitorDescriptions(Date preSiblingDate, Date currentDate, ArrayList<AlarmPoint> descriptions,String jh) {
        if(descriptions == null || null == currentDate){
            return false;
        }
        if(descriptions.size()==0){
            return false;
        }
        MonitorAlarmRange iFind = null;
        if(preSiblingDate!=null){
            for (MonitorAlarmRange range: this.children) {
                if(preSiblingDate.getTime() >= range.startDate.getTime() && preSiblingDate.getTime()<= range.endDate.getTime()){
                    iFind = range;
                    break;
                }
            }
        }

        //判断时间点是否在已有区间
        if(iFind==null){
            iFind = new MonitorAlarmRange(currentDate,currentDate);
            iFind.startDepth = descriptions.get(0).depth;
            iFind.startZtDepth = descriptions.get(0).ztDepth;
            iFind.endDepth = descriptions.get(0).depth;
            iFind.endZtDepth = descriptions.get(0).ztDepth;
            iFind.description = this.setDescription(iFind,jh,descriptions.get(0).WarningCause);
            iFind.details.addAll(descriptions);
            this.children.add(0,iFind);
            return true;
        }
        else{
            //相同的起点的时候
            iFind.startDate.setTime(currentDate.getTime());
            iFind.startDepth = descriptions.get(0).depth;
            iFind.startZtDepth = descriptions.get(0).ztDepth;
            iFind.description = this.setDescription(iFind,jh,descriptions.get(0).WarningCause);
            for (int i = 0; i < descriptions.size(); i++) {
                if(!iFind.details.contains(descriptions.get(i))){
                    iFind.details.add(descriptions.get(i));
                }
            }
        }
        return true;
    }

    private String setDescription(MonitorAlarmRange monitorAlarmRange ,String jh,String des){
        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("[ %s %s ]",  jh  , this.groupName ));
        sb.append("<br/>");
        sb.append(String.format("时间：%s 至 %s", DateUtil.format(monitorAlarmRange.startDate,"yyyy-MM-dd HH:mm:ss"),DateUtil.format(monitorAlarmRange.endDate,"yyyy-MM-dd HH:mm:ss")));
        sb.append("<br/>");
        sb.append(String.format("深度段:%s～%sm", decimalFormat.format(monitorAlarmRange.startDepth), decimalFormat.format(monitorAlarmRange.endDepth)));
        sb.append("<br/>");
        sb.append(String.format("%s", des));
        return sb.toString();
    }
}