package com.example.gui03;

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.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

//http://androidsrc.net/android-client-server-using-sockets-client-implementation/

public class MainActivity extends Activity {

		private Button button;
	 	private ToggleButton toggleButton1, toggleButton2;
	 	private Button btnDisplay;
	 	
	 	EditText editTextAddress, editTextPort;
	 	Button buttonConnect, buttonClear;
	 	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
		
			addListenerOnButton();
		
			editTextAddress = (EditText) findViewById(R.id.addressEditText);
			editTextPort = (EditText) findViewById(R.id.portEditText);
			buttonConnect = (Button) findViewById(R.id.connectButton);
				
	      
			buttonConnect.setOnClickListener(new OnClickListener() 
			{
	    	  
		          @Override
		          public void onClick(View arg0) 
		          {
		             Client myClient = new Client(editTextAddress.getText().toString(), Integer.parseInt(editTextPort.getText().toString()));
		             myClient.execute();
		          }
	       });
	  
	       buttonClear.setOnClickListener(new OnClickListener() 
	       {
	  
		          @Override
		          public void onClick(View v) 
		          {
		             int titi = 5;
		          }
	       });
	      
	}

	@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 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;
		      String data = "Toobie ornaught toobie";
		      
		      try 
		      {
		    	  	socket = new Socket(dstAddress, dstPort);
		    	  	// envoi 
			        PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
					pw.println("cmd");
		      } 
		      
		      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)
	   {
		   int aqw = 5;
	      //textResponse.setText(response);
	      //super.onPostExecute(result);
		 //  Toast.makeText(getApplicationContext(), "dkjfgdklskj", Toast.LENGTH_SHORT).show();
	   }
	 
}
