package com.yem.tws.common1;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class StringUtils {
    public static boolean isEmpty(final String string) {
        return isNull(string) || isBlank(string);
    }

    public static boolean isNotEmpty(final String string) {
        return !isEmpty(string);
    }

    public static String getEmpty() {
        return "";
    }

    public static boolean isNull(final String string) {
        return string == null;
    }

    public static boolean isNotNull(final String string) {
        return !isNull(string);
    }

    public static boolean isBlank(final String string) {
        return isNotNull(string) && string.trim().length() == 0;
    }

    public static String trim(final String string) {
        return isNull(string) ? getEmpty() : string.trim();
    }

    public static String formatMessage(final String msg, final Object[] params) {
        return MessageFormat.format(msg, params);
    }

    public static String setToString(final Set valueSet) {
        final StringBuffer returnValue = new StringBuffer("(");
        if (valueSet != null && valueSet.size() != 0) {
            final Iterator it = valueSet.iterator();
            boolean flag = false;
            String tempValue = null;
            Object obj = null;
            while (it.hasNext()) {
                obj = it.next();
                if (obj instanceof String) {
                    tempValue = (String)obj;
                }
                else {
                    tempValue = obj.toString();
                }
                if (tempValue != null) {
                    if (tempValue.trim().length() == 0) {
                        continue;
                    }
                    if (flag) {
                        returnValue.append(",");
                    }
                    returnValue.append("'");
                    returnValue.append(tempValue);
                    returnValue.append("'");
                    flag = true;
                }
            }
        }
        else {
            returnValue.append("'null'");
        }
        returnValue.append(")");
        return returnValue.toString();
    }

    public static String setToString(final DynamicObjectCollection value, final String field) {
        final StringBuffer returnValue = new StringBuffer("(");
        if (value != null && value.size() != 0) {
            boolean flag = false;
            for (final DynamicObject obj : value) {
                if (flag) {
                    returnValue.append(",");
                }
                returnValue.append("'");
                returnValue.append(obj.getString(field));
                returnValue.append("'");
                flag = true;
            }
        }
        else {
            returnValue.append("'null'");
        }
        returnValue.append(")");
        return returnValue.toString();
    }

    public static String splitString(String number, final int step, final String split) {
        String value = null;
        if (!isEmpty(number)) {
            while (number.length() > 0) {
                int cutStep;
                if ((cutStep = step) > number.length()) {
                    cutStep = number.length();
                }
                if (isEmpty(value)) {
                    value = number.substring(0, cutStep);
                }
                else {
                    value = value + split + number.substring(0, cutStep);
                }
                number = number.substring(cutStep);
            }
        }
        return value;
    }

    public static String listToString(final List<String> fliedList) {
        final StringBuffer sb = new StringBuffer();
        if (fliedList.size() == 0) {
            throw new IllegalArgumentException("intList.size cannot be 0!");
        }
        if (fliedList.size() == 1) {
            sb.append(fliedList.get(0));
        }
        else {
            final Iterator<String> it = fliedList.iterator();
            for (int i = 0, n = fliedList.size(); i < n - 1; ++i) {
                sb.append(it.next()).append(", ");
            }
            sb.append(it.next());
        }
        return sb.toString();
    }

    public static Object[] getPks(final DynamicObject[] objArray) {
        if (objArray == null || objArray.length == 0) {
            return new Object[0];
        }
        final Object[] pks = new Object[objArray.length];
        for (int i = 0; i < objArray.length; ++i) {
            pks[i] = objArray[i].getPkValue();
        }
        return pks;
    }

    public static Object[] getPks(final DynamicObjectCollection objArray) {
        if (objArray == null || objArray.size() == 0) {
            return new Object[0];
        }
        final Object[] pks = new Object[objArray.size()];
        for (int i = 0; i < objArray.size(); ++i) {
            pks[i] = ((DynamicObject)objArray.get(i)).getPkValue();
        }
        return pks;
    }

    public static <T> List<T> makeList(final T... e) {
        final List<T> statusList = new ArrayList<T>();
        for (int i = 0; i < e.length; ++i) {
            statusList.add(e[i]);
        }
        return statusList;
    }

    public static String setToSelectorString(final Set<String> valueSet) {
        final StringBuffer returnValue = new StringBuffer("");
        if (valueSet != null && valueSet.size() != 0) {
            final Iterator<String> it = valueSet.iterator();
            boolean flag = false;
            String tempValue = null;
            Object obj = null;
            while (it.hasNext()) {
                obj = it.next();
                if (obj instanceof String) {
                    tempValue = (String)obj;
                }
                else {
                    tempValue = obj.toString();
                }
                if (tempValue != null) {
                    if (tempValue.trim().length() == 0) {
                        continue;
                    }
                    if (flag) {
                        returnValue.append(",");
                    }
                    returnValue.append(tempValue);
                    flag = true;
                }
            }
        }
        return returnValue.toString();
    }

    /**
     * @功能 String左对齐
     */
    public static String padLeft(String src, int len, char ch) {
        int diff = len - src.length();
        if (diff <= 0) {
            return src;
        }

        char[] charr = new char[len];
        System.arraycopy(src.toCharArray(), 0, charr, 0, src.length());
        for (int i = src.length(); i < len; i++) {
            charr[i] = ch;
        }
        return new String(charr);
    }
    /**
     * @功能 String右对齐
     */
    public static String padRight(String src, int len, char ch) {
        int diff = len - src.length();
        if (diff <= 0) {
            return src;
        }

        char[] charr = new char[len];
        System.arraycopy(src.toCharArray(), 0, charr, diff, src.length());
        for (int i = 0; i < diff; i++) {
            charr[i] = ch;
        }
        return new String(charr);
    }
}
