package com.gyee.runeconomy.service.monitor;

/*
@author   谢生杰
@date   2023/5/11-9:27
*/

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.DoubleUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicPowerstationPoint;
import com.gyee.runeconomy.model.auto.ProBasicWeatherStation;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.util.StringUtils;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AnemometerTowerService {
    @Resource
    private IEdosUtil edosUtil;

    @Resource
    private TokenService tokenService;

    public Map<String, Double> info(String weatherid) throws Exception {

        Map<String, Double> resultMap = new HashMap<>();


        Map<String, Map<String, ProBasicPowerstationPoint>> weatherwppointmap = CacheContext.weatherwppointmap;
        List<String> weapoints = new ArrayList<>();
        Map<String, ProBasicPowerstationPoint> powerstationPointMap = weatherwppointmap.get(weatherid);
        ProBasicPowerstationPoint wdpoint = powerstationPointMap.get(ContantXk.FCCFTWD);
        weapoints.add(wdpoint.getNemCode());
        ProBasicPowerstationPoint sdpoint = powerstationPointMap.get(ContantXk.FCCFTSD);
        weapoints.add(sdpoint.getNemCode());
        ProBasicPowerstationPoint kqmdpoint = powerstationPointMap.get(ContantXk.KQMD);
        weapoints.add(kqmdpoint.getNemCode());
        ProBasicPowerstationPoint yqpoint = powerstationPointMap.get(ContantXk.FCCFTYQ);
        weapoints.add(yqpoint.getNemCode());
        ProBasicPowerstationPoint fs10point = powerstationPointMap.get(ContantXk.FCCFTFS10);
        weapoints.add(fs10point.getNemCode());
        ProBasicPowerstationPoint fx10point = powerstationPointMap.get(ContantXk.FCCFTFX10);
        weapoints.add(fx10point.getNemCode());
        ProBasicPowerstationPoint fs30point = powerstationPointMap.get(ContantXk.FCCFTFS30);
        weapoints.add(fs30point.getNemCode());
        ProBasicPowerstationPoint fx30point = powerstationPointMap.get(ContantXk.FCCFTFX30);
        weapoints.add(fx30point.getNemCode());
        ProBasicPowerstationPoint fs50point = powerstationPointMap.get(ContantXk.FCCFTFS50);
        weapoints.add(fs50point.getNemCode());
        ProBasicPowerstationPoint fx50point = powerstationPointMap.get(ContantXk.FCCFTFX50);
        weapoints.add(fx50point.getNemCode());
        ProBasicPowerstationPoint fs70point = powerstationPointMap.get(ContantXk.FCCFTFS70);
        weapoints.add(fs70point.getNemCode());
        ProBasicPowerstationPoint fx70point = powerstationPointMap.get(ContantXk.FCCFTFX70);
        weapoints.add(fx70point.getNemCode());

        List<PointData> realData = edosUtil.getRealData(weapoints);
        if (StringUtils.isNotEmpty(realData)) {
            resultMap.put("wd", DoubleUtils.keepPrecision(realData.get(0).getPointValueInDouble(), 2));
            resultMap.put("sd", DoubleUtils.keepPrecision(realData.get(1).getPointValueInDouble(), 2));
            resultMap.put("kqmd", DoubleUtils.keepPrecision(realData.get(2).getPointValueInDouble(), 2));
            resultMap.put("yq", DoubleUtils.keepPrecision(realData.get(3).getPointValueInDouble(), 2));

            if (weatherid.contains("NJLF")) {
                resultMap.put("fs10", DoubleUtils.keepPrecision(realData.get(4).getPointValueInDouble() / 10, 2));
            } else {
                resultMap.put("fs10", DoubleUtils.keepPrecision(realData.get(4).getPointValueInDouble(), 2));
            }

            resultMap.put("fx10", DoubleUtils.keepPrecision(realData.get(5).getPointValueInDouble(), 2));

            if (weatherid.contains("NJLF")) {
                resultMap.put("fs30", DoubleUtils.keepPrecision(realData.get(6).getPointValueInDouble() / 10, 2));
            } else {
                resultMap.put("fs30", DoubleUtils.keepPrecision(realData.get(6).getPointValueInDouble(), 2));
            }

            if (weatherid.contains("NJLF")) {
                resultMap.put("fx30", DoubleUtils.keepPrecision(realData.get(7).getPointValueInDouble() / 10, 2));
            } else {
                resultMap.put("fx30", DoubleUtils.keepPrecision(realData.get(7).getPointValueInDouble(), 2));
            }

            if (weatherid.contains("NJLF")) {
                resultMap.put("fs50", DoubleUtils.keepPrecision(realData.get(8).getPointValueInDouble() / 100, 2));
            } else {
                resultMap.put("fs50", DoubleUtils.keepPrecision(realData.get(8).getPointValueInDouble(), 2));
            }
            resultMap.put("fx50", DoubleUtils.keepPrecision(realData.get(9).getPointValueInDouble(), 2));
            if (weatherid.contains("NJLF")) {
                resultMap.put("fs70", DoubleUtils.keepPrecision(realData.get(10).getPointValueInDouble() / 10, 2));
            } else {
                resultMap.put("fs70", DoubleUtils.keepPrecision(realData.get(10).getPointValueInDouble(), 2));
            }
            if (weatherid.contains("NJLF")) {
                resultMap.put("fx70", DoubleUtils.keepPrecision(realData.get(11).getPointValueInDouble() / 10, 2));
            } else {
                resultMap.put("fx70", DoubleUtils.keepPrecision(realData.get(11).getPointValueInDouble(), 2));
            }
        }
        return resultMap;
    }

    public List<ProBasicWeatherStation> towerByWp(String wpid) {


        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        List<ProBasicWeatherStation> weawpls = tokenService.getWeawpls(request);
        return weawpls.stream().filter(wea -> wea.getWindpowerstationId().equals(wpid)).collect(Collectors.toList());
    }
}
