package com.futurice.android.reservator;


import java.util.List;

import com.futurice.android.reservator.model.ReservatorException;
import com.futurice.android.reservator.model.Room;
import com.futurice.android.reservator.model.rooms.RoomsInfo;
import com.futurice.android.reservator.view.WeekView;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;

public class RoomInfo extends Activity implements OnItemSelectedListener, OnMenuItemClickListener{
	/** Called when the activity is first created. */
	public static final String ROOM_EMAIL_EXTRA = "roomEmail";

	WeekView weekView;
	List<Room> rooms;
	ArrayAdapter<Room> roomAdapter;
	TextView roomNameLabel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.room_browse_view);
		this.weekView = (WeekView)findViewById(R.id.weekView1);
		this.roomNameLabel = (TextView)findViewById(R.id.roomNameLabel);
	}
	@Override
	public void onResume(){
		super.onResume();
		try {
			rooms = ((ReservatorApplication)getApplication()).getDataProxy().getRooms();

			SharedPreferences settings = getSharedPreferences(getString(R.string.PREFERENCES_NAME), 0);
			String roomEmail = getIntent().getStringExtra(ROOM_EMAIL_EXTRA);
			if(roomEmail == null){
				roomEmail = settings.getString(getString(R.string.PREFERENCES_SHOW_ROOM), "");
			}
			if(roomEmail != null){
				for(Room r : rooms){
					if(r.getEmail().equals(roomEmail)){
						setRoom(r);
						break;
					}
				}
			}
			else if(!rooms.isEmpty()){
				setRoom(rooms.get(0));
			}
		} catch (ReservatorException e) {
			// TODO: XXX
			Log.e("DataProxy", "getRooms", e);
		}
	}
	private void setRoom(Room r){
		roomNameLabel.setText(RoomsInfo.getRoomsInfo(r).getRoomName());
		weekView.setRoom(r);
	}
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		Room selectedRoom = (Room)parent.getItemAtPosition(position);
		weekView.setRoom(selectedRoom);
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    menu.add("Settings").setOnMenuItemClickListener(this);
		return true;
	}

	@Override
	public boolean onMenuItemClick(MenuItem item) {
		Intent i = new Intent(this, SettingsActivity.class);
		startActivity(i);
		return true;
	}
}
@@ -902,60 +902,8 @@
 r%7B%0D%0A
-%09/** Called when the activity is first created. */%0D%0A
 %09pub
@@ -1060,24 +1060,64 @@
 meLabel;%0D%0A%0D%0A
+%09MenuItem settingsMenu, refreshMenu;%0D%0A%0D%0A
 %09@Override%0D%0A
@@ -2662,59 +2662,277 @@
     
-menu.add(%22Settings%22).setOnMenuItemClickListener(thi
+refreshMenu = menu.add(%22Refresh%22).setOnMenuItemClickListener(this);%0D%0A%09    refreshMenu.setIcon(android.R.drawable.ic_popup_sync);%0D%0A%09    settingsMenu = menu.add(%22Settings%22).setOnMenuItemClickListener(this);%0D%0A%09    settingsMenu.setIcon(android.R.drawable.ic_menu_preference
 s);%0D
@@ -3016,16 +3016,48 @@
 tem) %7B%0D%0A
+%09%09if (item == settingsMenu) %7B%0D%0A%09
 %09%09Intent
@@ -3106,16 +3106,17 @@
 ss);%0D%0A%09%09
+%09
 startAct
@@ -3122,24 +3122,94 @@
 tivity(i);%0D%0A
+%09%09%7D else if (item == refreshMenu) %7B%0D%0A%09%09%09weekView.refreshData();%0D%0A%09%09%7D%0D%0A
 %09%09return tru

add refresh button to the roominfo activity