package com.wdkl.ncs.android.middleware.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class CommonUtils {
    private static final String SP_NAME = "SP_URL";
    private static final String KEY_SP_THIRD_URL = "KEY_SP_THIRD_URL";
    private static final String KEY_SP_THIRD_URL_PORT = "KEY_SP_THIRD_URL_PORT";
    private static final String KEY_SP_URL = "KEY_SP_URL";
    private static final String KEY_SP_URL_PORT = "KEY_SP_URL_PORT";
    private static final String KEY_SP_SIP_URL = "KEY_SP_SIP_URL";
    private static final String KEY_SP_SIP_PORT = "KEY_SP_SIP_PORT";

    //默认服务器ip端口
    private static final String DEFAULT_URL = "172.28.100.100";
    //private static final String DEFAULT_URL = "8.129.220.143";
    //private static final String DEFAULT_URL = "192.168.101.1";
    //private static final String DEFAULT_URL = "192.168.1.152";
    //private static final String DEFAULT_URL = "119.23.151.229";
    //private static final String DEFAULT_URL = "172.168.0.254"; //眉山
    private static final String DEFAULT_URL_PORT = "8006";
    private static final String DEFAULT_SIP_PORT = "8188";

    public static String subStringBefore(String origin, String delimiter) {
        int index = origin.indexOf(delimiter);
        if (index > 0) {
            return origin.substring(0, index);
        } else {
            return origin;
        }
    }


    public static String getUrl(Context context) {
        if (TextUtils.isEmpty(getSP(context).getString(KEY_SP_URL, DEFAULT_URL))) {
            return DEFAULT_URL;
        }
        return getSP(context).getString(KEY_SP_URL, DEFAULT_URL);
    }

    public static void setUrl(Context context, String url) {
        getEditor(context).putString(KEY_SP_URL, url).apply();
    }

    public static String getUrlPort(Context context) {
        if (TextUtils.isEmpty(getSP(context).getString(KEY_SP_URL_PORT, DEFAULT_URL_PORT))) {
            return DEFAULT_URL_PORT;
        }
        return getSP(context).getString(KEY_SP_URL_PORT, DEFAULT_URL_PORT);
    }

    public static void setUrlPort(Context context, String port) {
        getEditor(context).putString(KEY_SP_URL_PORT, port).apply();
    }

    /*public static String getSipUrl(Context context) {
        if (TextUtils.isEmpty(getSP(context).getString(KEY_SP_SIP_URL, DEFAULT_URL))) {
            return DEFAULT_URL;
        }
        return getSP(context).getString(KEY_SP_SIP_URL, DEFAULT_URL);
    }

    public static void setSipUrl(Context context, String url) {
        getEditor(context).putString(KEY_SP_SIP_URL, url).apply();
    }

    public static String getSipPort(Context context) {
        if (TextUtils.isEmpty(getSP(context).getString(KEY_SP_SIP_PORT, DEFAULT_SIP_PORT))) {
            return DEFAULT_SIP_PORT;
        }
        return getSP(context).getString(KEY_SP_SIP_PORT, DEFAULT_SIP_PORT);
    }

    public static void setSipPort(Context context, String port) {
        getEditor(context).putString(KEY_SP_SIP_PORT, port).apply();
    }*/


    private static SharedPreferences getSP(Context context) {
        return context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }

    private static SharedPreferences.Editor getEditor(Context context) {
        return getSP(context).edit();
    }
}
