/* 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.tourit;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;


public class ConfigActivity extends Activity {
	public static final String CONFIGURED="true";
	public static final String LOCATION_PROVIDER="provider";
	public static final String SHOW_LOCATION="show_loc";
	public static final String FOLLOW_ME="follow_me";
	public static final String ALERT_SOUND="alert_sound";
	public static final String ALERT_VIBRATE="alert_vibrate";
	public static final String ALERT_INSISTENT="alert_insistent";
	public static final String SHOW_HOME="show_home";
	public static final String PREFS="common";
	
	private static final int HELP_ID = Menu.FIRST+100;
	private static final int CLOSE_ID = Menu.FIRST+2;
	
	private Spinner providers;
	private List<ProviderWrapper> listWrappers=new ArrayList();

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.config);

		LocationManager mgr=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
		
		for (LocationProvider p : mgr.getProviders()) {
			listWrappers.add(new ProviderWrapper(p));
		}
		
		ArrayAdapter<Waypoint> aa=new ArrayAdapter<Waypoint>(this,
															R.layout.spinner,
															(List)listWrappers);
		
		providers=(Spinner)findViewById(R.id.providers);
		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		providers.setAdapter(aa);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		SharedPreferences prefs=getSharedPreferences(PREFS, 0);
		String provider=prefs.getString(LOCATION_PROVIDER, null);
		
		if (provider!=null) {
			int i=0;
			
			for (ProviderWrapper wrap : listWrappers) {
				if (wrap.toString().equals(provider)) {
					providers.setSelection(i);
					break;
				}
				
				i++;
			}
		}
		
		((CheckBox)findViewById(R.id.show_loc)).setChecked(prefs.getBoolean(SHOW_LOCATION, true));
		((CheckBox)findViewById(R.id.follow_loc)).setChecked(prefs.getBoolean(FOLLOW_ME, true));
		((CheckBox)findViewById(R.id.alert_sound)).setChecked(prefs.getBoolean(ALERT_SOUND, true));
		((CheckBox)findViewById(R.id.alert_vibrate)).setChecked(prefs.getBoolean(ALERT_VIBRATE, true));
		((CheckBox)findViewById(R.id.alert_insistent)).setChecked(prefs.getBoolean(ALERT_INSISTENT, true));
		
		if (prefs.getBoolean(SHOW_HOME, false)) {
			((RadioButton)findViewById(R.id.show_home)).setChecked(true);
		}
		else {
			((RadioButton)findViewById(R.id.show_tours)).setChecked(true);
		}
	}
	
	@Override
	public void onPause() {
		super.onPause();
		
		SharedPreferences.Editor prefs=getSharedPreferences(PREFS, 0).edit();
		
		prefs.putBoolean(CONFIGURED, true);
		prefs.putString(LOCATION_PROVIDER, providers.getSelectedItem().toString());
		prefs.putBoolean(SHOW_LOCATION, ((CheckBox)findViewById(R.id.show_loc)).isChecked());
		prefs.putBoolean(FOLLOW_ME, ((CheckBox)findViewById(R.id.follow_loc)).isChecked());
		prefs.putBoolean(ALERT_SOUND, ((CheckBox)findViewById(R.id.alert_sound)).isChecked());
		prefs.putBoolean(ALERT_VIBRATE, ((CheckBox)findViewById(R.id.alert_vibrate)).isChecked());
		prefs.putBoolean(ALERT_INSISTENT, ((CheckBox)findViewById(R.id.alert_insistent)).isChecked());
		prefs.putBoolean(SHOW_HOME, ((RadioButton)findViewById(R.id.show_home)).isChecked());
		
		if (!prefs.commit()) {
			Toast.makeText(this, R.string.save_prefs_failed, 2000).show();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, HELP_ID, "Help", R.drawable.help)
				.setAlphabeticShortcut('h');
		menu.add(0, CLOSE_ID, "Close", R.drawable.eject)
				.setAlphabeticShortcut('c');

		return(super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item) {
		switch (item.getId()) {
			case HELP_ID:
				Intent i=new Intent(this, HelpActivity.class);

				i.putExtra(HelpActivity.PAGE, "config");
				startActivity(i);
				return(true);
				
			case CLOSE_ID:
				finish();
				return(true);
		}

		return(super.onOptionsItemSelected(item));
	}
	
	class ProviderWrapper {
		LocationProvider p;
		
		ProviderWrapper(LocationProvider p) {
			this.p=p;
		}
		
		public String toString() {
			return(p.getName());
		}
	}
}