package jp.muo.smsproxy;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.widget.Toast;

public class SmsProxyManager {
	public enum Mode {
		SMS, CALL
	};

	private static final String KEY_SMS_ENABLED = "sms_enabled";
	private static final String KEY_CALL_ENABLED = "call_enabled";
	private static final String KEY_PROXY_TO = "proxyTo";
	private Context ctx = null;
	private SharedPreferences prefs = null;
	private Mode sendMode = Mode.SMS;

	public SmsProxyManager(Context ctx) {
		this.ctx = ctx;
		prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx);
	}

	public boolean isEnabled() {
		if (this.ctx == null || prefs == null) {
			return false;
		}
		switch (this.sendMode) {
		case SMS:
			return prefs.getBoolean(KEY_SMS_ENABLED, false);
		case CALL:
			return prefs.getBoolean(KEY_CALL_ENABLED, false);
		default:
			return false;
		}
	}

	public String getProxyTo() {
		if (this.ctx == null || prefs == null) {
			return "";
		}
		return prefs.getString(KEY_PROXY_TO, "");
	}

	public boolean send(String msgText) {
		if (this.ctx == null || prefs == null) {
			return false;
		}

		if (msgText.equals("")) {
			return false;
		}

		String msgSucceeded = "";
		String msgFailed = "";
		switch (this.sendMode) {
		case SMS:
			msgSucceeded = this.ctx.getString(R.string.forward_sms_ok);
			msgFailed = this.ctx.getString(R.string.forward_sms_ng);
			break;
		case CALL:
			msgSucceeded = this.ctx.getString(R.string.forward_call_ok);
			msgFailed = this.ctx.getString(R.string.forward_call_ng);
			break;
		}

		try {
			SmsManager sms = SmsManager.getDefault();
			sms.sendTextMessage(this.getProxyTo(), null, msgText, null, null);
			Toast.makeText(this.ctx, msgSucceeded, Toast.LENGTH_LONG).show();
			return true;
		} catch (Exception e) {
			System.out.println(e.getLocalizedMessage());
			Toast.makeText(this.ctx, msgFailed, Toast.LENGTH_LONG).show();
			return false;
		}
	}

	/**
	 * @deprecated setType(Mode) -> send(String) is deprecated as of 1.1b. Should use send(Mode, String)
	 * @param call
	 */
	public void setType(Mode call) {
		this.sendMode = call;
	}

	public boolean send(Mode smsMode, String msgText) {
		this.sendMode = smsMode;
		return send(msgText);
	}
}

@@ -1110,24 +1110,228 @@
 O, %22%22);%0A%09%7D%0A%0A
+%09/**%0A%09 * Sends SMS text message based on specified message types.%0A%09 * @param smsMode message type: SMS / CALL%0A%09 * @param msgText message body%0A%09 * @return whether message submission succeeded or not.%0A%09 */%0A
 %09public bool
@@ -1335,24 +1335,38 @@
 oolean send(
+Mode smsMode, 
 String msgTe
@@ -1414,40 +1414,12 @@
 null
-) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%0A%09%09if (
+ %7C%7C 
 msgT
@@ -1457,16 +1457,45 @@
 se;%0A%09%09%7D%0A
+%09%09%0A%09%09this.sendMode = smsMode;
 %0A%09%09Strin
@@ -1540,16 +1540,19 @@
 d = %22%22;%0A
+%09%09%0A
 %09%09switch
@@ -2243,305 +2243,7 @@
 %7D%0A%09%7D
-%0A%0A%09/**%0A%09 * @deprecated setType(Mode) -%3E send(String) is deprecated as of 1.1b. Should use send(Mode, String)%0A%09 * @param call%0A%09 */%0A%09public void setType(Mode call) %7B%0A%09%09this.sendMode = call;%0A%09%7D%0A%0A%09public boolean send(Mode smsMode, String msgText) %7B%0A%09%09this.sendMode = smsMode;%0A%09%09return send(msgText);%0A%09%7D
 %0A%7D%0A

remove deprecated method