package org.sunnyone.lightcontroller;

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

import android.os.AsyncTask;
import java.net.HttpURLConnection;
import java.net.URL;

import java.io.*;

public class MainActivity extends Activity {

	class AccessCgiTask extends AsyncTask<String, Void, String> {
		private static final String SERVER_URL = "http://192.168.1.1/lightremo/lightremo.cgi?sw=";

		@Override
		protected String doInBackground(String... swParams) {
			HttpURLConnection urlConn = null;
			try {
				URL url = new URL(SERVER_URL + swParams[0]);
				urlConn = (HttpURLConnection)url.openConnection();
				urlConn.connect();
				return urlConn.getResponseMessage();
			} catch (Exception ex) {
				return "Failed to request: " + ex.toString();
			} finally {
				if (urlConn != null)
					urlConn.disconnect();
			}
		}

		@Override
        protected void onPostExecute(String response) {
			Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button buttonOn = (Button)findViewById(R.id.button_on);
		buttonOn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new AccessCgiTask().execute("on");
			}
		});

		Button buttonOff = (Button)findViewById(R.id.button_off);
		buttonOff.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new AccessCgiTask().execute("off");
			}
		});

		Button buttonClose = (Button)findViewById(R.id.button_close);
		buttonClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				MainActivity.this.finish();
			}
		});

	}

	@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) {
        switch (item.getItemId()) {
        case R.id.action_settings:
        	Intent intent = new Intent(this, PrefActivity.class);
        	startActivityForResult(intent, 0);

            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return false;
    }


}

@@ -106,17 +106,152 @@
 oid.
-view.Menu
+content.Context;%0Aimport android.content.Intent;%0Aimport android.content.SharedPreferences;%0Aimport android.view.Menu;%0Aimport android.view.MenuItem
 ;%0Aim
@@ -363,16 +363,62 @@
 ncTask;%0A
+import android.preference.PreferenceManager;%0A%0A
 import j
@@ -598,102 +598,8 @@
 %3E %7B%0A
-%09%09private static final String SERVER_URL = %22http://192.168.1.1/lightremo/lightremo.cgi?sw=%22;%0A%0A
 %09%09@O
@@ -650,23 +650,25 @@
 ring... 
-swParam
+urlString
 s) %7B%0A%09%09%09
@@ -701,16 +701,53 @@
 = null;%0A
+%09%09%09String urlString = urlStrings%5B0%5D;%0A
 %09%09%09try %7B
@@ -773,32 +773,17 @@
 URL(
-SERVER_URL + swParams%5B0%5D
+urlString
 );%0A%09
@@ -954,15 +954,32 @@
  to 
-request
+access %22 + urlString + %22
 : %22 
@@ -1218,24 +1218,291 @@
 ();%0A%09%09%7D%0A%09%7D%0A%0A
+%09private String getLightRemoUrl(Context context, String swParam) %7B%0A%09%09SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);%0A%09%09String lightRemoUrl = pref.getString(%22lightremo_url%22, %22%22);%0A%09%09return lightRemoUrl + %22?sw=%22 + swParam;%0A%09%7D%0A%0A
 %09@Override%0A%09
@@ -1790,32 +1790,100 @@
 Click(View v) %7B%0A
+%09%09%09%09String lightRemoUrl = getLightRemoUrl(MainActivity.this, %22on%22);%0A
 %09%09%09%09new AccessCg
@@ -1898,20 +1898,28 @@
 execute(
-%22on%22
+lightRemoUrl
 );%0A%09%09%09%7D%0A
@@ -2087,24 +2087,93 @@
 k(View v) %7B%0A
+%09%09%09%09String lightRemoUrl = getLightRemoUrl(MainActivity.this, %22off%22);%0A
 %09%09%09%09new Acce
@@ -2192,21 +2192,28 @@
 execute(
-%22off%22
+lightRemoUrl
 );%0A%09%09%09%7D%0A

Load url from the preference.