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

import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.DataApiUtils;
import com.gic.commons.util.DateUtil;

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

/**
 * @Author guojx
 * @Date 2024/5/30 15:34
 */
public class DateExpandUtils {

    public static List<String> getDateList(String startDate, String endDate, Integer dateType) {
        //数据类型 1天 2近7天 3近30天 4周 5月
        List<String> list = new ArrayList<>();
        if (dateType == null) {
            dateType = 1;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            switch (dateType) {
                case 1:
                case 2:
                case 3:
                    calendar.setTime(dateType == 1 ? df.parse(startDate) : df.parse(endDate));
                    calendar.add(Calendar.DAY_OF_YEAR, -29);
                    list = DateUtil.getBetweenDates(df.format(calendar.getTime()), dateType == 1 ? startDate : endDate);
                    break;
                case 4:
                    calendar.setTime(DateUtil.strToDate(startDate, "yyyy-MM-dd"));

                    calendar.add(Calendar.WEEK_OF_YEAR, -11);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("startDate", df.format(calendar.getTime()));
                    jsonObject.put("endDate", startDate);
                    jsonObject.put("pageNum", 1);
                    jsonObject.put("pageSize", 100);
                    Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), "data_pub_date_week");
                    List<JSONObject> data = DataApiUtils.getPageList(res);
                    list = data.stream().map(e ->e.getString("weekYear")).collect(Collectors.toList());
                    break;
                case 5:
                    //月
                    SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
                    calendar.setTime(df.parse(startDate));
                    calendar.add(Calendar.MONTH, -11);
                    for (int i = 1; i <= 12; i++) {
                        list.add(monthFormat.format(calendar.getTime()));
                        calendar.add(Calendar.MONTH, 1);
                    }
                    break;
                default:
            }
        } catch (Exception e) {

        }
        return list;
    }

    public static String getVisitTimeStr(Long avgVisitTime) {
        if (avgVisitTime != null) {
            int hour = (int) (avgVisitTime / 3600);

            String str = getTimeStr(hour);

            if (hour > 0) {
                avgVisitTime = avgVisitTime - (3600 * hour);
            }

            int minute = (int) (avgVisitTime / 60);
            str = str + ":" + getTimeStr(minute);
            if (minute > 0) {
                avgVisitTime = avgVisitTime - (60 * minute);
            }
            str = str +":"+ getTimeStr(avgVisitTime.shortValue());
            return str;
        }
        return null;
    }

    private static String getTimeStr(int time) {
        String str = "";
        if (time == 0) {
            str = "00";
        } else if (time < 10) {
            str = "0" + time + "";
        } else {
            str = "" + time;
        }
        return str;
    }
}
