package cn.reghao.tnb.content.app.data.service;

import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.jutil.jdk.io.TextFile;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.api.dto.VideoRegion;
import cn.reghao.tnb.content.app.exam.model.chart.LineChartData;
import cn.reghao.tnb.content.app.vod.service.CategoryService;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-08-14 09:31:22
 */
@Service
public class ChartService {
    private final TextFile textFile = new TextFile();
    private final CategoryService categoryService;

    public ChartService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public LineChartData getLineChartData() {
        String filePath = "/home/reghao/Downloads/2010四川高考一分一段.txt";
        List<String> lines = textFile.read(filePath);
        Map<Integer, Integer> map = new TreeMap<>();
        for (String line : lines) {
            String[] arr = line.split("\\s+");
            int score = Integer.parseInt(arr[0].replace("分", ""));
            int num = Integer.parseInt(arr[1]);
            int total = Integer.parseInt(arr[2]);
            map.put(score, num);
        }

        int level1 = 512;
        int level2 = 441;
        int level3 = 413;
        int level4 = 361;
        int level0 = level1+100;
        int level5 = level4-50;

        Map<String, Integer> map1 = new TreeMap<>();
        int num0 = getNum(map, level0, 750);
        map1.put("level0", num0);

        int num1 = getNum(map, level1, level0);
        map1.put("level1", num1);

        int num2 = getNum(map, level2, level1);
        map1.put("level2", num2);

        int num3 = getNum(map, level3, level2);
        map1.put("level3", num3);

        int num4 = getNum(map, level4, level3);
        map1.put("level4", num4);

        int num5 = getNum(map, level5, level4);
        map1.put("level5", num5);

        int num6 = getNum(map, 1, level5);
        map1.put("level6", num6);

        int max = map1.values().stream().mapToInt(i -> i).max().orElse(-1);
        LineChartData chartData = new LineChartData("人数", 0, max, 10, "分数段");
        chartData.setXLabel(map1.keySet().toArray(new String[0]));
        chartData.setXValue(map1.values().toArray(new Integer[0]));
        return chartData;
    }

    private int getNum(Map<Integer, Integer> map, int start, int end) {
        int num = 0;
        for (int i = start; i < end; i++) {
            if (map.get(i) != null) {
                num += map.get(i);
            } else {
                System.out.println(i + " get null");
            }
        }

        return num;
    }

    public List<String> getWatchLineChart() {
        long loginUser = UserContext.getUserId();

        List<String> xAxis = new ArrayList<>();
        LocalDateTime current = LocalDateTime.now();
        int i = 8;
        while (i-- > 1) {
            LocalDateTime localDateTime = current.minusDays(i);
            String dtStr = DateTimeConverter.format(localDateTime);
            xAxis.add(dtStr.split(" ")[0]);
        }
        String xStr = xAxis.toString().replace("[", "").replace("]", "");

        List<String> yAxis = new ArrayList<>();
        for (int j = 0; j < 7; j++) {
            yAxis.add(String.format("%.2f", getRandomDouble()));
        }
        String yStr = yAxis.toString().replace("[", "").replace("]", "");

        List<String> list = new ArrayList<>();
        list.add(xStr);
        list.add(yStr);
        return list;
    }

    public List<String> getRegionPieChart() {
        List<VideoRegion> list = categoryService.getCategories(true);
        List<String> names = list.stream().map(VideoRegion::getName).collect(Collectors.toList());
        int total = names.size();
        List<String> xAxis = new ArrayList<>();
        int i = 8;
        while (i-- > 1) {
            xAxis.add(names.get(getRandomInt(total)));
        }
        String xStr = xAxis.toString().replace("[", "").replace("]", "");

        List<String> yAxis = new ArrayList<>();
        for (int j = 0; j < 7; j++) {
            yAxis.add(String.valueOf(getRandomInt(100)));
        }
        String yStr = yAxis.toString().replace("[", "").replace("]", "");

        List<String> dataList = new ArrayList<>();
        dataList.add(xStr);
        dataList.add(yStr);
        return dataList;
    }

    Random random = new SecureRandom();
    private double getRandomDouble() {
        int max = 86400;
        double randomNum = getRandomInt(max);
        return randomNum/3600;
    }

    private int getRandomInt(int max) {
        int min = 0;
        return min + random.nextInt(max - min);
    }
}
