package com.xhkjedu.slive.utils;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @Description
 * @Author WN
 * Date 2022/3/31 10:53
 **/
@Slf4j
public class LiveUtil {

    /**
     * @Description 获取指定日期星期
     * @Param [datestr]
     * @Return java.lang.String
     * @Author wn
     * @Date 2022/3/31 11:06
     **/
    public static String getDateZhou(String datestr) {
        LocalDate localDate = LocalDate.parse(datestr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String k = String.valueOf(localDate.getDayOfWeek());
        Map<String, String> map = saveZhou();
        String zhoustr = map.get(k);
        return "星期" + zhoustr;
    }

    //存放周
    private static Map<String, String> saveZhou() {
        Map<String, String> map = new HashMap<>();
        map.put("MONDAY", "一");
        map.put("TUESDAY", "二");
        map.put("WEDNESDAY", "三");
        map.put("THURSDAY", "四");
        map.put("FRIDAY", "五");
        map.put("SATURDAY", "六");
        map.put("SUNDAY", "日");
        return map;
    }

    /**
     * @Description 获取索引对应的数字
     * @Param [indexnum]
     * @Return java.lang.String
     * @Author wn
     * @Date 2022/3/31 12:45
     **/
    public static String getChineStrByNum(int indexnum) {
        String[] strnum = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一",
                "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十"};

        return strnum[indexnum];
    }


    /**
     * @Description 获取指定日期所在往后一周，包含指定日期
     * @Param [selectdate, days]
     * @Return java.util.List<java.lang.String>
     * @Author wn
     * @Date 2022/4/2 11:43
     **/
    public static List<String> listDateAfterDates(String beginstr,String endstr, int days) {
        List<String> rtnlst = new ArrayList<>();
        DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate datas = LocalDate.parse(beginstr, fomatter);
        LocalDate enddate = LocalDate.parse(endstr,fomatter);
        //days是的往后一周
        int i=0;
        while (i<days){
            int td = i ;
            LocalDate rtndatas = datas.with(temporal -> temporal.plus(td, ChronoUnit.DAYS));
            if (rtndatas.isBefore(enddate) || rtndatas.isEqual(enddate)) {
                rtnlst.add(rtndatas.format(fomatter));
                i++;
            } else {
                break;
            }
        }
        return rtnlst;
    }

    /**
     * 获取指定日期往后7天日期
     *
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.String>>
     * @Param [beginstr, endstr, days]
     * @Author ywx
     * @Date 2022/6/21 16:29
     **/
    public static Map<String, String> listDateAfterDates2(String beginstr, String endstr) {
        Map<String, String> map = new HashMap<>();
        DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate datas = LocalDate.parse(beginstr, fomatter);
        LocalDate enddate = LocalDate.parse(endstr, fomatter);
        Integer days = 7;//往后一周
        int i = 0;
        while (i < days) {
            int td = i;
            LocalDate rtndatas = datas.with(temporal -> temporal.plus(td, ChronoUnit.DAYS));
            if (rtndatas.isBefore(enddate) || rtndatas.isEqual(enddate)) {
                String date = rtndatas.format(fomatter);
                map.put(getDateZhou(date), date);
                i++;
            } else {
                break;
            }
        }
        return map;
    }

    /**
     * @Description 获取指定日期段内指定的周几日期数集合
     * @Param [startDateStr, endDateStr, weekDate]
     * @Return java.util.List<String>
     * @Author wn
     * @Date 2022/4/2 10:54
     **/
    public static List<String> getMeetDayOfWeekDates(String startDateStr, String endDateStr, Integer weekDate) {
        DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startDateStr, fomatter);
        LocalDate endDate = LocalDate.parse(endDateStr, fomatter);

        //指定的周几
        DayOfWeek dayOfWeek = DayOfWeek.of(weekDate);
        long distance = ChronoUnit.DAYS.between(startDate, endDate);

        List<String> rtnlst = new ArrayList<>();
        Stream.iterate(startDate, d -> d.plusDays(1))
              .limit(distance + 1)
              .filter(localDate -> dayOfWeek.toString().equals(localDate.getDayOfWeek().toString()))
              .forEach(f -> rtnlst.add(f.toString()));
        return rtnlst;
    }

    /**
     * @Description 根据一组日期获取指定星期的日期
     * @Param [dates, seldate]
     * @Return java.util.List<java.lang.String>
     * @Author wn
     * @Date 2022/4/2 15:14
     **/
    public static List<String> getMeetDayOfDates(List<String> dates,String seldate){
        List<String> rtn = new ArrayList<>();
        DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        //先获取当前日期所对应的周
        String selWeek = String.valueOf(LocalDate.parse(seldate,fomatter).getDayOfWeek());

        for(int i=0;i<dates.size();i++){
            LocalDate objdate = LocalDate.parse(dates.get(i), fomatter);
            if(objdate.getDayOfWeek().toString().equals(selWeek)){
                rtn.add(dates.get(i));
            }
        }
        return rtn;
    }

    /**
     * @Description 指定时间段内符合开始时间的周几的所有日期集合
     * @Param [startDateStr, endDateStr]
     * @Return java.util.List<java.lang.String>
     * @Author wn
     * @Date 2022/4/6 16:16
     **/
    public static List<String> getDaysForMeetDates(String startDateStr, String endDateStr) {
        DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startDateStr, fomatter);
        LocalDate endDate = LocalDate.parse(endDateStr, fomatter);

        DayOfWeek dayOfWeek = startDate.getDayOfWeek();
        long distance = ChronoUnit.DAYS.between(startDate, endDate);

        List<String> rtnlst = new ArrayList<>();
        Stream.iterate(startDate, d -> d.plusDays(1))
              .limit(distance + 1)
              .filter(localDate -> dayOfWeek.toString().equals(localDate.getDayOfWeek().toString()))
              .forEach(f -> rtnlst.add(f.toString()));
        return rtnlst;
    }

    //发送请求
    public static ResultVo sendJsonPost(String url, String param) {
        String result = N_Utils.sendJsonPost(url, param);
        if (N_Utils.isEmpty(result)) return new ResultVo(1, "发送请求失败");
        ResultVo vo = JSON.parseObject(result, ResultVo.class);
        return vo;
    }

}
