package com.android.launcher3.settings.wifi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;


import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import static android.content.Context.WIFI_SERVICE;
import static android.net.wifi.WifiManager.WIFI_STATE_CHANGED_ACTION;

public class NetworkConnectChangedReceiver extends BroadcastReceiver {

    private static final String TAG = NetworkConnectChangedReceiver.class.getSimpleName();

    private Context mContext;

    private WifiManager mWifiManager;


    private List<WifiInfoBean> mWifiMacs = new ArrayList<>();

    public NetworkConnectChangedReceiver(Context context) {
        mContext = context;
        mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(WIFI_SERVICE);
    }


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


    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
//        writeLog(action+"\r\n");
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)||action.equals(WIFI_STATE_CHANGED_ACTION)) {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            String bssid = intent.getStringExtra(WifiManager.EXTRA_BSSID);
            String ssid;
            if (null != parcelableExtra) {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                NetworkInfo.State state = networkInfo.getState();
                if (state == NetworkInfo.State.CONNECTED) {
                    WifiManager wifiManager = getWifiManager();
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    if (TextUtils.isEmpty(bssid)) {
                        bssid = wifiInfo.getBSSID();
                    }

                    if (wifiInfo != null && !TextUtils.isEmpty(bssid)) {
                        bssid = bssid.toUpperCase();
                        ssid = wifiInfo.getSSID();
                        if (TextUtils.isEmpty(ssid)) {
                            ssid = "";
                        }
                        ssid = ssid.replace("\"", "");
                        int networkId = wifiInfo.getNetworkId();
                        //检测bssid是否是否是禁用的。(数据库检测)

                        if (getmWifiMacs(context).size() > 0) {
                            boolean isRemove = true;
                            for (int i = 0; i < mWifiMacs.size(); i++) {
                                if (TextUtils.isEmpty(mWifiMacs.get(i).getMac())) {
                                    Log.i(TAG, "wifiInfoBean ssid: " + mWifiMacs.get(i).getSsid());
                                    Log.i(TAG, "wifiInfo ssid: " + ssid);
                                    if (mWifiMacs.get(i).getSsid().equals(ssid)) {
                                        Log.i(TAG, "isRemove: " + isRemove);
                                        isRemove = false;
                                        break;
                                    }
                                } else {
                                    if (mWifiMacs.get(i).getSsid().equals(ssid) && mWifiMacs.get(i).getMac().equals(bssid)) {
                                        isRemove = false;
                                        break;
                                    }
                                }
                            }
                            if (isRemove) {
                                boolean removeNetwork = WifiManagerUtils.getInstance(context).removeNetwork(networkId);
                                Log.i(TAG, "removeNetwork: " + removeNetwork);
                            }
                        }
                    }
                }
            }
        }
    }

    private List<WifiInfoBean> getmWifiMacs(Context context) {
        synchronized (String.class) {
            mWifiMacs.clear();
            List<WifiInfoBean> wifiInfoBeans = Gson2ListHelper.stringWifiWhite2List(context);
            mWifiMacs.addAll(wifiInfoBeans);
            return mWifiMacs;
        }
    }


    private void writeLog(String content) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NetWork.text");
        try {
            if (file == null) {
                file.createNewFile();
            }
            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
            raf.seek(file.length());
            raf.write(content.getBytes());
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
