/* 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.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.drawable.PaintDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class TourListActivity extends Activity
	implements AdapterView.OnItemClickListener {
	private static final String[] PROJECTION = new String[] {
			Provider.Tours.ID, Provider.Tours.TITLE};
	private static final int FLIP_ID = Menu.FIRST+1;
	private static final int CONFIG_ID = Menu.FIRST+2;
	private static final int HELP_ID = Menu.FIRST+3;
	private static final int CLOSE_ID = Menu.FIRST+4;

	private Cursor toursCursor;
	private ViewFlipper flipper;
	private ListView list;
	private Menu.Item flipMenu;
	private boolean firstPass=true;
			
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		
		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(Provider.Tours.CONTENT_URI);
		}
		
		toursCursor = managedQuery(getIntent().getData(), PROJECTION, null, null, null);
		
		list=(ListView)findViewById(android.R.id.list);
		list.setOnItemClickListener(this);
		
		ListAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.tourlist_item, toursCursor,
				new String[] {Provider.Tours.TITLE},
				new int[] {android.R.id.text1});
		list.setAdapter(adapter);
		
		list.setDivider(new PaintDrawable(0xFF000000));
		list.setDividerHeight(2);
		
		flipper=(ViewFlipper)findViewById(R.id.flipper);
		((WebView)findViewById(R.id.browser)).loadUrl("file:///android_asset/about.html");
		((ImageButton)findViewById(R.id.show_tours)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				showList();
			}
		});
		((ImageButton)findViewById(R.id.configure)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				startActivity(new Intent(TourListActivity.this, ConfigActivity.class));
			}
		});
		((ImageButton)findViewById(R.id.help)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				startActivity(new Intent(TourListActivity.this, HelpActivity.class));
			}
		});
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		if (firstPass) {
			SharedPreferences prefs=getSharedPreferences(ConfigActivity.PREFS, 0);
			
			if (prefs.getBoolean(ConfigActivity.SHOW_HOME, true)) {
				showHome();
			}
			else {
				showList();
			}
		}
		
		firstPass=false;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.setQwertyMode(true);
		flipMenu=menu.add(0, FLIP_ID, "", R.drawable.home);
		menu.add(0, CONFIG_ID, "Config", R.drawable.configure);
		menu.add(0, HELP_ID, "Help", R.drawable.help);
		menu.add(0, CLOSE_ID, "Close", R.drawable.eject)
				.setAlphabeticShortcut('c');
				
		if (flipper.getDisplayedChild()==0) {
			showList();
		}
		else {
			showHome();
		}

		return(super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item) {
		switch (item.getId()) {
			case FLIP_ID:
				if (flipMenu.getTitle().equals("Go Home")) {
					showHome();
				}
				else {
					showList();
				}
				
				return(true);
		
			case CONFIG_ID:
				startActivity(new Intent(this, ConfigActivity.class));
				return(true);
		
			case HELP_ID:
				startActivity(new Intent(this, HelpActivity.class));
				return(true);
		
			case CLOSE_ID:
				finish();
				return(true);
		}

		return(super.onOptionsItemSelected(item));
	}
	
	public void onItemClick(AdapterView l, View v, int position, long id) {
		Cursor c=(Cursor)list.getAdapter().getItem(position);
		Uri tourUri=Uri.withAppendedPath(Provider.Tours.CONTENT_URI, c.getString(0));
		
		startActivity(new Intent(Intent.VIEW_ACTION, tourUri));
	}
	
	private void showHome() {
		flipper.setDisplayedChild(1);
		setTitle("TourIt! - Home");
		
		if (flipMenu!=null) {
			flipMenu.setTitle("Show Tours");
			flipMenu.setIcon(R.drawable.list);
		}
	}
	
	private void showList() {
		flipper.setDisplayedChild(0);
		setTitle("TourIt! - Tours");
		
		if (flipMenu!=null) {
			flipMenu.setTitle("Go Home");
			flipMenu.setIcon(R.drawable.home);
		}
	}
}