/***
	Copyright (c) 2008 CommonsWare, LLC
	
	Licensed under the Apache License, Version 2.0 (the "License"); you may
	not use this file except in compliance with the License. You may obtain
	a copy of the License at
		http://www.apache.org/licenses/LICENSE-2.0
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

package com.commonsware.android.service;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;

public class MailBuzz extends Activity {
	private static final int CLOSE_ID = Menu.FIRST+1;
	private Intent serviceIntent=null;
	private IBuzz service=null;
	private Button checkNowButton=null;
	private Button setAccountButton=null;
	private CheckBox setEnabled=null;
	private RadioGroup accountType=null;
	private EditText server=null;
	private EditText user=null;
	private EditText password=null;
	private EditText pollPeriod=null;
	private ServiceConnection svcConn=new ServiceConnection() {
		public void onServiceConnected(ComponentName className,
										IBinder binder) {
			service=IBuzz.Stub.asInterface(binder);
			checkNowButton.setEnabled(true);
			setAccountButton.setEnabled(true);
			setEnabled.setEnabled(true);
			
			try {
				setEnabled.setChecked(service.isEnabled());
			}
			catch (DeadObjectException e) {
				svcConn.onServiceDisconnected(null);
			}
		}

		public void onServiceDisconnected(ComponentName className) {
			service=null;
 			checkNowButton.setEnabled(false);
			setAccountButton.setEnabled(false);
			setEnabled.setEnabled(false);
		}
	};
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		
		serviceIntent=new Intent(this, MailBuzzService.class);
		accountType=(RadioGroup)findViewById(R.id.account_type);
		server=(EditText)findViewById(R.id.server);
		user=(EditText)findViewById(R.id.user);
		password=(EditText)findViewById(R.id.pw);
		pollPeriod=(EditText)findViewById(R.id.pollPeriod);
		
		checkNowButton=(Button)findViewById(R.id.check_now);
		checkNowButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				try {
					service.checkNow();
				}
				catch (DeadObjectException e) {
					svcConn.onServiceDisconnected(null);
				}
			}
		});
		
		setAccountButton=(Button)findViewById(R.id.set_account);
		setAccountButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				String type=MailBuzzService.POP3;
				
				if (accountType.getCheckedRadioButtonId()==R.id.imap) {
					type=MailBuzzService.IMAP;
				}
				
				SharedPreferences settings=getSharedPreferences(MailBuzzService.PREFS, 0);
				SharedPreferences.Editor editor=settings.edit();
				
				editor.putString("type", type);
				editor.putString("server", server.getText().toString());
				editor.putString("user", user.getText().toString());
				editor.putString("pw", password.getText().toString());
				editor.putInt("pollPeriod", new Integer(pollPeriod
													.getText()
													.toString()));
				editor.commit();
			}
		});
		
		setEnabled=(CheckBox)findViewById(R.id.enabled);
		setEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				try {
					if (isChecked) {
						startService(serviceIntent, new Bundle());
					}
					else {
						stopService(serviceIntent);
					}
					
					service.enable(isChecked);
				}
				catch (DeadObjectException e) {
					svcConn.onServiceDisconnected(null);
				}
			}
		});
		
		bindService(serviceIntent, svcConn, BIND_AUTO_CREATE);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		SharedPreferences settings=getSharedPreferences(MailBuzzService.PREFS, 0);
		
		if (settings.getString("type", MailBuzzService.POP3).equals(MailBuzzService.POP3)) {
			accountType.check(R.id.pop);
		}
		else {
			accountType.check(R.id.imap);
		}
		
		server.setText(settings.getString("server", ""));
		user.setText(settings.getString("user", ""));
		password.setText(settings.getString("pw", ""));
		pollPeriod.setText(new Integer(settings.getInt("pollPeriod", 5)).toString());
		
		NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
		nm.cancel(MailBuzzService.NOTIF_ID);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		unbindService(svcConn);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, CLOSE_ID, "Close", R.drawable.eject)
				.setAlphabeticShortcut('c');

		return(super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item) {
		Intent i;
		
		switch (item.getId()) {
			case CLOSE_ID:
				finish();
				return(true);
		}

		return(super.onOptionsItemSelected(item));
	}
}