package com.fs.statis.service.utils;

import com.fs.statis.dto.RewardMoneyTrendDTO;
import com.fs.statis.dto.WatchEndPlayTrendDTO;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 时序数据补全工具类
 */
public class TrendDataFiller {
    public static List<RewardMoneyTrendDTO> fillRewardHourData(List<RewardMoneyTrendDTO> data){

        boolean[] hourExists = new boolean[24];

        for (RewardMoneyTrendDTO dto : data) {
            int hour = Integer.parseInt(dto.getStartDate());
            if (hour >= 0 && hour < 24) {
                hourExists[hour] = true;
                dto.setX(String.valueOf(hour));
            }
        }

        // 填充不存在的小时数据
        for (int i = 0; i < 24; i++) {
            if (!hourExists[i]) {
                RewardMoneyTrendDTO dto = new RewardMoneyTrendDTO();
                dto.setX(String.valueOf(i));
                dto.setRewardMoney(BigDecimal.ZERO);
                data.add(dto);
            }
        }

        data.sort((a, b) -> {
            int intA = Integer.parseInt(a.getX());
            int intB = Integer.parseInt(b.getX());
            return Integer.compare(intA, intB);
        });
        return data;
    }

    /**
     * 填充日期段数据
     * @param data
     * @return
     */
    public static List<RewardMoneyTrendDTO> fillRewardDateSegmentData(List<RewardMoneyTrendDTO> data,String startDate,String endDate){
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate start = LocalDate.parse(startDate, dateTimeFormatter);
        LocalDate end = LocalDate.parse(endDate, dateTimeFormatter);

        LocalDate current = start;
        while (!current.isAfter(end)) {
            String dateStr = current.format(dateFormatter);
            boolean found = false;

            for (RewardMoneyTrendDTO dto : data) {
                if (dateStr.equals(dto.getStartDate())) {
                    found = true;
                    dto.setX(dateStr);
                    break;
                }
            }

            if (!found) {
                RewardMoneyTrendDTO dto = new RewardMoneyTrendDTO();
                dto.setStartDate(dateStr);
                dto.setX(dateStr);
                dto.setRewardMoney(BigDecimal.ZERO);
                data.add(dto);
            }

            current = current.plusDays(1);
        }

        data.sort((a, b) -> {
            LocalDate dateA = LocalDate.parse(a.getX(), dateFormatter);
            LocalDate dateB = LocalDate.parse(b.getX(), dateFormatter);
            return dateA.compareTo(dateB);
        });

        return data;
    }

    /**
     * 填充24小时类数据
     * @param data
     * @return
     */
    public static List<WatchEndPlayTrendDTO> fillHourData(List<WatchEndPlayTrendDTO> data){

        boolean[] hourExists = new boolean[24];

        for (WatchEndPlayTrendDTO dto : data) {
            int hour = Integer.parseInt(dto.getStartDate());
            if (hour >= 0 && hour < 24) {
                hourExists[hour] = true;
                dto.setX(String.valueOf(hour));
            }
        }

        // 填充不存在的小时数据
        for (int i = 0; i < 24; i++) {
            if (!hourExists[i]) {
                WatchEndPlayTrendDTO dto = new WatchEndPlayTrendDTO();
                dto.setX(String.valueOf(i));
                dto.setWatchUserCount(0L);
                dto.setCompletedUserCount(0L);
                data.add(dto);
            }
        }

        data.sort((a, b) -> {
            int intA = Integer.parseInt(a.getX());
            int intB = Integer.parseInt(b.getX());
            return Integer.compare(intA, intB);
        });
        return data;
    }

    /**
     * 填充日期段数据
     * @param data
     * @return
     */
    public static List<WatchEndPlayTrendDTO> fillDateSegmentData(List<WatchEndPlayTrendDTO> data,String startDate,String endDate){
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate start = LocalDate.parse(startDate, dateTimeFormatter);
        LocalDate end = LocalDate.parse(endDate, dateTimeFormatter);

        LocalDate current = start;
        while (!current.isAfter(end)) {
            String dateStr = current.format(dateFormatter);
            boolean found = false;

            for (WatchEndPlayTrendDTO dto : data) {
                if (dateStr.equals(dto.getStartDate())) {
                    found = true;
                    dto.setX(dateStr);
                    break;
                }
            }

            if (!found) {
                WatchEndPlayTrendDTO dto = new WatchEndPlayTrendDTO();
                dto.setStartDate(dateStr);
                dto.setX(dateStr);
                dto.setWatchUserCount(0L);
                dto.setCompletedUserCount(0L);
                data.add(dto);
            }

            current = current.plusDays(1);
        }

        data.sort((a, b) -> {
            LocalDate dateA = LocalDate.parse(a.getX(), dateFormatter);
            LocalDate dateB = LocalDate.parse(b.getX(), dateFormatter);
            return dateA.compareTo(dateB);
        });

        return data;
    }
}
