package cn.superdesk.uniorder.common.numerical;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Description: 超级前台 业务处理 StringUtils
 * @Author: linjinyu
 * @CreateDate: 5/28/20 5:58 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 5/28/20 5:58 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@NoArgsConstructor
@Slf4j
public class AnStringUtils {

    public static int GOODS_NAME_LENGHT  = 70;
    public static int UNIT_LENGHT        = 10;
    public static int SELF_OPTION_LENGHT = 30;

//    public static void main(String[] args) {
//        String s = "伊利\t金典纯牛奶1*12*250ml新老包装随机发货";
//
//        log.info("{}", AnStringUtils.szhtxx(s, 80));
//    }

    public static String szhtxx(String input, int limitLenght) {


        //throws UnsupportedEncodingException

//        return StrUtil.subPreGbk(StrUtil.removeAll(input, StrUtil.SPACE), lenght, ""); // 错

        String output = null;

        int currLenght   = 0;
        int reviseLenght = 0;

        if (StrUtil.isBlank(input)) {
            return "";
        }

        try {

//            input = StrUtil.removeAll(input, StrUtil.SPACE);
            input = StrUtil.removeAny(input,
                    /* 空格 */ StrUtil.SPACE,

                    /**************************************** add at 20220519 start ****************************************/

                    /* 水平制表符 */ StrUtil.TAB,
                    /* 回车 classic Mac */ StrUtil.CR,
                    /* 回车 classic Unix */ StrUtil.LF,
                    /* 回车 classic Windows */ StrUtil.CRLF

                    /**************************************** add at 20220519 start ****************************************/

            );

            byte[] buf = input.getBytes("GBK");

            log.info("{}", buf);

            currLenght = buf.length;

            if (currLenght > limitLenght) {
                reviseLenght = limitLenght;
            } else {
                reviseLenght = currLenght;
            }

            int count = 0;
            for (int x = reviseLenght - 1; x >= 0; x--) {

                // 使用GBK编码表进行字符串的截取，一个英文字符对应码表一个正数，一个汉字对应两个负数
                if (buf[x] < 0) {
                    count++;
                } else {
                    break;
                }
            }
            if (count % 2 == 0) {
                output = new String(input.getBytes("GBK"), 0, reviseLenght, "GBK");
            } else {
                output = new String(input.getBytes("GBK"), 0, reviseLenght - 1, "GBK");
            }

            return output;

        } catch (UnsupportedEncodingException unsupportedEncodingException) {
            return "不支持的编码方式";
        }


    }


    public static String stringArraySugarUpString(List<String> strings, String splitChat) {

        String resultStr = strings.stream()
                .filter(s -> StringUtils.isNotBlank(s))
                .distinct()
                .map(s -> s.trim() + splitChat).collect(Collectors.joining());

        if (StrUtil.isNotBlank(resultStr)) {
            return StrUtil.sub(resultStr, 0, resultStr.length() - 1);
        } else {
            return "";
        }


    }

    /**
     * 获取UUID,去掉了-
     *
     * @return
     * @author xuzhixiang
     * @date 2017年9月24日16:16:11
     */
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        log.debug("获取32位的UUID的调试日志-->> {}", uuid);
        return uuid;
    }


    /**
     * usage : List<Long> searchVirtIds = AnStringUtils.strArrayConvertToLongList(event.getVirtIds(), ";");
     *
     * @param ids
     * @param splitChat
     * @return
     */
    public static List<Long> stringArrayConvertToLongList(String ids, String splitChat) {


        if (StringUtils.isNotBlank(ids)) {
            String[] idArray = ids.split(splitChat);
            return Arrays.stream(idArray)
                    .filter(s -> StringUtils.isNotBlank(s))
                    .distinct()
                    .map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

    public static List<String> stringArrayConvertToStringList(String ids, String splitChat) {


        if (StringUtils.isNotBlank(ids)) {
            String[] idArray = ids.split(splitChat);
            return Arrays.stream(idArray)
                    .map(s -> s.trim()).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * @param input
     * @return
     */
    public static String stringListConvertToString(List<String> input) {

        String output = "";

        if (CollUtil.isNotEmpty(input)) {
            output = input.stream().collect(Collectors.joining(","));
        }

        return output;
    }

    public static String integerListConvertToString(List<Integer> input) {

        String output = "";

        if (CollUtil.isNotEmpty(input)) {
            output = input.stream().map(s -> getString(s)).collect(Collectors.joining(","));
        }

        return output;
    }

    public static String longListConvertToString(List<Long> input) {

        String output = "";

        if (CollUtil.isNotEmpty(input)) {
            output = input.stream().map(s -> getString(s)).collect(Collectors.joining(","));
        }

        return output;
    }

    public static String getString(Object object) {

        if (null == object) {
            return "";
        }
        return object.toString().trim();

    }


}




