package research.day;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

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

public class MainActivity extends Activity implements OnClickListener
{
	private Button lightSwitch;
	private Socket socket;
	private PrintWriter out;
	
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		lightSwitch = (Button)(findViewById(R.id.button));
		lightSwitch.setOnClickListener(this);
		
		try 
		{
			socket = new Socket("192.168.1.81", 3692);
			out = new PrintWriter(socket.getOutputStream(), true);
		} 
		catch (UnknownHostException e1) 
		{
			e1.printStackTrace();
		} 
		catch (IOException e1) 
		{
			e1.printStackTrace();
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) 
	{
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onClick(View view) 
	{
		if (lightSwitch.getText().equals("Turn on"))
		{
			lightSwitch.setText("Turn off");
			out.println("on");
		}
		else if (lightSwitch.getText().equals("Turn off"))
		{
			lightSwitch.setText("Turn on");
			out.println("off");
		}
	}
}
