package de.tum.in.mislcontrol.wizard;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.codepond.wizardroid.WizardStep;

import java.util.List;

import de.tum.in.mislcontrol.MainActivity;
import de.tum.in.mislcontrol.R;
import de.tum.in.mislcontrol.communication.ASEPConnector;

public class Step4 extends WizardStep {

    /* WIFI ICON BY Bjoern Andersson */

    /**
     * The handler for delayed events.
     */
    private final Handler delayedActionHandler = new Handler();

    //Wire the layout to the step
    public Step4() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.wizard_instructionstep, container, false);
        TextView tv = (TextView) v.findViewById(R.id.instructionTextView);
        tv.setText(R.string.wizard_step_4);
        ImageView iv = (ImageView) v.findViewById(R.id.instructionImageView);
        iv.setImageResource(R.drawable.wifi);

        WifiManager wifiMan = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        wifiMan.setWifiEnabled(true);

        WifiConfiguration wifiC = new WifiConfiguration();
        wifiC.SSID = "\"" + ASEPConnector.WIFI_SSID + "\"";
        wifiC.preSharedKey = "\"" + "password" + "\"";

        //TODO:
        //wifiMan.addNetwork(wifiC);
        List<WifiConfiguration> configs = wifiMan.getConfiguredNetworks();
        if (configs != null)
            for (WifiConfiguration config : configs) {
                if (config.SSID != null && config.SSID.equals(wifiC.SSID)) {
                    wifiMan.disconnect();
                    wifiMan.enableNetwork(config.networkId, true);
                    wifiMan.reconnect();
                    notifyCompleted();
                    break;
                }
            }

        return v;
    }

    @Override
    public void onExit(int exitCode) {
        switch (exitCode) {
            case WizardStep.EXIT_NEXT:
                // forward to main activity
                getActivity().setContentView(R.layout.activity_start);
                delayedActionHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(getActivity(), MainActivity.class);
                        startActivity(intent);
                    }
                }, 2000);
                break;
            case WizardStep.EXIT_PREVIOUS: //This means skip for us
                break;
        }
    }
}

@@ -99,51 +99,8 @@
 nt;%0A
-import android.net.wifi.WifiConfiguration;%0A
 impo
@@ -128,24 +128,24 @@
 ifiManager;%0A
+
 import andro
@@ -392,32 +392,8 @@
 p;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -466,66 +466,8 @@
 l.R;
-%0Aimport de.tum.in.mislcontrol.communication.ASEPConnector;
 %0A%0Apu
@@ -1274,16 +1274,49 @@
 RVICE);%0A
+        if (wifiMan != null)%0A    
         
@@ -1349,722 +1349,8 @@
 e);%0A
-%0A        WifiConfiguration wifiC = new WifiConfiguration();%0A        wifiC.SSID = %22%5C%22%22 + ASEPConnector.WIFI_SSID + %22%5C%22%22;%0A        wifiC.preSharedKey = %22%5C%22%22 + %22password%22 + %22%5C%22%22;%0A%0A        //TODO:%0A        //wifiMan.addNetwork(wifiC);%0A        List%3CWifiConfiguration%3E configs = wifiMan.getConfiguredNetworks();%0A        if (configs != null)%0A            for (WifiConfiguration config : configs) %7B%0A                if (config.SSID != null && config.SSID.equals(wifiC.SSID)) %7B%0A                    wifiMan.disconnect();%0A                    wifiMan.enableNetwork(config.networkId, true);%0A                    wifiMan.reconnect();%0A                    notifyCompleted();%0A                    break;%0A                %7D%0A            %7D%0A%0A
     

remove wifi connecting since it doesn't do anything as of now