package com.example.imlinkclient.helpr;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.example.imlinkclient.MainClientActivity;

import java.util.List;

/**
 * WIFI
 * 参考：https://stackoverflow.com/questions/8818290/how-do-i-connect-to-a-specific-wi-fi-network-in-android-programmatically
 * https://developer.aliyun.com/article/12840
 * https://developer.android.google.cn/reference/android/net/wifi/WifiConfiguration
 * https://github.com/ZhangJianIsAStark/Demos/blob/master/wifitest
 */
public class WifiHelper {
    private final static String TAG = "WifiHelper";

    private static volatile WifiHelper mInstance = null;
    private WifiManager mWifiManager;
    private WifiInfo mWifiInfo;

    private WifiHelper() {

    }

    public void init(Context context) {
        // 取得WifiManager对象
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        // 取得WifiInfo对象
        mWifiInfo = mWifiManager.getConnectionInfo();
    }

    public static WifiHelper getInstance() {
        // 二次校验，防止多线程调用导致出现多个实例
        if (mInstance == null) {
            synchronized (WifiHelper.class) {
                if (mInstance == null) {
                    mInstance = new WifiHelper();
                }
            }
        }
        return mInstance;
    }

    private WifiConfiguration isExist(String SSID) {
        List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
        if (existingConfigs != null) {
            for (WifiConfiguration existingConfig : existingConfigs) {
                if (existingConfig.SSID.equals("\"" + SSID + "\"") /*&& existingConfig.preSharedKey.equals("\"" + password + "\"")*/) {
                    return existingConfig;
                }
            }
        }
        return null;
    }

    /**
     * 获取WIFI的开关状态
     *
     * @return WIFI的可用状态
     */
    public boolean isWifiEnabled() {
        return null != mWifiManager && mWifiManager.isWifiEnabled();
    }

    /**
     * 打开Wifi
     */
    public void openWiFi() {
        if (!isWifiEnabled() && null != mWifiManager) {
            mWifiManager.setWifiEnabled(true);
        }
    }

    /**
     * 关闭Wifi
     */
    public void closeWiFi() {
        if (isWifiEnabled() && null != mWifiManager) {
            mWifiManager.setWifiEnabled(false);
        }
    }


    /**
     * 根据SSID和密码连接wifi
     * 参考：https://stackoverflow.com/questions/8818290/how-do-i-connect-to-a-specific-wi-fi-network-in-android-programmatically
     * @param ssid
     * @param password
     */
    public void connectWifi(String ssid, String password) {
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = String.format("\"%s\"", ssid);
        wifiConfig.preSharedKey = String.format("\"%s\"", password);
        int netId = mWifiManager.addNetwork(wifiConfig);
        mWifiManager.disconnect();
        mWifiManager.enableNetwork(netId, true);
        mWifiManager.reconnect();
    }
}