package se.hv.dindag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * Start-Up activity giving the user a Login-button plus a feed of News
 * 
 * @author imcoh
 */
public class MyDay extends ListActivity {
	/**
	 * Define the keys in the XML-feed we're interested in
	 */
	static final String KEY_ITEM = "item"; // parent node
	static final String KEY_LINK = "link";
	static final String KEY_TITLE = "title";
	static final String KEY_DESC = "description";
	static final String KEY_DATE = "pubDate";

	/*
	 * Fires up the activity_myday and waits for the Login-button to be pressed.
	 * Then it starts the ASynkTask to gather the XML-feed in the background.
	 * When this is done we can populate the ListAdapter with the stuff from the
	 * feed.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent in = getIntent();

		// Den personliga URL innehllande URL samt app_key=HEMLIG_HASH
		String url = in.getExtras().getString("URL");

		// Riktiga namnet p den inloggade anvndaren
		String realName = in.getExtras().getString("REALNAME");

		try {
			// Initiate the ASynkTask
			MyDayHandler myDayNews = new MyDayHandler();

			// Start the task and give it the URL as input
			myDayNews.execute(url);

			// Create a local ArrayList
			ArrayList<HashMap<String, String>> myDayItems = new ArrayList<HashMap<String, String>>();

			// Fill the ArrayList with the items we got from the ASynkTask
			myDayItems = myDayNews.get();

			// Add the menuItems to our ListView
			ListAdapter adapter = new SimpleAdapter(this, myDayItems,
					R.layout.activity_myday_list, new String[] { KEY_TITLE,
							KEY_LINK, KEY_DESC, KEY_DATE }, new int[] {
							R.id.mydayTitle, R.id.mydayLink,
							R.id.mydayDescription, R.id.mydayDate });

			setListAdapter(adapter);

			ListView lv = (ListView) findViewById(R.id.myDayList);

			// Wait for an item in the list to be clicked
			lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// getting values from selected ListItem
					String link = ((TextView) view.findViewById(R.id.mydayLink))
							.getText().toString();

					// Starting new intent
					Intent in = new Intent(getApplicationContext(),
							WebReader.class);

					// Pass the URL to the new Activity
					in.putExtra("KEY_LINK", link);
					startActivity(in);
				}
			});

		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.out
					.println("============= MOTHER OF ALL ERRORS IN MYDAY ================");
			e.printStackTrace();
		}
	}

	/**
	 * Displays the menu for the startup screen
	 */
	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater blowUp = getMenuInflater();
		blowUp.inflate(R.menu.menu_loggedin, menu);
		return true;
	}

	/**
	 * Handles the different kinds of buttons wich can be pressed in the current
	 * Activitys menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuSave:
			AlertDialog.Builder savePopup = new AlertDialog.Builder(this);
			savePopup.setTitle("Spara dina loginuppgifter");
			savePopup
					.setMessage("Hr kan vi implementera en SQLlite-databas fr att spara uppgifterna man matade in.");
			savePopup.setNeutralButton("OK", null);
			savePopup.show();

			break;

		case R.id.menuMail:
			AlertDialog.Builder mailPopup = new AlertDialog.Builder(this);
			mailPopup.setTitle("Kolla mail");
			mailPopup
					.setMessage("ppnar helt enkelt en WebView till studenternas Live-mail sida. Vi behver inte ta med ngon login eller s, bara ppna en WebView.");
			mailPopup.setNeutralButton("OK", null);
			mailPopup.show();

			break;

		}
		return false;
	}
}
