package com.hc.model.YNYD;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class YNYDContentProvider {

    private static final String URI_BASE_HW = "content://stbconfig/authentication";
    private static final String URI_BASE = "content://stbauthinfo/authentication";
    private static final String URI_USER_NAME = "username";
    private static final String URI_USER_TOKEN = "user_token";
    private static final String URI_PLATFORM = "platform";
    private static final String URI_EPG = "epg_server";

    private Context context;

    public YNYDContentProvider(Context context) {
        this.context = context;
    }

    /**
     * 获取业务账号
     */
    public String getUserName() {
        String userName = getValue(URI_BASE, URI_USER_NAME);
        Logger.d("业务账号：%s", userName);
        if(TextUtils.isEmpty(userName)) {
            userName = getValue(URI_BASE_HW, URI_USER_NAME);
            Logger.d("业务账号：%s", userName);
        }
        return userName;
    }

    public String getPlatform() {
        String platform = getValue(URI_BASE, URI_PLATFORM);
        Logger.d("平台：%s", platform);
        if(TextUtils.isEmpty(platform)) {
            platform = getValue(URI_BASE_HW, URI_PLATFORM);
            Logger.d("平台：%s", platform);
        }
        return platform;
    }

    public String getEpg() {
        String epg = getValue(URI_BASE, URI_EPG);
        Logger.d("EPG Server：%s", epg);
        if(TextUtils.isEmpty(epg)) {
            epg = getValue(URI_BASE_HW, URI_EPG);
            Logger.d("EPG Server：%s", epg);
        }
        return epg;
    }

    /**
     * 获取token
     */
    public String getToken() {
        String token = getValue(URI_BASE, URI_USER_TOKEN);
        Logger.d("Token：%s", token);
        if(TextUtils.isEmpty(token)) {
            token = getValue(URI_BASE_HW, URI_EPG);
            Logger.d("Token：%s", token);
        }
        return token;
    }

    private String getValue(String base, String contentName) {
        String value = "";
        Uri uri = Uri.parse(base);
        Cursor cursor = null;

        try {
            cursor =  context.getContentResolver().query(uri, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    if (name.equals(contentName)) {
                        value = cursor.getString(cursor.getColumnIndex("value"));
                        break;
                    }
                }
            }
        } catch (Exception e) {
            Logger.e(e.getCause(), e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return value;
    }

    private String getIP(String url, boolean needProtocol) {
        //使用正则表达式过滤，
        String re = "((http|ftp|https)://)(([a-zA-Z0-9._-]+)|([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))(([a-zA-Z]{2,6})|(:[0-9]{1,4})?)";
        String str = "";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(re);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(url);
        //若url==http://127.0.0.1:9040或www.baidu.com的，正则表达式表示匹配
        if (matcher.matches()) {
            str = url;
        } else {
            String[] split2 = url.split(re);
            if (split2.length > 1) {
                str = url.substring(0, url.length() - split2[1].length());
                if (!needProtocol) {
                    String[] split3 = str.split("//");
                    if (split3.length > 1) {
                        str = split3[1];
                    }
                }
            } else {
                str = split2[0];
            }
        }
        return str;
    }
}
