package com.example.iotcloudapp;




import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
	 EditText et1,et2,et3,et4,et5,et6,et7,et8,data1,data2;
	
	 MainActivity tact;
	 public boolean exitflag= false;
	 private static final int REQUEST_CONNECT_DEVICE = 1;
	
	 public String alertmsg=null;
	// String datas = null;
	 
	Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;
	 String text;
	 public String cstatus="*00000000#";
	 public String b1status="0";
	 public String b2status="0";
	 public String b3status="0";
	 public String b4status="0";
	 public String b5status="0";
	 public String b6status="0";
	 public String b7status="0";
	 public String b8status="0";
	 
	 public boolean b1flag=false;
	  public boolean b2flag=false;
	  public boolean b3flag=false;
	  public boolean b4flag=false;
	  public boolean b5flag=false;
	  public boolean b6flag=false;
	  public boolean b7flag=false;
	  public boolean b8flag=false;
	  private MenuItem mMenuItemConnect;

	 
	 TextView tv1,tv2,tv3,presstext;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		tact=this;
		//addListenerOnButton();

	data1=(EditText) findViewById(R.id.entry1);
	data2=(EditText) findViewById(R.id.entry2);
		
		tv1=(TextView) findViewById(R.id.logid);
		tv2=(TextView) findViewById(R.id.logtime);
		tv3=(TextView) findViewById(R.id.logdate);
		
		
		b2 = (Button) findViewById(R.id.b2);
		b3 = (Button) findViewById(R.id.b3);
		b4 = (Button) findViewById(R.id.b4);
		b5 = (Button) findViewById(R.id.b5);
		b6 = (Button) findViewById(R.id.b6);
		b7 = (Button) findViewById(R.id.b7);
		b8 = (Button) findViewById(R.id.b8);
        //receivedata();   
		
		et1=(EditText) findViewById(R.id.device1);
		et2=(EditText) findViewById(R.id.device2);
		et3=(EditText) findViewById(R.id.device3);
		et4=(EditText) findViewById(R.id.device4);
		et5=(EditText) findViewById(R.id.device5);
		et6=(EditText) findViewById(R.id.device6);
		et7=(EditText) findViewById(R.id.device7);
		et8=(EditText) findViewById(R.id.device8);
		
		b1 = (Button) findViewById(R.id.b1);
		b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		
		if(b1flag==false)
		{
				
				b1.setBackgroundResource(R.drawable.poweron);
				b1status="1";
	    		et1.setText("On");
				b1flag=true;
		}
		else
		{
			
			b1.setBackgroundResource(R.drawable.power);
			b1status="0";
			et1.setText("OFF");
			b1flag=false;
		}
		  receivedata();   
         }
      });
      
      b2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		
		if(b2flag==false)
		{
				
				b2.setBackgroundResource(R.drawable.poweron);
				b2status="1";
	    		et2.setText("On");
				b2flag=true;
		}
		else
		{
			
			b2.setBackgroundResource(R.drawable.power);
			b2status="0";
			et2.setText("OFF");
			b2flag=false;
		}
		  receivedata();   
         }
      });
      
      b3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		
		if(b3flag==false)
		{
				
				b3.setBackgroundResource(R.drawable.poweron);
				b3status="1";
	    		et3.setText("On");
				b3flag=true;
		}
		else
		{
			
			b3.setBackgroundResource(R.drawable.power);
			b3status="0";
			et3.setText("OFF");
			b3flag=false;
		}
		  receivedata();   
         }
      });
      
      b4.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		
		if(b4flag==false)
		{
				
				b4.setBackgroundResource(R.drawable.poweron);
				b4status="1";
	    		et4.setText("On");
				b4flag=true;
		}
		else
		{
			
			b4.setBackgroundResource(R.drawable.power);
			b4status="0";
			et4.setText("OFF");
			b4flag=false;
		}
		  receivedata();   
       }
    });
    
    b5.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		
		if(b5flag==false)
		{
				
				b5.setBackgroundResource(R.drawable.poweron);
				b5status="1";
	    		et5.setText("On");
				b5flag=true;
		}
		else
		{
			
			b5.setBackgroundResource(R.drawable.power);
			b5status="0";
			et5.setText("OFF");
			b5flag=false;
		}
		  receivedata();   
       }
    });
    
    b6.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		
		if(b6flag==false)
		{
				
				b6.setBackgroundResource(R.drawable.poweron);
				b6status="1";
	    		et6.setText("On");
				b6flag=true;
		}
		else
		{
			
			b6.setBackgroundResource(R.drawable.power);
			b6status="0";
			et6.setText("OFF");
			b6flag=false;
		}
		  receivedata();   
       }
    });
    
    b7.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
	
	if(b7flag==false)
	{
			
			b7.setBackgroundResource(R.drawable.poweron);
			b7status="1";
    		et7.setText("Device 7 On");
			b7flag=true;
	}
	else
	{
		
		b7.setBackgroundResource(R.drawable.power);
		b7status="0";
		et7.setText("Device 7 OFF");
		b7flag=false;
	}
	  receivedata();   
   }
});

