package com.rob57530.smsserver;

import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private Button m_btnStart;
	private Button m_btnStop;
	private EditText m_textPort;
	private EditText m_textHttp;
	private EditText m_textName;
	private EditText m_textNumber;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		m_btnStart = (Button) findViewById(R.id.btnStart);
		m_btnStop = (Button) findViewById(R.id.btnStop);
		m_textPort = (EditText) findViewById(R.id.editTextPort);
		m_textHttp = (EditText) findViewById(R.id.editTextHttp);
		m_textName = (EditText) findViewById(R.id.editTextName);
		m_textNumber = (EditText) findViewById(R.id.editTextNumber);
		m_btnStart.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				startServer();
			}
		});
		m_btnStop.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				stopServer();
			}
		});
		m_textName.setHint(Build.MODEL);
		try
		{
			m_textNumber.setHint(((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number());
		}
		catch (Exception e)
		{

		}

		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
		m_textHttp.setText(p.getString("http", m_textHttp.getHint().toString()));
		m_textName.setText(p.getString("name", m_textName.getHint().toString()));
		m_textPort.setText("" + p.getInt("port", 8080));
		m_textNumber.setText(p.getString("number", m_textNumber.getHint().toString()));

		CheckBox checkBoot = ((CheckBox) findViewById(R.id.checkBoxBoot));
		checkBoot.setChecked(p.getBoolean("boot", true));
		checkBoot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				Editor e = p.edit();
				e.putBoolean("boot", isChecked);
				e.commit();
			}
		});

		updateButtons();
		startServer();
	}

	public void startServer()
	{
		int p = 0;
		try
		{
			p = Integer.parseInt(m_textPort.getText().toString());
		}
		catch (Exception e)
		{

		}
		String http = m_textHttp.getText().toString();
		String name = m_textName.getText().toString();
		String number = m_textNumber.getText().toString();

		SharedPreferences pr = PreferenceManager.getDefaultSharedPreferences(this);
		Editor edit = pr.edit();
		edit.putString("http", http);
		edit.putString("name", name);
		edit.putInt("port", p);
		edit.putString("number", number);
		edit.commit();

		try
		{

			if (http.length() == 0)
				throw new Exception("Empty SMS Server");
			if (name.length() == 0)
				throw new Exception("Empty Name");
			if (number.length() == 0)
				throw new Exception("Empty Number");
			if (p < 1 || p > 65535)
				throw new Exception("Invalid Port (1 <= port <= 65535)");

			startService(new Intent(MainActivity.this, SMSServerService.class));
			updateButtons();

		}
		catch (Exception e)
		{
			Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
		}
	}

	public void stopServer()
	{
		stopService(new Intent(MainActivity.this, SMSServerService.class));
		updateButtons();
	}

	private boolean isServiceRunning()
	{
		ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
			if (SMSServerService.class.getName().equals(service.service.getClassName()))
				return true;
		return false;
	}

	private void updateButtons()
	{
		boolean is = isServiceRunning();
		m_btnStart.setEnabled(!is);
		m_btnStop.setEnabled(is);
		m_textPort.setEnabled(!is);
		m_textHttp.setEnabled(!is);
		m_textName.setEnabled(!is);
		m_textNumber.setEnabled(!is);
	}

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

}
