package com.gyee.runeconomy.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gyee.runeconomy.model.auto.ProBasicEquipment;
import com.gyee.runeconomy.model.auto.Temperatureinfo;

import java.io.Serializable;
import java.util.*;

/**
 * 温度信息
 */
public class TemperatureInfo implements Serializable {
    /**
     * 风机信息
     */
    private ProBasicEquipment windturbineInfo;
    private String windturbineId;
    private String code;
    private String stationId;
    private String stationName;
    private Map<String, TemperatureComponentInfo> temperatureComponentInfoMap;
    private List<TemperatureComponentInfo> temperatureComponentInfos;
    private int windturbineIndex;

    /**
     * 是否排序
     */
    private boolean isSorted;

    public void setWindturbineInfo(ProBasicEquipment windturbineInfo) {
        this.windturbineInfo = windturbineInfo;
    }

    public String getWindturbineId() {
        return windturbineId;
    }

    public void setWindturbineId(String windturbineId) {
        this.windturbineId = windturbineId;
    }

    public String getStationId() {
        return stationId;
    }

    public void setStationId(String stationId) {
        this.stationId = stationId;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }



    /**
     * 转速
     */
//    public double getRollSpeed() {
//        if (windturbineInfo == null) {
//            return 0;
//        }
//        return windturbineInfo.getRollSpeed();
//    }

    /**
     * 功率
     */
//    public double getPower() {
//        if (windturbineInfo == null) {
//            return 0;
//        }
//        return windturbineInfo.getPower();
//    }

    /**
     * 风速
     */
//    public double getWindSpeed() {
//        if (windturbineInfo == null) {
//            return 0;
//        }
//        return windturbineInfo.getWindSpeed();
//    }

    /**
     * 状态
     *
     * @return
     */
//    public double getStatus() {
//        if (windturbineInfo == null) {
//            return 0;
//        }
//        return windturbineInfo.getStatus();
//    }

    /**
     * 期次
     *
     * @return
     */
    public String getProjectId() {
        if (windturbineInfo == null) {
            return "";
        }
        return windturbineInfo.getProjectId();
    }

    public String getStationName() {
        return stationName;
    }

    public void setStationName(String stationName) {
        this.stationName = stationName;
    }

    @JsonIgnore
    public Map<String, TemperatureComponentInfo> getTemperatureComponentInfoMap() {
        if (temperatureComponentInfoMap == null) {
            temperatureComponentInfoMap = new HashMap<>();
        }
        return temperatureComponentInfoMap;
    }

    /**
     * 添加温度点信息
     */
    public void addTemperaturePoint(Temperatureinfo ti) {
        if (ti.getComponent() == null || "".equals(ti.getComponent())) {
            return;
        }
        Map<String, TemperatureComponentInfo> tcim = getTemperatureComponentInfoMap();
        if (temperatureComponentInfos == null) {
            temperatureComponentInfos = new ArrayList<>();
        }
        if (!tcim.containsKey(ti.getComponent())) {
            TemperatureComponentInfo ntci = new TemperatureComponentInfo();
            ntci.setName(ti.getComponent());
            tcim.put(ti.getComponent(), ntci);
            temperatureComponentInfos.add(ntci);
        }
        tcim.get(ti.getComponent()).addTemperaturePoint(ti);
        isSorted = true;
    }


    public List<TemperatureComponentInfo> getTemperatureComponentInfos() {
        if (temperatureComponentInfos == null) {
            temperatureComponentInfos = new ArrayList<>();
        }
        if (!isSorted) {
            temperatureComponentInfos.sort(Comparator.comparing(TemperatureComponentInfo::getName));
            isSorted = true;
        }
        return temperatureComponentInfos;
    }

    @JsonIgnore
    public List<TemperatureTagInfo> getTemperatureInfos() {
        List<TemperatureTagInfo> ls = new ArrayList<>();
        List<TemperatureComponentInfo> tcis = getTemperatureComponentInfos();
        for (TemperatureComponentInfo tci : tcis) {
            List<TemperatureTagInfo> tis = tci.getTemperatureInfos();
            ls.addAll(tis);
        }
        return ls;
    }

    @JsonIgnore
    public int getWindturbineIndex() {
        if (windturbineIndex <= 0) {
            String[] ss = windturbineId.split("_");
            if (ss.length == 2) {
                try {
                    windturbineIndex = Integer.parseInt(ss[1]);
                } catch (Exception e) {
                }
            }
        }
        return windturbineIndex;
    }

    /**
     * 是否含有特定状态的点
     *
     * @param tts 状态字符串
     * @return
     */
    public boolean isHadByStatus(String[] tts) {
        final List<TemperatureComponentInfo> tcis = getTemperatureComponentInfos();
        Set<TemperatureStatus> tss = new HashSet<>();
        try {
            for (String s : tts) {
                if (s == null || "".equals(s)) {
                    continue;
                }
                tss.add(TemperatureStatus.valueOf(s));
            }
        } catch (Exception e) {
        }

        return tcis.stream().anyMatch(tc -> tc.isHadByStatus(tss));
    }


    /**
     * 是否含有特定状态的点
     *
     * @param ts 状态
     * @return
     */
    public boolean isHadByStatus(TemperatureStatus ts) {
        final List<TemperatureComponentInfo> tcis = getTemperatureComponentInfos();
        return tcis.stream().anyMatch(tc -> tc.isHadByStatus(ts));
    }

    /**
     * 初始化控制信息
     *
     * @param controlType 控制类型
     */
//    public void initControlInto(OperateStyle controlType) {
//        if (controlType == OperateStyle.Start) {
//            final List<TemperatureTagInfo> temperatureInfos = getTemperatureInfos().stream().filter(ti -> ti.getStopMaxValue() != 0).collect(Collectors.toList());
//            for (TemperatureTagInfo ti : temperatureInfos) {
//                ti.setStopMaxValue(0);
//            }
//        } else if (controlType == OperateStyle.Stop && isHadByStatus(TemperatureStatus.OverLimit)) {
//            final List<TemperatureTagInfo> temperatureInfos = getTemperatureInfos().stream().filter(ti -> ti.getStatus() == TemperatureStatus.OverLimit).collect(Collectors.toList());
//            for (TemperatureTagInfo ti : temperatureInfos) {
//                ti.setStopMaxValue(ti.getValue());
//            }
//        }
//    }
}