b8.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
	
	if(b8flag==false)
	{
			
			b8.setBackgroundResource(R.drawable.poweron);
			b8status="1";
    		et8.setText("Device 8 On");
			b8flag=true;
	}
	else
	{
		
		b8.setBackgroundResource(R.drawable.power);
		b8status="0";
		et8.setText("Device 8 OFF");
		b8flag=false;
	}
	  receivedata();   
   }
});
		
        new Thread(new Runnable() {

            @Override
            public void run() {
                for(int i = 0; i < 100; i++) {
                	
                	if(exitflag){
                		break;
                	}

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                        	 
                        	 receivedata();                  	
                        	  
                           
                        }
                    });
                   try {
                        Thread.sleep(20000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                   
                }
            }
        }).start();
        
        

	}
	
	 @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
         case R.id.connect:
         	
        
             return true;
         case R.id.EXIT:
       	 }
         return false;
     }
	 private void receivedata() {
	 		
	 		//HttpConnectSample httpConnectSample = new HttpConnectSample();
	 		
	 		// TODO Auto-generated method stub
	 		Intent serverIntent = new Intent(MainActivity.this, HttpConnectSample.class);
	 		Bundle b= new Bundle();
			//	 b.putString("name", name.getText().toString());
	 		cstatus=b1status+b2status+b3status+b4status+b5status+b6status+b7status+b8status;
				b.putString("cstatus", cstatus);
				
				serverIntent.putExtras(b);
	 		startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
	 		
	 	
	 	
	 	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		   MenuInflater inflater = getMenuInflater();
	         inflater.inflate(R.menu.option_menu, menu);
	         mMenuItemConnect = menu.getItem(0);
	         return true;
	}
	
	public void showCustomAlert()
    {
        Context context =tact.getBaseContext();
       
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // Call toast.xml file for toast layout
        View toastRoot = inflater.inflate(R.layout.toast_layout, null);
        ImageView imagev = (ImageView) toastRoot.findViewById(R.id.image);
        
        TextView textv = (TextView) toastRoot.findViewById(R.id.text);
        imagev.setImageResource(R.drawable.reddot);
        textv.setText(alertmsg);
        Toast toast = new Toast(context);

        // Set layout to toast
        toast.setView(toastRoot);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
                0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();

    }
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	      //  Bundle bundle = data.getExtras();
	        	             // When DeviceListActivity returns with a device to connect
		
		/* switch (requestCode) {
         
         case REQUEST_CONNECT_DEVICE:

          
	             if (resultCode == Activity.RESULT_OK) {
	             */
	            	 String address = data.getExtras()
                             .getString(HttpConnectSample.CARD_DATA);
	            	 Toast.makeText(getApplicationContext(), address, Toast.LENGTH_SHORT).show();
	            	 
	            	 if(address != null && !address.isEmpty())
	            	 {
	            	//	Toast.makeText(getApplicationContext(), "Inside", Toast.LENGTH_SHORT).show();
		            	  
	            //	String[] datas = address.split(",");
	            		 
	              data1.setText(address);
	            // data2.setText(datas[1]);
	            	//et3.setText(datas[1]);
	            	//et5.setText(datas[2]);
	            	//int e1=Integer.parseInt(datas);
	            	//int e2=Integer.parseInt(datas[1]);
	            	//int e3=Integer.parseInt(datas[2]);
	            	            	
	            	 }		

	        	     }
	

	
	
}
