package cn.net.cpzslibs.label;

import java.util.regex.Pattern;

public class LabelHelper {
    /**
     * 区分一代二代三代的标签 2014-9-17下午5:08:05
     */
    public static String getLabelSplits(String scanContent) {
        if (scanContent == null)
            return "";
        scanContent = scanContent.trim();
        scanContent = scanContent.replaceAll("\\s", "");
        String new_lable_id = "";
        // String second =
        // "http://jymf.cpzs.net.cn:8080/download/lableid.html?lableId=";
        //http://y.cpzs.org/2100031000001000012457
        String third = "http://d.cpzs.org/?id=";
        String four = "http://y.cpzs.org/";
        if (scanContent.contains("lableId=")) {
            // 二代码//http://jymf.cpzs.net.cn:8080/download/lableid.html?lableId=1000123170957980
            String[] ids = scanContent.split("lableId=");
            new_lable_id = ids[1];
        } else if (scanContent.contains(third)) {
            // 三代标签http://d.cpzs.org/?id=1000123170957980
            new_lable_id = scanContent.split("d.cpzs.org\\/\\?id=")[1];
        } else if (scanContent.contains(four)) {
            // 三代标签http://d.cpzs.org/?id=1000123170957980
            new_lable_id = scanContent.split("y.cpzs.org/")[1];
        } else if (scanContent.length() > 22) {
            //长度大于22位，截取后22位判断
            String sub22Str = scanContent.substring(scanContent.length() - 22, scanContent.length());
            if (isNumber(sub22Str)) {
                new_lable_id = sub22Str;
            } else {
                String sub16Str = scanContent.substring(scanContent.length() - 16, scanContent.length());
                if (isNumber(sub16Str)){
                    new_lable_id = sub16Str;
                } else{
                    new_lable_id = scanContent;
                }
            }

        } else if (scanContent.length() > 16 && scanContent.length() < 22) {
            String sub16Str = scanContent.substring(scanContent.length() - 16, scanContent.length());
            if (isNumber(sub16Str)){
                new_lable_id = sub16Str;
            } else{
                new_lable_id = scanContent;
            }
        } else {
            new_lable_id = scanContent.trim();
        }
        return new_lable_id;
    }

    /**
     * 验证消费码 2014-5-12下午9:26:32
     *
     * @param inputConsCode 输入的四位消费码
     * @param consCode      服务器获取消费码
     * @return
     */
    public static boolean checkConsCode(String inputConsCode, String consCode) {

        if (inputConsCode.trim().equals(consCode.trim())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 简单的验证追溯码是否正确
     * <p>
     * 小于16位和大于19位为错误
     * </p>
     * <p>
     * 2014-10-27下午1:54:20
     * </p>
     *
     * @param label
     * @return
     */
    public static boolean checkLabelLength(String label) {
        if (label.trim().length() < 16 || label.trim().length() > 19) {
            return false;
        }
        return true;
    }

    /**
     * 每隔4位空一格 2014-5-22下午1:53:57
     *
     * @param label
     * @return
     */
    public static String formatLabel(String label) {
        if (label != null) {
            return label.replaceAll("\\d{4}(?!$)", "$0  ");
        }
        return label;
    }

    /**
     * 添加换行符 2014-5-27下午9:52:07
     *
     * @param str
     * @return
     */
    public static String replaceEnter(String str) {
        String ss = null;
        if (str == null) {
            return "";
        }
        if (!"".equals(str)) {
            if (str.contains("#%*")) {
                ss = str.replace("#%*", "\n");
            } else {
                ss = str;
            }
        } else {
            ss = str;
        }
        return ss;
    }

    /**
     * 返回该路径的末尾数 info_0_时间戳 用来判断公司追溯模式中区分图片位置
     *
     * @param path
     * @return
     */
    public static String retTail(String path) {
        String[] buffers = path.split("\\_");
        String temp = buffers[1];
        return temp;
    }

    /**
     * 判断是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        return Pattern.compile("^\\d*$").matcher(str).matches();
    }
}
