package org.jeecg.modules.okki.site.service;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URL;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @author Chen
 */
@Slf4j
@Service
public class IOkCommonService {

    /**
     * 判断当前时间是否为早上9点到下午18点之间
     *
     * @return
     */
    public boolean isWorkingHours(Date date) {
        int currHour = DateUtils.getCurrentHour(date);
//        log.info("currHour：{}", currHour);
        return currHour >= 9 && currHour < 18;
    }

    /**
     * 判断当前时间是否为晚上18点到21点30分之间
     *
     * @return
     */
    public boolean isAfterHours(Date date) {
        int currHour = DateUtils.getCurrentHour(date);
        int currMinute = DateUtils.getCurrentMinute(date);
//        log.info("currHour：{} currMinute：{}", currHour, currMinute);
        return currHour == 18 && currMinute >= 0 ||
                currHour > 18 && currHour < 21 ||
                (currHour == 21 && currMinute < 30);
    }

    /**
     * @param sourceMap
     * @return
     */
    public static Map<String, Object> convertMap(Map<String, List<String>> sourceMap) {
        Map<String, Object> targetMap = new HashMap<>();
        for (Map.Entry<String, List<String>> entry : sourceMap.entrySet()) {
            String key = entry.getKey();
            List<String> stringList = entry.getValue();
            List<Object> objectList = new ArrayList<>(stringList);
            targetMap.put(key, objectList);
        }
        return targetMap;
    }

    /**
     * @param sourceMap
     * @return
     */
    public Map<String, List<Map<String, Object>>> convertMapList(Map<String, List<String>> sourceMap) {
        Map<String, List<Map<String, Object>>> resultData = new HashMap<>();
        for (Map.Entry<String, List<String>> entry : sourceMap.entrySet()) {
            String role = entry.getKey();
            List<String> ids = entry.getValue();
            List<Map<String, Object>> newArray = new ArrayList<>();

            for (String id : ids) {
                Map<String, Object> idObject = new HashMap<>();
                idObject.put("id", id);
                newArray.add(idObject);
            }
            resultData.put(role, newArray);
        }
        return resultData;
    }

    /**
     * 提取baseId
     *
     * @param url
     * @return
     */
    public String extractBaseId(String url) {
        String regex = "base/([^?&]*)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    /**
     * 提取表格ID
     *
     * @param url
     * @return
     */
    public String extractTableId(String url) {
        String regex = "table=([^&]*)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }

    /**
     * 提取指定 CSS 选择器的标题
     *
     * @param document
     * @param selector
     */
    public String extractTitle(Document document, String selector) {
        Element element = document.selectFirst(selector);
        String title = null;
        if (element != null) {
            title = element.text();
        }
        return title;
    }

    /**
     * 从页面中获取标题并处理
     *
     * @param document 页面 URL
     */
    public String getPageLinkTitle(Document document) {
        // 获取标题
        String title = extractTitle(document, "h1.entry-title");
        if (title == null) {
            title = extractTitle(document, "h2.entry-title");
        }
        if (title == null) {
            title = extractTitle(document, "h5.post-title");
        }
        return title;
    }

    /**
     * 计算当前日期是星期几，如果是周末，则返回4，否则返回3
     *
     * @return
     */
    public int getCalculateDays() {
        LocalDate today = LocalDate.now();
        DayOfWeek dayOfWeek = today.getDayOfWeek();
        log.info("dayOfWeek:{}", dayOfWeek);
//        if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) {
//            return 4;
//        } else {
//            return 3;
//        }
        return 3;
    }

}
