package com.jiayue.insu.inclientqn.service.client;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.jiayue.insu.inclientqn.constant.CommonStant;
import com.jiayue.insu.inclientqn.constant.TemplateStant;
import com.jiayue.insu.inclientqn.entity.BackForeForecastData;
import com.jiayue.insu.inclientqn.entity.BackForeUsForecastData;
import com.jiayue.insu.inclientqn.entity.BackStatData;
import com.jiayue.insu.inclientqn.inenum.ElectricFieldTypeEnum;
import com.jiayue.insu.inclientqn.inenum.QNHLEnum;
import com.jiayue.insu.inclientqn.model.RequestVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * @description: 回传数据请求生成
 * @author: yuanhao
 * @createDate: 2022/6/17
 * @version: 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
@RefreshScope
public class BackDataGenService {

    /**
     * 生成回传实际功率
     *
     * @param el 场站标识
     * @return RequestVo
     */
    public RequestVo genStateRealPowerData(String el, long signTime, List<BackStatData> backStatDatas) throws Exception {

        RequestVo requestVo = null;

        if (CollectionUtil.isNotEmpty(backStatDatas) && backStatDatas.get(0).getRealPower() != null) {
            StringBuilder content = new StringBuilder();

            String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, el, DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
            String labelStart = TemplateStant.GEN_LABELSTART( QNHLEnum.DATA_STAT_TYPE_RealPower.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND( QNHLEnum.DATA_STAT_TYPE_RealPower.getCode());
            String title = TemplateStant.BACK_STAT_REAL_POWER_TITLE;
            content.append(header);
            content.append(labelStart);
            content.append(title);

            String template = TemplateStant.BACK_STAT_REAL_POWER_TEMPLATE;

            String dataContent = StrUtil.format(template, backStatDatas.get(0).getRealPower().toString());

            content.append(dataContent);
            content.append(labelEnd);

            requestVo = RequestVo.UPLOAD_STAT(el, content.toString(), signTime);
        }

        return requestVo;
    }


    /**
     * 生成回传实际气象
     *
     * @param el          场站标识
     * @param stationType 场站类型
     * @return RequestVo
     */
    public RequestVo genStateRealWeatherData(String el, String stationType, long signTime, List<BackStatData> backStatDatas) throws Exception {

        RequestVo requestVo = null;

        if (CollectionUtil.isNotEmpty(backStatDatas)) {
            StringBuilder content = new StringBuilder();

            String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, el, DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
            String labelStart = TemplateStant.GEN_LABELSTART( QNHLEnum.DATA_STAT_TYPE_RealWeather.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND( QNHLEnum.DATA_STAT_TYPE_RealWeather.getCode());
            content.append(header);
            content.append(labelStart);
            String title = "";
            String template = "";
            String dataContent = "";
            BackStatData backStatData = backStatDatas.get(0);

            if (stationType.equals(ElectricFieldTypeEnum.E2.name())) {

                title = TemplateStant.BACK_STAT_REAL_WEATHER_TITLE_E2;
                template = TemplateStant.BACK_STAT_REAL_WEATHER_TEMPLATE_E2;
                dataContent = StrUtil.format(template, backStatData.getWs().toString(), backStatData.getWd().toString()
                        , backStatData.getTemperature().toString(), backStatData.getHumidity().toString(), backStatData.getPressure().toString());

            } else {

                title = TemplateStant.BACK_STAT_REAL_WEATHER_TITLE_E1;
                template = TemplateStant.BACK_STAT_REAL_WEATHER_TEMPLATE_E1;
                dataContent = StrUtil.format(template, backStatData.getGlobalR().toString(), backStatData.getDirectR().toString()
                        , backStatData.getDiffuseR().toString(), backStatData.getTemperature().toString(), backStatData.getCellT().toString(),
                        backStatData.getHumidity().toString(), backStatData.getPressure().toString(), backStatData.getWs().toString(), backStatData.getWd().toString()
                );

            }

            content.append(title);
            content.append(dataContent);
            content.append(labelEnd);

            requestVo = RequestVo.UPLOAD_STAT(el, content.toString(), signTime);
        }

        return requestVo;
    }


    /**
     * 生成短期上报
     *
     * @param el 场站标识
     * @return RequestVo
     */
    public RequestVo genStateForecastPowerReport(String el, long signTime, List<BackStatData> backStatDatas) throws Exception {

        RequestVo requestVo = null;

        if (CollectionUtil.isNotEmpty(backStatDatas) && backStatDatas.get(0).getReportStateForecast() != null) {
            StringBuilder content = new StringBuilder();

            String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, el, DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
            String labelStart = TemplateStant.GEN_LABELSTART( QNHLEnum.DATA_STAT_TYPE_ForecastPowerReport.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND( QNHLEnum.DATA_STAT_TYPE_ForecastPowerReport.getCode());
            String title = TemplateStant.BACK_FORE_FORECAST_TITLE_REPORT;

            content.append(header);
            content.append(labelStart);
            content.append(title);

            String template = TemplateStant.BACK_FORE_FORECAST_TEMPLATE_REPORT;

            String dataContent = StrUtil.format(template, backStatDatas.get(0).getReportStateForecast().toString());

            content.append(dataContent);
            content.append(labelEnd);

            requestVo = RequestVo.UPLOAD_STAT(el, content.toString(), signTime);
        }

        return requestVo;
    }

    /**
     * 生成超短期上报
     *
     * @param el 场站标识
     * @return RequestVo
     */
    public RequestVo genStateUltraShortTermForecastReport(String el, long signTime, List<BackStatData> backStatDatas) throws Exception {

        RequestVo requestVo = null;

        if (CollectionUtil.isNotEmpty(backStatDatas) && backStatDatas.get(0).getReportStateForecastshort() != null) {
            StringBuilder content = new StringBuilder();

            String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, el, DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
            String labelStart = TemplateStant.GEN_LABELSTART( QNHLEnum.DATA_STAT_TYPE_UltraShortTermForecastReport.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND( QNHLEnum.DATA_STAT_TYPE_UltraShortTermForecastReport.getCode());
            String title = TemplateStant.BACK_FORE_US_FORECAST_TITLE_REPORT;

            content.append(header);
            content.append(labelStart);
            content.append(title);

            String template = TemplateStant.BACK_FORE_US_FORECAST_TITLE_TEMPLATE;

            String dataContent = StrUtil.format(template, backStatDatas.get(0).getReportStateForecastshort().toString());

            content.append(dataContent);
            content.append(labelEnd);

            requestVo = RequestVo.UPLOAD_STAT(el, content.toString(), signTime);
        }


        return requestVo;
    }

    public RequestVo genForeUltraShortTermForecast(String el, String stationType, long signTime, List<BackForeUsForecastData> backForeUsForecastDatas) throws Exception {

        RequestVo requestVo = null;

        if (CollectionUtil.isNotEmpty(backForeUsForecastDatas) && backForeUsForecastDatas.size() == 16) {

            Collections.sort(backForeUsForecastDatas, Comparator.comparing(BackForeUsForecastData::getForecastTime));
            StringBuilder content = new StringBuilder();
            String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, el, DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
            String labelStart = TemplateStant.GEN_LABELSTART( QNHLEnum.DATA_FORE_TYPE_UltraShortTermForecast.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND( QNHLEnum.DATA_FORE_TYPE_UltraShortTermForecast.getCode());

            String title = "";
            String template = "";

            content.append(header);
            content.append(labelStart);

            if (stationType.equals(ElectricFieldTypeEnum.E2.name())) {
                title = TemplateStant.BACK_FORE_US_FORECAST_TITLE_E2;
                template = TemplateStant.BACK_FORE_US_FORECAST_TEMPLATE_E2;

            } else {
                title = TemplateStant.BACK_FORE_US_FORECAST_TITLE_E1;
                template = TemplateStant.BACK_FORE_US_FORECAST_TEMPLATE_E1;

            }

            content.append(title);

            for (int i = 0; i < backForeUsForecastDatas.size(); i++) {

                BackForeUsForecastData b = backForeUsForecastDatas.get(i);
                String format = LocalDateTimeUtil.format(b.getForecastTime(), DatePattern.PURE_DATETIME_PATTERN);
                String dataContent = StrUtil.format(template, i, format, b.getForecastValue(), b.getForecastOpenCap());
                content.append(dataContent);
            }

            content.append(labelEnd);

            requestVo = RequestVo.UPLOAD_FORE(el, content.toString(), signTime);

        }

        return requestVo;
    }


    public RequestVo genForeForecast(String el, long signTime, List<BackForeForecastData> backForeForecastDatas) throws Exception {

        RequestVo requestVo = null;

        if (CollectionUtil.isNotEmpty(backForeForecastDatas)) {

            boolean checkDate = true;

            if (checkDate) {
                Collections.sort(backForeForecastDatas, Comparator.comparing(BackForeForecastData::getForecastTime));
                StringBuilder content = new StringBuilder();
                String header = StrUtil.format(TemplateStant.HEADER_TEMPLATE, el, DateUtil.parse(DateUtil.now()).toString("yyyy-MM-dd HH:mm:ss"));
                String labelStart = TemplateStant.GEN_LABELSTART( QNHLEnum.DATA_FORE_TYPE_ForecastPower.getCode());
                String labelEnd = TemplateStant.GEN_LABELEND( QNHLEnum.DATA_FORE_TYPE_ForecastPower.getCode());

                String title = TemplateStant.BACK_FORE_FORECAST_TITLE;
                content.append(header);
                content.append(labelStart);
                content.append(title);

                String template = TemplateStant.BACK_FORE_FORECAST_TEMPLATE;

                for (int i = 0; i < backForeForecastDatas.size(); i++) {

                    BackForeForecastData b = backForeForecastDatas.get(i);
                    String format = LocalDateTimeUtil.format(b.getForecastTime(), DatePattern.PURE_DATETIME_PATTERN);
                    String dataContent = StrUtil.format(template, i, format, b.getForecastValue(), b.getForecastOpenCap());
                    content.append(dataContent);
                }

                content.append(labelEnd);

                requestVo = RequestVo.UPLOAD_FORE(el, content.toString(), signTime);
            }
        }

        return requestVo;
    }

}
