package com.quyunshuo.module.home.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.Proxy;
import android.os.Build.VERSION;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.zhanshow.mylibrary.network.LibraryConfig;
import com.zhanshow.mylibrary.network.NetworkStateReceiver;

public class NetWorkUtils {
    public static final String NETWORK_TYPE_WIFI = "wifi";
    public static final String NETWORK_TYPE_3G = "3g";
    public static final String NETWORK_TYPE_4G = "4g";
    public static final String NETWORK_TYPE_2G = "2g";
    public static final String NETWORK_TYPE_WAP = "wap";
    public static final String NETWORK_TYPE_UNKNOWN = "unknown";
    public static final String NETWORK_TYPE_DISCONNECT = "disconnect";
    private static NetworkStateReceiver mNetworkStateReceiver;
    private static ConnectivityManager connectivityManager;
    private static ConnectivityManager.NetworkCallback networkCallback;
    public NetWorkUtils() {
    }

    private static int getNetworkType(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService("connectivity");
        NetworkInfo networkInfo = connectivityManager == null ? null : connectivityManager.getActiveNetworkInfo();
        return networkInfo == null ? -1 : networkInfo.getType();
    }

    public static String getNetworkTypeName(Context context) {
        ConnectivityManager manager = (ConnectivityManager)context.getSystemService("connectivity");
        String type = "disconnect";
        NetworkInfo networkInfo;
        if (manager != null && (networkInfo = manager.getActiveNetworkInfo()) != null) {
            if (networkInfo.isConnected()) {
                String typeName = networkInfo.getTypeName();
                if ("WIFI".equalsIgnoreCase(typeName)) {
                    type = "wifi";
                } else if ("MOBILE".equalsIgnoreCase(typeName)) {
                    String proxyHost = Proxy.getDefaultHost();
                    type = TextUtils.isEmpty(proxyHost) ? (is4GNetwork(context) ? "4g" : (isFastMobileNetwork(context) ? "3g" : "2g")) : "wap";
                } else {
                    type = "unknown";
                }
            }

            return type;
        } else {
            return type;
        }
    }

    private static boolean isFastMobileNetwork(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService("phone");
        if (telephonyManager == null) {
            return false;
        } else {
            switch (telephonyManager.getNetworkType()) {
                case 0:
                    return false;
                case 1:
                    return false;
                case 2:
                    return false;
                case 3:
                    return true;
                case 4:
                    return false;
                case 5:
                    return true;
                case 6:
                    return true;
                case 7:
                    return false;
                case 8:
                    return true;
                case 9:
                    return true;
                case 10:
                    return true;
                case 11:
                    return false;
                case 12:
                    return true;
                case 13:
                    return true;
                case 14:
                    return true;
                case 15:
                    return true;
                default:
                    return false;
            }
        }
    }

    private static boolean is4GNetwork(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService("phone");
        if (telephonyManager == null) {
            return false;
        } else {
            switch (telephonyManager.getNetworkType()) {
                case 13:
                    return true;
                default:
                    return false;
            }
        }
    }

    public static void registerLister(final Activity activity, NetworkStateReceiver.NetworkStateReceiverListener listener) {
        LibraryConfig.getInstance().initApplication(activity);
        mNetworkStateReceiver = new NetworkStateReceiver();
        mNetworkStateReceiver.addListener(listener);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        intentFilter.addAction("com.example.CONNECTIVITY_ACTION_LOLLIPOP");
        activity.registerReceiver(mNetworkStateReceiver, intentFilter);
        if (VERSION.SDK_INT >= 21) {
            connectivityManager = (ConnectivityManager)activity.getSystemService("connectivity");
            NetworkRequest.Builder builder = new NetworkRequest.Builder();
            networkCallback = new ConnectivityManager.NetworkCallback() {
                public void onAvailable(Network network) {
                    Intent intent = new Intent("com.example.CONNECTIVITY_ACTION_LOLLIPOP");
                    intent.putExtra("noConnectivity", false);
                    activity.sendBroadcast(intent);
                }

                public void onLost(Network network) {
                    Intent intent = new Intent("com.example.CONNECTIVITY_ACTION_LOLLIPOP");
                    intent.putExtra("noConnectivity", true);
                    activity.sendBroadcast(intent);
                }
            };
            connectivityManager.registerNetworkCallback(builder.build(), networkCallback);
        }
    }

    public static void unRegisterNetWork(Activity activity) {
        activity.unregisterReceiver(mNetworkStateReceiver);
        mNetworkStateReceiver.removeListeners();
        connectivityManager.unregisterNetworkCallback(networkCallback);
        networkCallback = null;
        mNetworkStateReceiver= null;

    }
}