package com.example.smarthome;

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

import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	boolean wasEnabled;
    EditText edtname,edtpwd;
	Button btncancel,btnlogin;
	WifiManager mainWifi;
    WifiReceiver receiverWifi;
    StringBuilder sb = new StringBuilder();
    static int z=100;
    private final Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		edtname=(EditText)findViewById(R.id.edtname);
		edtpwd=(EditText)findViewById(R.id.edtpswd);
		
		
		btnlogin=(Button)findViewById(R.id.btn);
		 mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

	        receiverWifi = new WifiReceiver();
	        registerReceiver(receiverWifi, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
	       wasEnabled = mainWifi.isWifiEnabled();
	        mainWifi.setWifiEnabled(true);
	        mainWifi.startScan();
	
		btnlogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				

				
				if(edtname.getText().toString().equals("cdac")&&edtpwd.getText().toString().equals("desd")){
					Intent intent=new Intent(MainActivity.this,SecondPage.class);
						startActivity(intent);
				}
				else
					 Toast.makeText(MainActivity.this, "Wrong id or password", Toast.LENGTH_LONG).show();
			}
				
			});

		
	
	}

	 
	 public boolean onCreateOptionsMenu(Menu menu) {
	        menu.add(0, 0, 0, "Refresh");
	        return super.onCreateOptionsMenu(menu);
	   }
	   
	
	 public boolean onKeyDown(int keyCode, KeyEvent event){
		 if(keyCode == KeyEvent.KEYCODE_BACK)
		 {
			 mainWifi.setWifiEnabled(wasEnabled);
			 this.finish();
		 }
		 
		 return super.onKeyDown(keyCode, event);
		 }



	    @Override
	    protected void onPause()
	    {
	        unregisterReceiver(receiverWifi);
	        super.onPause();
	    }

	    @Override
	    protected void onResume()
	    {
	        registerReceiver(receiverWifi, new IntentFilter(
	                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
	        		super.onResume();
	    }

	    class WifiReceiver extends BroadcastReceiver
	    {
	    	int id=0;
	        public void onReceive(Context c, Intent intent)
	        {
	        	int j=0;
	        	List<ScanResult> result= mainWifi.getScanResults();
	        	for(j=0;j<result.size();j++){
	        		if(result.get(j).SSID.equals("AndroidAP")){
	        			Toast.makeText(MainActivity.this, "Network found", Toast.LENGTH_LONG).show();
	        			break;
	        		}
	        	}
	        	if(j==result.size()){
	        		Toast.makeText(MainActivity.this, "Network not found", Toast.LENGTH_LONG).show();
	        		
	        	}	
	        	else{
	        		String ssid = "AndroidAP";
	        		String pass = "passwora";
	            
	        		WifiConfiguration conf = new WifiConfiguration();
	        		conf.SSID= String.format("\"%s\"", ssid);
	            
	        		conf.preSharedKey =String.format("\"%s\"", pass);
	        		mainWifi= (WifiManager)getSystemService(WIFI_SERVICE);
	        		int id= mainWifi.addNetwork(conf);
	        		mainWifi.disconnect();
	            	mainWifi.enableNetwork(id, true);
	            	mainWifi.reconnect();
	    	        Toast.makeText(MainActivity.this, "Connected to"+ssid, Toast.LENGTH_LONG).show();
	            	z=1;
	         }
	        	

	       }
	   }
}
