package com.gyee.runeconomy.service.Windresourceanalysis;

import com.gyee.common.contant.ContantXk;
import com.gyee.runeconomy.dto.WindspeedVo;
import com.gyee.runeconomy.dto.speed.PowerPointData;
import com.gyee.runeconomy.dto.speed.WindDirectionALG;
import com.gyee.runeconomy.feign.AdapterApi;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.PointData;
import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
import com.gyee.runeconomy.model.auto.ProBasicPowerstationPoint;
import com.gyee.runeconomy.model.auto.ProBasicWeatherStation;
import com.gyee.runeconomy.service.auto.IProBasicPowerstationPointService;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay1Service;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class WindresourceanalysisService2 {

    @Resource
    private IProEconPowerstationInfoDay1Service proEconPowerstationInfoDay1Service;

    @Resource
    private IProBasicPowerstationPointService proBasicPowerstationPointService;

    @Resource
    private IEdosUtil edosUtil;

    @Resource
    private AdapterApi adapterApi;

    public URI goldenUri() {
        return URI.create("http://10.220.1.8:8011/ts");
    }



    /**
     * 风向玫瑰图\风向频率玫瑰图
     *
     * @param wpid 预处理数据的id
     * @param mode 0:单台风机  1:多台风机合并
     * @return
     */
    public Object fxRoses(String wpid, int mode, String Data) throws Exception {

        List<ProBasicWeatherStation> wplsf = CacheContext.weawpls.stream().filter(w -> w.getWindpowerstationId().equals(wpid)).collect(Collectors.toList());

        //转换时间为Date类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(Data);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        Date startTime = calendar.getTime();

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);

        // 获取当前日期
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        // 判断是否是当天
        if (calendar1.get(Calendar.YEAR) == today.get(Calendar.YEAR) &&
                calendar1.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) {
            // 当天，设置endTime为当前时间
            Date now = new Date();
            calendar1.setTime(now);
        } else {
            // 非当天，设置endTime为传入的date的23:59:59
            calendar1.set(Calendar.HOUR_OF_DAY, 23);
            calendar1.set(Calendar.MINUTE, 59);
            calendar1.set(Calendar.SECOND, 59);
        }
        Date endTime = calendar1.getTime();

        ProBasicPowerstationPoint pjfs = null;
        ProBasicPowerstationPoint pjfx = null;

        List<WindspeedVo> vos = new ArrayList<>();

        for (ProBasicWeatherStation ww : wplsf) {
            if (ww.getId().contains("GJYF02")) continue;

            pjfs = proBasicPowerstationPointService.getPowerstationPoint(ww.getId(), ContantXk.FCCFTFS70);
            pjfx = proBasicPowerstationPointService.getPowerstationPoint(ww.getId(), ContantXk.FCCFTFX70);

            List<ProBasicPowerstation> collect = CacheContext.wplsf.stream().filter(c -> c.getId().equals(ww.getWindpowerstationId())).collect(Collectors.toList());

            if (pjfs.getNemCode() != null && !pjfs.getNemCode().equals("INITIAL")) {

                List<PointData> pjfsls = adapterApi.getHistorySnap(goldenUri(), pjfs.getNemCode(), startTime.getTime(), endTime.getTime(), 900);
                List<PointData> pjfxls = adapterApi.getHistorySnap(goldenUri(), pjfx.getNemCode(), startTime.getTime(), endTime.getTime(), 900);
                double temp1 = 0;
                for (int i = 0; i < pjfsls.size(); i++) {
                    WindspeedVo vo = new WindspeedVo();
                    temp1 = pjfsls.get(i).getDoubleValue();
                    vo.setWpid(ww.getId());
                    vo.setHours(pjfsls.get(i).getTs());
                    vo.setPjfs(temp1);
                    vo.setPjfx(pjfxls.get(i).getDoubleValue());
                    vos.add(vo);
                }

                if (pjfxls != null && pjfxls.size() > 1) {
                    vos.stream().forEach(i -> {
                        Optional<PointData> optional = pjfxls.stream()
                                .filter(j -> j.getTime().equals(i.getHours()))
                                .findFirst();
                        if (optional.isPresent()) {
                            PointData tqinfoday = optional.get();
                            i.setPjfx(tqinfoday.getDoubleValue());
                        }
                    });
                }
            }

        }

        List<Object> result = new ArrayList<>();
        if (mode == 0) {

            Map<String, Object> map = new HashMap<>();
            List<PowerPointData> ls = new ArrayList<>();
            for (WindspeedVo v : vos) {
                PowerPointData data = new PowerPointData();
                data.setFx(v.getPjfx());
                data.setSpeed(v.getPjfs());
                ls.add(data);
            }
            map.put("wt", wpid);
            map.put("roses", WindDirectionALG.fxRoses(ls));
            map.put("count", WindDirectionALG.fxCountRoses(ls));
            //                map.put("radar", WindDirectionALG.fxRadarRoses(ls));
            //                map.put("frequency", WindDirectionALG.windDeviationPoint(ls));
            result.add(map);

        }
        if (mode == 1) {
            List<PowerPointData> ls = new ArrayList<>();
            for (WindspeedVo obj : vos)
                ls.addAll(csvParse(obj));
            Map<String, Object> map = new HashMap<>();
            map.put("wt", "merge");
            map.put("roses", WindDirectionALG.fxRoses(ls));
            map.put("count", WindDirectionALG.fxCountRoses(ls));
            //            map.put("radar", WindDirectionALG.fxRadarRoses(ls));
            //            map.put("frequency", WindDirectionALG.windDeviationPoint(ls));
            result.add(map);
        }

        return result;
    }

    /**
     * csv 文件解析成对象
     *
     * @param obj
     * @return
     */
    private List<PowerPointData> csvParse(WindspeedVo obj) {
        List<PowerPointData> ls = new ArrayList<>();
        //        List<String> content = FileUtil.readFile(obj.getPath(), true);
        //        for (int i = 1; i < content.size(); i++){
        //            String[] split = content.get(i).split(",");
        PowerPointData data = new PowerPointData();
        //            if (data.getSpeed() < 0 || data.getPower() < 0)
        //                continue;
        data.setWtId(obj.getWpid());
        data.setFx(obj.getPjfx());
        data.setSpeed(obj.getPjfs());
        ls.add(data);
        //        }
        return ls;
    }
}