package com.gyee.runeconomy.controller;

import com.gyee.common.model.PointData;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.TagInfo;
import com.gyee.runeconomy.model.TemperatureInfo;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/api")
public class TemperatureInfoController {

    @Resource
    private IEdosUtil edosUtil;

    /**
     * 温度矩阵信息
     */
//    private List<TemperatureInfo> temperatureInfos =new ArrayList<>();


    private Collection<TemperatureInfo> getPage(List<TemperatureInfo> ls, Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            return ls;
        }
        int pagenum = pageNum < 1 ? 1 : pageNum;
        return ls.stream().skip((long) (pagenum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
    }



    /**
     * 获取温度信息
     *
     * @param id 场站ID
     */
    @GetMapping("/windturbine/temperature-info")
    public Collection<TemperatureInfo> getStationTemperatureInfo(@RequestParam(value = "id", required = false) String id,
                                                                 @RequestParam(value = "status", required = false) String ts,
                                                                 @RequestParam(value = "projects", required = false) String projects,
                                                                 @RequestParam(value = "pagenum", required = false) Integer pageNum,
                                                                 @RequestParam(value = "pagesize", required = false) Integer pageSize) throws Exception {
        List<TemperatureInfo> ls = null;
        if (id == null || "".equals(id)) {
            ls = CacheContext.temperatureInfos;
        } else {
            ls = CacheContext.temperatureInfos.stream().filter(t -> t.getStationId().equals(id)).collect(Collectors.toList());
        }
        if (ts != null && !"".equals(ts)) {
            String[] tts = ts.split(",");
            ls = ls.stream().filter(wt -> wt.isHadByStatus(tts)).collect(Collectors.toList());
        }
        addTemperatureInfo(ls);
        if (projects == null || "".equals(projects)) {
            return getPage(ls, pageNum, pageSize);
        }
        String[] ps = projects.split(",");
        Set<String> psset = Arrays.stream(ps).collect(Collectors.toSet());

        ls = ls.stream().filter(w -> psset.contains(w.getProjectId())).collect(Collectors.toList());

        addTemperatureInfo(ls);
        return getPage(ls, pageNum, pageSize);
    }


    private void addTemperatureInfo(List<TemperatureInfo> ls) throws Exception {
        List<String> tag = new ArrayList<>();
        List<TagInfo> tagInfos = new ArrayList<>();
        for (TemperatureInfo tem : ls) {
            tagInfos.addAll(tem.getTemperatureInfos());
        }
        for (TagInfo tagInfo : tagInfos) {
            tag.add(tagInfo.getTag());
        }
        List<PointData> realData = edosUtil.getRealData(tag);
        for (TagInfo tagInfo : tagInfos) {
            for (PointData real : realData) {
                if (real.getPointName().equals(tagInfo.getTag())) {
                    tagInfo.setValue(real.getPointValueInDouble());
                    tagInfo.setTimestamp(real.getPointTime());
                }
            }
        }

    }


}
