package com.tuxofil.android;

import android.view.View;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;

import android.widget.ToggleButton;
import android.widget.EditText;

/**
 * Represents application's user interface.
 */
public class Gps2UdpActivity extends Activity {

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Config config = new Config(this);
        ((ToggleButton) findViewById(R.id.btn_enabled)).
            setChecked(config.isEnabled());
        ((EditText) findViewById(R.id.dst_host)).
            setText(config.getHost());
        ((EditText) findViewById(R.id.dst_port)).
            setText(String.valueOf(config.getPort()));
        ((EditText) findViewById(R.id.send_period)).
            setText(String.valueOf(config.getPeriod()));
	if (config.isEnabled()) {
	    // automatically start the background service
	    // if not started yet
            startService(new Intent(Gps2UdpService.ACTION_START));
	}
    }

    /**
     * Called when the activity is lost the focus
     * but is still visible to the user.
     */
    @Override
    public void onPause() {
        super.onPause();
        applyConfigs();
    }

    /**
     * Called when Enable/Disable button clicked.
     */
    public void onOnOffClicked(View view) {
        if (((ToggleButton) view).isChecked()) {
            startService(new Intent(Gps2UdpService.ACTION_START));
        } else {
            stopService(new Intent(Gps2UdpService.ACTION_STOP));
        }
    }

    /**
     * Called when Apply button clicked.
     */
    public void onApplyClicked(View view) {
        applyConfigs();
    }

    /**
     * Validate the configuration values from the form
     * and store them to the application's persistent storage.
     */
    private void applyConfigs() {
        boolean enabled =
            ((ToggleButton) findViewById(R.id.btn_enabled)).isChecked();
        String host = _getText(R.id.dst_host);
        int port = Integer.parseInt(_getText(R.id.dst_port));
        if (port < 1) port = 1;
        if (port >= 0xffff) port = 0xffff - 1;
        int period = Integer.parseInt(_getText(R.id.send_period));
        if (period < 1) period = 1;
        (new Config(this)).set(enabled, host, port, period);
    }

    /**
     * Return the text from the text field.
     */
    private String _getText(int id) {
        return ((EditText) findViewById(id)).getText().toString();
    }
}

@@ -975,39 +975,8 @@
 );%0A%09
-if (config.isEnabled()) %7B%0A%09    
 // a
@@ -1020,16 +1020,8 @@
 vice
-%0A%09    //
  if 
@@ -1040,78 +1040,47 @@
 yet%0A
-            startService(new Intent(Gps2UdpService.ACTION_START
+%09setDaemonEnabled(config.isEnabled(
 ));%0A
-%09%7D%0A
     
@@ -1397,27 +1397,33 @@
 view) %7B%0A
-        if 
+%09setDaemonEnabled
 (((Toggl
@@ -1453,169 +1453,9 @@
 d())
- %7B%0A            startService(new Intent(Gps2UdpService.ACTION_START));%0A        %7D else %7B%0A            stopService(new Intent(Gps2UdpService.ACTION_STOP));%0A        %7D
+;
 %0A   
@@ -2386,11 +2386,324 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Enable or disable the daemon according to the argument value.%0A     */%0A    private void setDaemonEnabled(boolean enabled) %7B%0A%09if (enabled) %7B%0A            startService(new Intent(Gps2UdpService.ACTION_START));%0A        %7D else %7B%0A            stopService(new Intent(Gps2UdpService.ACTION_STOP));%0A%09%7D%0A    %7D
 %0A%7D%0A

move daemon on/off to separate method