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 BackDataGenBodyService {

    /**
     * 生成回传实际功率
     *
     * @param backStatDatas 数据集
     * @return RequestVo
     */
    public StringBuilder genStateRealPowerData(List<BackStatData> backStatDatas) {

        StringBuilder content = new StringBuilder();

        if (backStatDatas.get(0).getRealPower() != null) {

            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(labelStart);
            content.append(title);

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

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


        return content;
    }


    /**
     * 生成回传实际气象
     *
     * @param stationType   场站类型
     * @param backStatDatas 数据集
     * @return RequestVo
     */
    public StringBuilder genStateRealWeatherData(String stationType, List<BackStatData> backStatDatas) {

        StringBuilder content = new StringBuilder();


        try {
            String labelStart = TemplateStant.GEN_LABELSTART(QNHLEnum.DATA_STAT_TYPE_RealWeather.getCode());
            String labelEnd = TemplateStant.GEN_LABELEND(QNHLEnum.DATA_STAT_TYPE_RealWeather.getCode());

            content.append(labelStart);
            String title = "";
            String dataContent = "";
            BackStatData backStatData = backStatDatas.get(0);

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

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

            } else {

                title = TemplateStant.BACK_STAT_REAL_WEATHER_TITLE_E1;
                dataContent = StrUtil.format(TemplateStant.BACK_STAT_REAL_WEATHER_TEMPLATE_E1, 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);
        }catch (Exception e){
            log.warn("回传数据-实际气象信息缺少数据");
            content = new StringBuilder();
        }




        return content;
    }


    /**
     * 生成短期上报
     *
     * @param backStatDatas 结果集
     * @return RequestVo
     */
    public StringBuilder genStateForecastPowerReport(List<BackStatData> backStatDatas) {

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

            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(labelStart);
            content.append(title);

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

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

        return content;
    }


    /**
     * 生成超短期上报
     *
     * @param backStatDatas 数据集
     * @return RequestVo
     */
    public StringBuilder genStateUltraShortTermForecastReport(List<BackStatData> backStatDatas) {


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

            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(labelStart);
            content.append(title);

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

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

        }

        return content;
    }


    public StringBuilder genForeUltraShortTermForecast(String stationType, List<BackForeUsForecastData> backForeUsForecastDatas) {


        StringBuilder content = new StringBuilder();

        Collections.sort(backForeUsForecastDatas, Comparator.comparing(BackForeUsForecastData::getForecastTime));

        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(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);


        return content;
    }


    public StringBuilder genForeForecast(List<BackForeForecastData> backForeForecastDatas) {

        StringBuilder content = new StringBuilder();

        Collections.sort(backForeForecastDatas, Comparator.comparing(BackForeForecastData::getForecastTime));

        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(labelStart);
        content.append(title);

        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(TemplateStant.BACK_FORE_FORECAST_TEMPLATE, i + 1, format, b.getForecastValue(), b.getForecastOpenCap());
            content.append(dataContent);
        }

        content.append(labelEnd);

        return content;
    }


}
