package com.gyee.runeconomy.model;

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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

/**
 * 部件信息
 */
public class TemperatureComponentInfo implements Serializable {
    private String name;
    private List<TemperatureItemInfo> temperatureItemInfos;
    /**
     * 是否排序
     */
    private boolean isSorted;

    public String getName() {
        return name;
    }

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

    public void addTemperaturePoint(Temperatureinfo ti) {
        if (ti.getName() == null || "".equals(ti.getName())) {
            return;
        }

        if (temperatureItemInfos == null) {
            temperatureItemInfos = new ArrayList<>();
        }

        TemperatureItemInfo ntii = new TemperatureItemInfo();
        ntii.setName(ti.getName());
        TemperatureTagInfo tti = new TemperatureTagInfo();
        tti.setMaxval(ti.getMaxval());
        tti.setReasonablemaxval(ti.getReasonablemaxval());
        tti.setTag(ti.getTag());
        ntii.setTemperatureTagInfo(tti);
        temperatureItemInfos.add(ntii);

        isSorted = false;
    }

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

    @JsonIgnore
    public List<TemperatureTagInfo> getTemperatureInfos() {
        List<TemperatureTagInfo> ls = new ArrayList<>();
        List<TemperatureItemInfo> tiis = getTemperatureItemInfos();
        for (TemperatureItemInfo tii : tiis) {
            ls.add(tii.getTemperatureTagInfo());
        }
        return ls;
    }

    /**
     * 是否含有特定状态的点
     *
     * @param ts
     * @return
     */
    public boolean isHadByStatus(Set<TemperatureStatus> ts) {
        return getTemperatureItemInfos().stream().anyMatch(wt -> ts.contains(wt.getStatus()));
    }

    /**
     * 是否含有特定状态的点
     *
     * @param ts
     * @return
     */
    public boolean isHadByStatus(TemperatureStatus ts) {
        return getTemperatureItemInfos().stream().anyMatch(wt -> wt.getStatus() == ts);
    }
}
