package com.yuchen.saas.park.service.util;

import java.util.Random;

public class TextHelper {
    protected static final String TAG = "[Tmp]TextHelper";
    protected static Random random = new Random();
    static final String RANDOM_BYTE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    public static String combineStr(String first, String second) {
        return first + second;
    }

    public static String combineStr(String first, String second, String third) {
        return first + second + third;
    }

    public static String queryEventIdentifier(String method) {
        String identifier = null;
        if (StringUtils.isEmpty(method)) {
            return identifier;
        } else if (method.equalsIgnoreCase("thing.event.property.post")) {
            return "post";
        } else {
            String[] segmentList = method.split("\\.");
            if (segmentList != null && segmentList.length > 1) {
                if ("post".equalsIgnoreCase(segmentList[segmentList.length - 1])) {
                    identifier = segmentList[segmentList.length - 2];
                } else {
                    identifier = segmentList[segmentList.length - 1];
                }
            }

            return identifier;
        }
    }

    public static String getRandomString() {
        return getRandomString(16);
    }

    public static String getRandomString(int length) {
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; ++i) {
            sb.append(RANDOM_BYTE.charAt(random.nextInt(RANDOM_BYTE.length())));
        }

        return sb.toString();
    }

    public static String formatPostReplyTopic(String pk, String dn) {
        return "$" + "thing" + "/" + pk + "/" + dn + "/" + "event/property" + "/" + "post_res";
    }

    public static String formatEventReplyTopic(String pk, String dn, String identifier) {
        return "$" + "thing" + "/" + pk + "/" + dn + "/" + "event/" + identifier + "/" + "post_res";
    }
}
