package com.gh.gamecenter.core.utils;

import android.text.Html;
import android.text.TextUtils;

/**
 * Created by khy on 2017/5/2.
 */

public class StringUtils {

    public static String buildString(String... arrStr) {
        int strCount = 0;

        for (int i = 0; i < arrStr.length; ++i) {
            strCount += arrStr[i] == null ? 0 : arrStr[i].length();
        }

        StringBuilder result = new StringBuilder(strCount);

        for (int j = 0; j < arrStr.length; ++j) {
            result.append(arrStr[j]);
        }

        return result.toString();
    }

    /**
     * 将两个字符串拼接起来，以 "displayName(description)" 的形式返回
     *
     * @param displayName 若传入的 displayName 长度大于 30 截取 30 并补充 "..." PS: 仅使用 displayName 中的纯文本部分
     * @param description 不需额外处理的入参
     * @return "display(description)"
     */
    public static String combineTwoString(String displayName, String description) {
        if (TextUtils.isEmpty(displayName) || TextUtils.isEmpty(description)) return "";

        displayName = Html.fromHtml(displayName).toString();

        displayName = displayName.replace("\n", "");

        if (displayName.length() > 30) {

            displayName = displayName.substring(0, 30) + "...";
        }

        return displayName + "(" + description + ")";
    }

    public static String eliminateHtmlContent(String s) {
        if (TextUtils.isEmpty(s)) return "";

        s = Html.fromHtml(s).toString();
        s = s.replace("\n", "");
        return s;
    }

    /**
     * 截取字符串部分长度，超出的以 "..." 代替
     *
     * @param text      字符串内容
     * @param maxLength 最大长度
     * @return 修饰后的字符串
     */
    public static String shrinkStringWithDot(String text, int maxLength) {
        if (TextUtils.isEmpty(text)) return "";

        if (text.length() > maxLength) {
            text = text.substring(0, maxLength) + "...";
        }
        return text;
    }

    /**
     * 删除尾部\n字符
     *
     * @param str
     * @return
     */
    public static String deleteStringTailLineBreak(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }

        if (str.length() == 1) {
            char ch = str.charAt(0);
            if (ch == '\n') {
                return "";
            }
            return str;
        }

        int lastIdx = str.length() - 1;
        char last = str.charAt(lastIdx);

        if (last == '\n') {
            return str.substring(0, lastIdx);
        } else {
            return str;
        }
    }

    /**
     * 判断字符串末尾是否有\n
     *
     * @param str
     * @return
     */
    public static boolean isStringTailHasLineBreak(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }

        int lastIdx = str.length() - 1;
        char last = str.charAt(lastIdx);

        return last == '\n';
    }
}
