package com.casic.dcms.utils;

import android.text.TextUtils;
import android.util.Log;

import com.casic.dcms.R;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringHelper {

    private static final String TAG = "StringHelper";

    public static void saveUserAccount(String account) {
        SaveKeyValues.putValue("userAccount", account);
    }

    public static String getUserAccount() {
        return (String) SaveKeyValues.getValue("userAccount", "");
    }

    public static void saveUserId(String userId) {
        SaveKeyValues.putValue("userId", userId);
    }

    public static String getUserId() {
        return (String) SaveKeyValues.getValue("userId", "");
    }

    public static void saveUserName(String name) {
        SaveKeyValues.putValue("userName", name);
    }

    public static String getUserName() {
        return (String) SaveKeyValues.getValue("userName", "");
    }

    public static void saveUserPhone(String phone) {
        SaveKeyValues.putValue("userPhone", phone);
    }

    public static String getUserPhone() {
        return (String) SaveKeyValues.getValue("userPhone", "");
    }

    public static void saveUserDept(String dept) {
        SaveKeyValues.putValue("userDept", dept);
    }

    public static String getUserDept() {
        return (String) SaveKeyValues.getValue("userDept", "");
    }

    public static void saveUserDeptId(String deptid) {
        SaveKeyValues.putValue("userDeptId", deptid);
    }

    public static String getUserDeptId() {
        return (String) SaveKeyValues.getValue("userDeptId", "");
    }

    public static boolean isLetterAndDigit(String str) {
        boolean isDigit = false;
        boolean isLetter = false;
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {
                isDigit = true;
            } else if (Character.isLetter(str.charAt(i))) {
                isLetter = true;
            }
        }
        return isDigit && isLetter;
    }

    public static boolean isPhoneNumber(String number) {
        String regExp = "^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$";
        if (number.length() != 11) {
            Log.d(TAG, "手机号应为11位数");
            return false;
        } else {
            Pattern p = Pattern.compile(regExp);
            Matcher m = p.matcher(number);
            boolean isMatch = m.matches();
            if (!isMatch) {
                Log.d(TAG, "请填入正确的手机号");
            }
            return isMatch;
        }
    }

    public static String appendCompleteURL(String url) {
        if (TextUtils.isEmpty(url)) {
            return "";
        }
        if (url.endsWith(".m4a") || url.endsWith(".mp4")) {
            String data = url.replace("\\", "/");
            String mediaPath = SaveKeyValues.getValue(Constant.IP_KEY, "http://111.198.10.15:11409") + "/static/" + data;
//            String mediaPath = SaveKeyValues.getValue(Constant.IP_KEY, "http://223.82.47.232:11401") + "/static/" + data;
            Log.d(TAG, "媒体路径: " + mediaPath);
            return mediaPath;
        } else {
            //TODO 由于之前测试数据，服务器垃圾数据较多，需要根据不同情况拼接图片地址
            /**
             * 正确返回值：2021-02\6a4a7784e9be40238f037a03c43a5d7a.jpg
             *
             * 情况一：["2021-04\\3c46b405c33b4100809b8c800fa914ca.jpeg"
             * 情况二："2021-04\\7c2c22005c9c47bdb7dc2c20676c9869.jpeg"]
             * 情况三：http://111.198.10.15:11409/static/2021-04/8ccd6d3220fb4d59bae865d6e8be0777.jpeg
             * */
            if (url.contains("[") || url.contains("]") || url.contains("\"") || url.contains("\\")) {
                //去掉[]和""，并替换\\
                url = url.replace("[", "")//左括号
                        .replace("]", "")//右括号
                        .replace("\"", "")//双引号
                        .replace("\\\\", "\\");//反双斜杠
            }
            /**
             * 情况四：\http://111.198.10.15:11409/static/2021-04/d78bd724431642e3adb1d78f1d99320b.jpeg\
             * */
            if (url.startsWith("\\") || url.endsWith("\\")) {
                url = url.replace("\\", "");
            }
            String imagePath;
            if (url.contains("http") || url.contains("https")) {
                imagePath = url;
            } else {
                String data = url.replace("\\", "/");
                imagePath = SaveKeyValues.getValue(Constant.IP_KEY, "http://111.198.10.15:11409") + "/static/" + data;
//                imagePath = SaveKeyValues.getValue(Constant.IP_KEY, "http://223.82.47.232:11401") + "/static/" + data;
            }
            Log.d(TAG, "图片路径: " + imagePath);
            return imagePath;
        }
    }

    public static String appendDownloadUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return url;
        }
        return SaveKeyValues.getValue(Constant.IP_KEY, "http://111.198.10.15:11409") + "/static/" + url;
//        return SaveKeyValues.getValue(Constant.IP_KEY, "http://223.82.47.232:11401") + "/static/" + url;
    }

    public static String reformatURL(List<String> urls) {
        if (urls.isEmpty()) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < urls.size(); i++) {
            if (i != urls.size() - 1) {
                builder.append(urls.get(i)).append(",");
            } else {
                builder.append(urls.get(i));
            }
        }
        String s = builder.toString();
        Log.d(TAG, "reformatURL: " + s);
        return s;
    }

    /**
     * 过滤空格，回车
     */
    public static String filterString(String s) {
        if (TextUtils.isEmpty(s)) {
            return s;
        }
        //先过滤回车换行
        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(s);
        s = m.replaceAll("");
        //再过滤空格
        return s.trim().replace(" ", "");
    }

    /**
     * 根据item名字获取icon
     */
    public static int obtainIcon(String item) {
        switch (item) {
            case "案卷上报":
                return R.mipmap.ajsb;
            case "快速上报":
                return R.mipmap.kssb;
            case "三包上报":
                return R.mipmap.sbsb;
            case "案卷核实":
                return R.mipmap.ajhs;
            case "案卷核查":
                return R.mipmap.ajhc;
            case "案卷处理":
                return R.mipmap.ajcl;
            case "紧急要件":
                return R.mipmap.jjyj;
            case "超时督办":
                return R.mipmap.csdb;
            case "统计分析":
                return R.mipmap.tjfx;
            case "在岗人员":
                return R.mipmap.zgry;
            case "案卷查询":
                return R.mipmap.ajcx;
            case "案卷分布":
                return R.mipmap.dtck;
            case "公厕保洁":
                return R.drawable.ic_toilet;
            default:
                return R.mipmap.app_logo;
        }
    }
}
