package com.storlead.framework.common.util;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @program: storelad
 * @description: String工具类
 * @author: chenkq
 * @create: 2022-09-29 11:24
 **/
public class StringUtil extends StringUtils {

    public static String trimSpace(String str, String space) {
        if (str.startsWith(space)) {
            str = str.substring(space.length());
        }
        if (str.endsWith(space)) {
            str = str.substring(0, str.length() - space.length());
        }
        return str;
    }

    public static String removePrefixAndSuffix(String str, String prefix, String suffix) {
        if (str.startsWith(prefix)) {
            str = str.substring(prefix.length());
        }
        if (str.endsWith(suffix)) {
            str = str.substring(0, str.length() - suffix.length());
        }
        return str;
    }

    /**
     * 判断两个字符串是否相等，除去html标签 两端空格和换行
     * @param s1 s1
     * @param s2 s2
     * @return: @return boolean
     * @author: xiajiawei
     * @date: 2022-09-29
     */
    public static  boolean ignoreHtmlSpaceEnterEquals(String s1,String s2){
        if(s1==null&&s2==null){
            return true;
        }
        if(s1 == null||s2==null){
            return false;
        }
        s1 = HtmlUtils.userContent(s1)
                .replace("/r", "")
                .replace("/n", "")
                .trim();
        s2 = HtmlUtils.userContent(s2)
                .replace("/r", "")
                .replace("/n", "")
                .trim();
        return Objects.equals(s1,s2);
    }

    public static List<Long> stringToLongList(String commaSeparatedString) {
        List<Long> longList = new ArrayList<>();
        if (commaSeparatedString != null && !commaSeparatedString.isEmpty()) {
            String[] strings = commaSeparatedString.split(",");
            for (String s : strings) {
                // 去除字符串两侧的空白字符，并尝试转换为Long
                Long number = Long.parseLong(s.trim());
                longList.add(number);
            }
        }
        return longList;
    }

    //List<Long>转逗号分割的字符串
    public static String longListToString(List<Long> longList) {
        if (CollectionUtils.isEmpty(longList)){
            return null;
        }
        String listString = String.join(",", longList.stream().map(Object::toString).toArray(String[]::new));

        return listString;
    }
    //List<String>转逗号分割的字符串
    public static String stringListToString(List<String> stringList) {
        if (CollectionUtils.isEmpty(stringList)){
            return null;
        }
        String listString = String.join(",", stringList);

        return listString;
    }
    //逗号分割的字符串转List<Long>
    public static List<Long> stringToStringList(String commaSeparatedString) {
        if (ObjectUtils.isEmpty(commaSeparatedString)){
            return new ArrayList<>();
        }
        List<Long> longList = Arrays.stream(commaSeparatedString.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return longList;
    }
    //逗号分割的字符串转List<String>
    public static List<String> stringToList(String commaSeparatedString) {
        if (ObjectUtils.isEmpty(commaSeparatedString)){
            return new ArrayList<>();
        }
        List<String> stringList = Arrays.stream(commaSeparatedString.split(","))
                .collect(Collectors.toList());
        return stringList;
    }

    public static boolean containsAllIds(List<Long> subScopeIdList, String subScopeId) {
        // Split the comma-separated string into a list of strings
        if (ObjectUtils.isEmpty(subScopeId)){
            return false;
        }
        List<String> subScopeIdStrList = Arrays.asList(subScopeId.split(","));

        // Convert the list of strings to a list of longs
        List<Long> subScopeIdLongList = subScopeIdStrList.stream()
                .map(Long::parseLong)
                .collect(Collectors.toList());

        // Check if subScopeIdList contains all elements of subScopeIdLongList
        return subScopeIdList.containsAll(subScopeIdLongList);
    }



    public static String extractUsernames(String recipients) {
        try {
            if (StrUtil.isBlank(recipients)) {
                return  "";
            }
            String regex = "(\\S+)@";

            // 创建一个匹配模式
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(recipients);
            List<String> usernames = new ArrayList<>();
            // 查找所有匹配的部分
            while (matcher.find()) {
                usernames.add(matcher.group(1)); // 获取 @ 前的部分
            }
            // 将结果列表用逗号连接
            return String.join(",", usernames);
        }catch (Exception e) {
            return "";
        }
    }


    public static String updateStringList(String ids, List<Long> idListAll, List<Long> idList) {
        // Step 1: Parse string A into a List<Long>
        if (ObjectUtils.isEmpty(ids)){
            return idList.stream()
                    .map(Object::toString)
                    .collect((Collectors.joining(",")));
        }
        List<Long> parsedA = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());

        // Step 2: Remove elements from B from parsedA
        Set<Long> setB = new HashSet<>(idListAll);
        List<Long> updatedA = parsedA.stream()
                .filter(element -> !setB.contains(element))
                .collect(Collectors.toList());

        // Step 3: Add elements from C to updatedA
        updatedA.addAll(idList);

        // Step 4: Convert updatedA back to a comma-separated string
        String result = updatedA.stream()
                .map(Object::toString)
                .collect((Collectors.joining(",")));
        return result;
    }

    public static void main(String[] args) {
//        System.out.println(stringListToString(null));
        String A = "1,3,5,6,7";
        List<Long> B = Arrays.asList(1L, 2L, 3L, 4L);
        List<Long> C = Arrays.asList(2L, 3L, 4L);
        System.out.println(updateStringList(A, B, C));
    }

    public static String concatRegexp(List<Long> ids){
        if(ids.size()==0){
            return "-";
        }else {
            return ids.stream().map(String::valueOf).collect(Collectors.joining("|"));
        }
    }
}
