package rd.util;

import org.apache.commons.codec.binary.Base64;

import java.nio.charset.StandardCharsets;
import java.util.*;

public class StringUtil {

    public static String NoneNullString(String str) {
        return str == null ? "" : str;
    }

    public static String NoneNullString(String str, String defaultStr) {
        return str == null ? defaultStr : str;
    }

    public static String getTermString(Date date, boolean autumn, boolean firstHalfTerm) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        return getTermString(calendar, autumn, firstHalfTerm);
    }

    public static String getTermString(Calendar calendar, boolean autumn, boolean firstHalfTerm) {
        int year = calendar.get(Calendar.YEAR);
        if (firstHalfTerm) {
            year -= 1;
        }

        int nextYear = year + 1;

        // 秋季期，就是第一个学期
        return String.format("%d-%d-%s", year, nextYear, autumn ? "1" : "2");
    }

    public static String getTermString(int year, boolean autumn, boolean firstHalf) {
        if (firstHalf) {
            year -= 1;
        }

        int nextYear = year + 1;

        // 秋季期，就是第一个学期
        return String.format("%d-%d-%s", year, nextYear, autumn ? "1" : "2");
    }

    public static String getCurrentTermString() {
        Calendar now = Calendar.getInstance();
        int month = now.get(Calendar.MONTH) + 1;

        return getTermString(now, month >= 8 || month < 2, month < 2);
    }

    static public List<String> split(String source, char gap) {
        List<String> result = new ArrayList<>(4);
        char[] sourceChars = source.toCharArray();

        int startIndex = 0;
        for (int index = -1; ++index != sourceChars.length; ) {
            if (sourceChars[index] != gap) {
                continue;
            }
            result.add(source.substring(startIndex, index));
            startIndex = index + 1;
        }

        result.add(source.substring(startIndex, sourceChars.length));

        return result;
    }

    /**
     * 清除字符串中的html（xml）标签，其中`&lt;br&gt;`被替换为换行`\n`
     *
     * @param raw 原始字符串
     * @return 处理后的字符串
     */
    public static String cleanHtml(String raw) {
        return cleanHtml(raw, true);
    }

    /**
     * 清除字符串中的html（xml）标签，并指定`&lt;br&gt;`是否要替换为换行符`\n`
     *
     * @param raw           原始字符串
     * @param withBreakLine 是否将换行标签`&lt;br&gt;`替换成换行符`\n`
     * @return 处理后的字符串
     */
    public static String cleanHtml(String raw, boolean withBreakLine) {
        if (withBreakLine) {
            raw = raw.replaceAll("<br>", "\n");
        }

        return raw.replaceAll("<.*?>", "");
    }

    /**
     * 解析转换字符串，如果出现任何问题，则返回给定的默认值
     *
     * @param string       待转换字符串
     * @param defaultValue 默认值
     * @return 转换后的int值，若转换中出现问题，则返回默认值
     */
    public static int parseInt(String string, int defaultValue) {
        try {
            return Integer.parseInt(string);
        } catch (Exception e) {
            return defaultValue;
        }
    }
}
