package com.casic.dcms.utils;

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

import com.casic.dcms.R;
import com.casic.dcms.model.BridgeConfigBean;
import com.casic.dcms.model.UserBean;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.pengxh.androidx.lite.utils.SaveKeyValues;

import java.util.ArrayList;
import java.util.List;

public class StringHelper {
    private static final String TAG = "StringHelper";

    public static UserBean.DataBean getUserBean() {
        String userDetailJson = (String) SaveKeyValues.getValue(LocaleConstant.USER_DETAIL_MODEL, "");
        if (!userDetailJson.isEmpty()) {
            return new Gson().fromJson(userDetailJson, new TypeToken<UserBean.DataBean>() {
            }.getType());
        }
        return new UserBean.DataBean();
    }

    public static void saveToiletLayerId(String toiletLayerId) {
        SaveKeyValues.putValue(LocaleConstant.TOILET_LAYER_ID, toiletLayerId);
    }

    static String getToiletLayerId() {
        return (String) SaveKeyValues.getValue(LocaleConstant.TOILET_LAYER_ID, "");
    }

    public static void saveBridgeList(String bridgesJson) {
        SaveKeyValues.putValue(LocaleConstant.BRIDGE_LIST, bridgesJson);
    }

    public static List<BridgeConfigBean.BridgesBean> getBridgeBeanList() {
        String bridgesJson = (String) SaveKeyValues.getValue(LocaleConstant.BRIDGE_LIST, "");
        List<BridgeConfigBean.BridgesBean> bridges = new ArrayList<>();
        if (bridgesJson.equals("")) {
            return bridges;
        }
        return new Gson().fromJson(bridgesJson, new TypeToken<List<BridgeConfigBean.BridgesBean>>() {
        }.getType());
    }

    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) {
        if (number.length() == 11) {
            return number.matches("^1[3-9]\\d{9}");
        } else {
            return false;
        }
    }

    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(LocaleConstant.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 {
            /**
             * 由于之前测试数据，服务器垃圾数据较多，需要根据不同情况拼接图片地址
             *
             * 正确返回值：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(LocaleConstant.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(LocaleConstant.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 StringToType(String s) {
        String punishType = "";
        switch (s) {
            case "劝导":
                punishType = "1";
                break;
            case "警告":
                punishType = "2";
                break;
            case "处罚":
                punishType = "3";
                break;
        }
        return punishType;
    }

    /**
     * 三包处罚转换
     */
    public static String TypeToString(String type) {
        String s = "";
        switch (type) {
            case "1":
                s = "劝导";
                break;
            case "2":
                s = "警告";
                break;
            case "3":
                s = "处罚";
                break;
        }
        return s;
    }

    public static int getResponseCode(String value) {
        if (value.isEmpty()) {
            return 404;
        }
        JsonElement element = JsonParser.parseString(value);
        JsonObject jsonObject = element.getAsJsonObject();
        return jsonObject.get("code").getAsInt();
    }

    public static String getResponseMessage(String value) {
        if (value.isEmpty()) {
            return "";
        }
        JsonElement element = JsonParser.parseString(value);
        JsonObject jsonObject = element.getAsJsonObject();
        return jsonObject.get("message").getAsString();
    }

    /**
     * 根据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.mipmap.gcbj;
            case "三包处置":
                return R.mipmap.sbcz;
            case "商铺变更":
                return R.mipmap.spbg;
            case "部门处置":
                return R.mipmap.bmcz;
            case "路桥养护":
                return R.mipmap.lqyh;
            case "管网管理":
                return R.mipmap.gwgl;
            case "路灯管理":
                return R.mipmap.ldgl;
            ////////////////////////////////////////////////////////
            case "网格":
                return R.mipmap.data_grid;
            case "部件":
                return R.mipmap.data_component;
            case "商铺":
                return R.mipmap.data_shop;
            case "作业车辆":
                return R.mipmap.data_machine;
            case "公务车辆":
                return R.mipmap.data_car;
            case "环境监测点":
                return R.mipmap.data_enviroment;
            case "垃圾中转站":
                return R.mipmap.data_dump;
            case "城区公厕":
                return R.mipmap.data_wc;
            case "分类垃圾桶":
                return R.mipmap.data_bucket;
            case "人员在岗率":
                return R.mipmap.data_worker;
            case "作业完成率":
                return R.mipmap.data_work;
            case "路灯亮灯率":
                return R.mipmap.data_light;
            case "视频在线率":
                return R.mipmap.data_video;
            case "管网报警率":
                return R.mipmap.data_pipeline;
            default:
                return R.mipmap.app_logo;
        }
    }
}
