package com.android.launcher3.settings.wifi;

import android.content.Context;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

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

import static com.android.launcher3.Launcher.TAG;

public class WifiManagerUtils {

    private static WifiManagerUtils wifiManagerUtils;

    private static final Object object = new Object();

    private Context mContext;

    private WifiManager mWifiManager;

    private WifiManagerUtils(Context context) {
        mContext = context;
        mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    }

    public WifiManager getWifiManager() {
        if (mWifiManager == null) {
            mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        }
        return mWifiManager;
    }

    public static WifiManagerUtils getInstance(Context context) {
        if (wifiManagerUtils == null) {
            synchronized (object) {
                if (wifiManagerUtils == null) {
                    wifiManagerUtils = new WifiManagerUtils(context);
                }
            }
        }
        return wifiManagerUtils;
    }


    /**
     * 移除保存的网络
     */
    public boolean removeNetwork(int networkId) {
        WifiManager wifiManager = getWifiManager();
        //此方法系统签名才会全局生效
        return wifiManager.removeNetwork(networkId);
    }

    public boolean isEnabled() {
        return getWifiManager().isWifiEnabled();
    }

    /**
     * 获取当前连接WIFI的SSID
     */
    public void isIllegalWifi() {
        WifiManager wifiManager = getWifiManager();
        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if (wifiInfo != null) {
                String bssid = wifiInfo.getBSSID();
                String ssid=wifiInfo.getSSID();
                if (wifiInfo != null && !TextUtils.isEmpty(bssid)) {
                    bssid = bssid.toUpperCase();
                    if (TextUtils.isEmpty(ssid)){
                        ssid="";
                    }
                    ssid=ssid.replace("\"","");
                    int networkId = wifiInfo.getNetworkId();
                    Log.e(TAG, "NetworkConnectChangedReceiver-onReceive: networkId=" + networkId + "   =bssid=" + bssid);
                    //检测bssid是否是否是禁用的。(数据库检测)
                    List<WifiInfoBean> wifiMacs = getWifiMacs(mContext);
                    if (wifiMacs.size()>0) {
                        boolean isRemove = true;
                        for (int i = 0; i < wifiMacs.size(); i++) {
                            if (TextUtils.isEmpty(wifiMacs.get(i).getMac())) {
                                if (wifiMacs.get(i).getSsid().equals(ssid)) {
                                    isRemove = false;
                                    break;
                                }
                            } else {
                                if (wifiMacs.get(i).getSsid().equals(ssid) && wifiMacs.get(i).getMac().equals(bssid)) {
                                    isRemove = false;
                                    break;
                                }
                            }
                        }
                        if (isRemove) {
                            boolean removeNetwork = WifiManagerUtils.getInstance(mContext).removeNetwork(networkId);
                            Log.i(TAG, "removeNetwork: " + removeNetwork);
                        }
                    }
                }
            }
        }
    }

    public boolean isConnectedWifi(){
        WifiManager wifiManager = getWifiManager();
        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if (wifiInfo != null) {
                String bssid = wifiInfo.getBSSID();
                if (wifiInfo != null && !TextUtils.isEmpty(bssid)) {
                    bssid = bssid.toUpperCase();
                    int networkId = wifiInfo.getNetworkId();
                    Log.e(TAG, "NetworkConnectChangedReceiver-onReceive: networkId=" + networkId + "   =bssid=" + bssid);
                    //检测bssid是否是否是禁用的。(数据库检测)
                    if (bssid.equals("00:00:00:00:00:00")){
                        return false;
                    }else {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private List<WifiInfoBean> getWifiMacs(Context context){
        List<WifiInfoBean> mWifiMacs=new ArrayList<>();
        List<WifiInfoBean> wifiInfoBeans = Gson2ListHelper.stringWifiWhite2List(context);
        if (wifiInfoBeans!=null&&wifiInfoBeans.size()>0){
            for (int i = 0; i < wifiInfoBeans.size(); i++) {
                mWifiMacs.add(wifiInfoBeans.get(i));
            }
        }
        return mWifiMacs;
    }
}
