package com.gic.haoban.manage.web.utils;

import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.DataApiUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @Author guojx
 * @Date 2023/11/29 10:49
 */
@Slf4j
public class DateFillUtils {

    public static List<String> getDay(String date) {
        List<String> dateList = new ArrayList<>();
        try {
            Calendar calendar = Calendar.getInstance();

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            calendar.setTime(df.parse(date));

            calendar.add(Calendar.DATE, -29);

            String temp = df.format(calendar.getTime());
            while (temp.compareTo(date) <= 0) {
                dateList.add(temp);
                calendar.add(Calendar.DATE, 1);
                temp = df.format(calendar.getTime());
            }
        } catch (Exception e) {
            log.error("时间匹配调整：{}", e.getMessage(), e);
        }
        return dateList;
    }

    public static List<String> getCustom(String start, String end) {
        List<String> dateList = new ArrayList<>();
        try {
            Calendar calendar = Calendar.getInstance();

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            calendar.setTime(df.parse(start));

            String temp = df.format(calendar.getTime());
            while (temp.compareTo(end) <= 0) {
                dateList.add(temp.substring(5));
                calendar.add(Calendar.DATE, 1);
                temp = df.format(calendar.getTime());
            }
        } catch (Exception e) {
            log.error("时间匹配调整：{}", e.getMessage(), e);
        }
        return dateList;
    }

    public static List<String> getWeek(String date) {
        List<String> dateList = new ArrayList<>();

        try {
            Calendar calendar = Calendar.getInstance();

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            calendar.setTime(df.parse(date));
            calendar.add(Calendar.WEEK_OF_YEAR, -11);
            String temp = df.format(calendar.getTime());

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pageNum", 1);
            jsonObject.put("pageSize", 20);
            jsonObject.put("startDate", temp);
            jsonObject.put("endDate", date);
            Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), "data_pub_date_week");
            List<JSONObject> list = DataApiUtils.getPageList(res);
            for (JSONObject json : list) {
                dateList.add(json.getString("weekYear"));
            }
            log.info("week data:{}", list);
        } catch (Exception e) {
            log.error("时间匹配调整：{}", e.getMessage(), e);
        }

        return dateList;
    }

    public static List<String> getYear(String date) {
        List<String> dateList = new ArrayList<>();
        Integer year = Integer.parseInt(date);
        Integer min = year - 11;
        for (int i = min; i <= year; i++) {
            dateList.add(i + "");
        }
        return dateList;
    }

    public static List<String> getMonth(String date) {
        List<String> dateList = new ArrayList<>();
        try {
            Calendar calendar = Calendar.getInstance();

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            calendar.setTime(df.parse(date));

            calendar.add(Calendar.MONTH, -11);

            String temp = df.format(calendar.getTime());
            while (temp.compareTo(date) <= 0) {
                dateList.add(temp);
                calendar.add(Calendar.MONTH, 1);
                temp = df.format(calendar.getTime());
            }
        } catch (Exception e) {
            log.error("时间匹配调整：{}", e.getMessage(), e);
        }
        return dateList;
    }

    public static void main(String[] args) {
        log.info("" + JSONObject.toJSONString(getWeek("2023-11-27")));
    }
}
