package com.example.patrice.rerservation01;


import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

// http://acesyde.developpez.com/tutoriels/android/wifi-android/

// http://cedric.cnam.fr/~farinone/RSX116/WiFiAndroid.pdf

public class wifi extends AppCompatActivity
{

    WifiManager wifimanager;

    TextView textWifiInfo;

    private static final int REQUEST_PERMISSION = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wifi_layout);

        textWifiInfo = (TextView)findViewById(R.id.wifiinfo);

        String strWifiInfo = "";


        //WifiManager mainWifiObj;
        //mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);


        if (ContextCompat.checkSelfPermission(wifi.this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(wifi.this, new String[]{Manifest.permission.ACCESS_WIFI_STATE}, REQUEST_PERMISSION);
        }

        Context context = null;

        WifiManager wifiMgr = (WifiManager) getBaseContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);


        int status = wifiMgr.getWifiState();

        String str_status;

        switch (wifiMgr.getWifiState())
        {
            case 0:
                str_status =  "disabling";
            case 1:
                str_status = "disabled";
            case 2:
                str_status = "enabling";
            case 3:
                str_status = "enabled";
            default:
                str_status = "unknown";
        }

        boolean etat = wifiMgr.isWifiEnabled();


        WifiInfo info = wifiMgr.getConnectionInfo();
        int speed = info.getLinkSpeed();
        String units = info.LINK_SPEED_UNITS;
        String ssid = info.getSSID();


        WifiInfo connectionInfo = wifiMgr.getConnectionInfo();

        int ipAddress = connectionInfo.getIpAddress();
        String ipString = String.format("%d.%d.%d.%d",
                (ipAddress & 0xff),
                (ipAddress >> 8 & 0xff),
                (ipAddress >> 16 & 0xff),
                (ipAddress >> 24 & 0xff));

        final int NumOfRSSILevels = 5;

        strWifiInfo +=
                "SSID: " + connectionInfo.getSSID() + "\n" +
                        "BSSID: " + connectionInfo.getBSSID() + "\n" +
                        "IP Address: " + ipString + "\n" +
                        "MAC Address: " + connectionInfo.getMacAddress() + "\n" +
                        "Frequency: " + connectionInfo.getFrequency() + WifiInfo.FREQUENCY_UNITS + "\n" +
                        "LinkSpeed: " + connectionInfo.getLinkSpeed() + WifiInfo.LINK_SPEED_UNITS + "\n" +
                        "Rssi: " + connectionInfo.getRssi() + "dBm" + "\n" +
                        "Rssi Level: " +
                        WifiManager.calculateSignalLevel(connectionInfo.getRssi(), NumOfRSSILevels) +
                        " of " + NumOfRSSILevels;


        textWifiInfo.setText(strWifiInfo);

        String zorro = "44";


       // WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        //WifiManager wmgr = (WifiManager)Context.getSystemService(Context.WIFI_SERVICE);

        //Context c = null;


     //   ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);



    //    WifiManager mainWifiObj;

        //mainWifiObj = (WifiManager) wifi.getSystemService(Context.WIFI_SERVICE);

   //     mainWifiObj = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);




       /// int status = mainWifiObj.getWifiState();


       // ConnectivityManager conManager = (ConnectivityManager) c.getSystemService(Context.WIFI_SERVICE);


    }


}