package com.example.gui02;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import android.view.View.OnClickListener;



import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends Activity {

	
	private Button button;
 	private ToggleButton toggleButton1, toggleButton2;
 	private Button btnDisplay;

 	
 	EditText editTextAddress, editTextPort;
 	Button buttonConnect, buttonClear;
	TextView resultat;
 	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		addListenerOnButton();
		
		buttonConnect = (Button) findViewById(R.id.connectButton);
		resultat = (TextView)findViewById(R.id.resultat);
		
		resultat.setText("46454");
		
		buttonConnect.setOnClickListener(new OnClickListener() 
		{
    	  
	          @Override
	          public void onClick(View arg0) 
	          {
	        	//  Toast.makeText(MainActivity.this, "Biloute", Toast.LENGTH_SHORT).show();
	        	Client myClient = new Client(editTextAddress.getText().toString(), Integer.parseInt(editTextPort.getText().toString())  );
		        pipo id = new pipo();
	        	int res =  id.toto(8,14);
	        	String conversion = Integer.toString(res);
	        	resultat.setText(conversion);
	          }
       });
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void addListenerOnButton() 
	{

		toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
		toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
		btnDisplay = (Button) findViewById(R.id.btnDisplay);

		btnDisplay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) 
			{
				   StringBuffer result = new StringBuffer();
				   result.append("toggleButton1 : ").append(toggleButton1.getText());
				   result.append("\ntoggleButton2 : ").append(toggleButton2.getText());
				   Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_SHORT).show();
			}

		});

	  }
}



class pipo 
{
	 
	//Context c;
	//String dstAddress;
	//int dstPort;
	   
	/*Client(String addr, int port )
	   {
	      dstAddress = addr;
	      dstPort = port;
	      //c= context;
	      //Toast.makeText(c, "dgsqgsgd", Toast.LENGTH_SHORT).show();
	   }*/
	
	//private Context context;
	
	int toto(int a, int b)
	{
		int z = a + b; 
		
		return(z);
		
	  //Toast.makeText(context , "Biloute", Toast.LENGTH_SHORT).show();	
	}
} 
	   


class Client extends AsyncTask<Void, Void, Void> {
	 
	   String dstAddress;
	   int dstPort;
	   
	   Client(String addr, int port)
	   {
	      dstAddress = addr;
	      dstPort = port;
	  
	   }
	 
	   @Override
	   protected Void doInBackground(Void... arg0) 
	   {
	 
			   //   Socket socket = null;
			 /*
			      try {
			         //socket = new Socket(dstAddress, dstPort);
			 
			         
			         
			 
			      } catch (UnknownHostException e) {
			         // TODO Auto-generated catch block
			         e.printStackTrace();
			      } catch (IOException e) {
			         // TODO Auto-generated catch block
			         e.printStackTrace();
			      } finally {
			         if (socket != null) {
			            try {
			               socket.close();
			            } catch (IOException e) {
			               // TODO Auto-generated catch block
			               e.printStackTrace();
			            }
			         }
			      }*/
			      return null;
	   }
	 
	   @Override
	   protected void onPostExecute(Void result) 
	   {
	     
	      //super.onPostExecute(result);
	   }
	 
	}

