package com.view;

import viewbg.EventItem;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;

public class UserMainPage extends Activity {
	Button bt_search;
	Button bt_add;
	Button bt_profile;
	Button bt_interact;
//	Button bt_logout;
	Button bt_help;
	
	TextView tv_userName;
	TextView tv_useraddr;
	
	static final int SEARCH_MENU_ITEM=Menu.FIRST;
	static final int ADD_MENU_ITEM=Menu.FIRST+1;
	static final int MY_MENU_ITEM=Menu.FIRST+2;
	static final int PROFILE_MENU_ITEM=Menu.FIRST+3;
	static final int MAIL_MENU_ITEM=Menu.FIRST+4;
	static final int LOGOUT_MENU_ITEM=Menu.FIRST+5;
	
	ScrolledEventsView sev;
	EventsCoverFlow ecf;
	
	static final int RESUEST_SEARCH=0;
	static final int REQUEST_ADD=1;
	static final int REQUEST_PROFILE=2;
	static final int REQUEST_HELP=3;
	static final int REQUEST_MAIL=4;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		
		
		if(this.getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
		{
			this.requestWindowFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.usermainlandscape);
			tv_userName=(TextView)findViewById(R.id.username_in_main);
			tv_useraddr=(TextView)findViewById(R.id.user_addr_in_main);
			ecf=new EventsCoverFlow(this);
			this.addContentView(ecf, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		}
		
		else
		{
			setContentView(R.layout.usermain);
			bt_search=(Button)findViewById(R.id.search_in_main);
			bt_add=(Button)findViewById(R.id.add_in_main);
			bt_profile=(Button)findViewById(R.id.profile_in_main);
			bt_interact=(Button)findViewById(R.id.interact_in_main);
//			bt_logout=(Button)findViewById(R.id.logout_in_main);
			bt_help=(Button)findViewById(R.id.help_in_main);
			this.getWindow().setBackgroundDrawableResource(R.drawable.bg4);
			
			Bundle b=getIntent().getExtras();
			//add username
			tv_userName=(TextView)findViewById(R.id.username_in_main);
			tv_useraddr=(TextView)findViewById(R.id.user_addr_in_main);
			/*
			if(b!=null)
				tv_userName.setText(b.getCharSequence("Name"));
			else{
				//tv_userName.setText(PreferencesUtil.userName);
				SharedPreferences sharedata = getSharedPreferences(PreferencesUtil.preferencesDouban, 0);  

				String data = sharedata.getString(PreferencesUtil.myName, null);  

				tv_userName.setText(data);
				tv_useraddr.setText(sharedata.getString("Address", null));
				
			}
			*/
			bt_search.setOnClickListener(click_search);
			//
			bt_add.setOnClickListener(click_add);
			bt_profile.setOnClickListener(click_profile);
			bt_help.setOnClickListener(click_help);
//			bt_logout.setOnClickListener(click_logout);
			/*
			EventItem[] items=new EventItem[]{new EventItem("A", "A", "A", this.getResources().getDrawable(R.drawable.logo)),
					new EventItem("B", "B", "B", this.getResources().getDrawable(R.drawable.logo)),
					new EventItem("C", "C", "C", this.getResources().getDrawable(R.drawable.logo)),
					new EventItem("D", "D", "D", this.getResources().getDrawable(R.drawable.logo))};
			*/
			
			bt_search.setVisibility(View.INVISIBLE);
			bt_search.setEnabled(false);
			bt_add.setVisibility(View.INVISIBLE);
			bt_add.setEnabled(false);
			bt_profile.setVisibility(View.INVISIBLE);
			bt_profile.setEnabled(false);
			bt_interact.setVisibility(View.INVISIBLE);
			bt_interact.setEnabled(false);
			bt_help.setVisibility(View.INVISIBLE);
			bt_help.setEnabled(false);
			sev=new ScrolledEventsView(this);
//			this.getWindow().
			this.addContentView(sev, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
			//this.setRequestedOrientation(ActivityIn)
		}
		
		
	}
	
	
	OnClickListener click_search=new OnClickListener()
	{
		public void onClick(View view)
		{
			Intent intent=new Intent(UserMainPage.this,TabbedSearch.class);
			startActivityForResult(intent,RESUEST_SEARCH);
		}
	};
	
	OnClickListener click_add=new OnClickListener()
	{
		public void onClick(View view)
		{
			Intent intent=new Intent(UserMainPage.this,AddEvent.class);
			startActivityForResult(intent,REQUEST_ADD);
		}
	};
	
	OnClickListener click_profile=new OnClickListener()
	{
		public void onClick(View view)
		{
			Intent intent=new Intent(UserMainPage.this,UserProfile.class);
			startActivityForResult(intent,REQUEST_PROFILE);
		}
	};
	
	OnClickListener click_help=new OnClickListener()
	{
		public void onClick(View view)
		{
			Intent intent=new Intent(UserMainPage.this,AboutAndHelp.class);
			startActivityForResult(intent,REQUEST_HELP);
		}
	};
	
	protected void onActivityResult(int requestCode,int resultCode,Intent data)
	{
		if(requestCode==REQUEST_PROFILE)
		{
			
		}
	}
	
	OnClickListener click_logout=new OnClickListener()
	{
		public void onClick(View view)
		{
			/*
			SharedPreferences settings=getSharedPreferences(PreferencesUtil.preferencesDouban, 0);
			settings.edit().putString(PreferencesUtil.acessToken, "false")
			               .putString(PreferencesUtil.acessTokenSecret,"false")
			               .commit();*/
			Intent intent=new Intent(UserMainPage.this,WelcomePage.class);
			startActivity(intent);
			}
		
		
	};
	
	public boolean onCreateOptionsMenu(Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, SEARCH_MENU_ITEM, Menu.NONE, "Search");
		menu.add(Menu.NONE, ADD_MENU_ITEM, Menu.NONE, "Add");
		menu.add(Menu.NONE, MY_MENU_ITEM, Menu.NONE, "My E");
		menu.add(Menu.NONE, PROFILE_MENU_ITEM, Menu.NONE, "Profile");
		menu.add(Menu.NONE, MAIL_MENU_ITEM, Menu.NONE, "Mails");
		menu.add(Menu.NONE, LOGOUT_MENU_ITEM, Menu.NONE, "Log out");
		
		return true;
	}
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
		case SEARCH_MENU_ITEM:
		{
			bt_search.performClick();
			return true;
		}
		case ADD_MENU_ITEM:
		{
			bt_add.performClick();
			return true;
		}
		case MY_MENU_ITEM:
		{
			//bt_add.performClick();
			return true;
		}
		case PROFILE_MENU_ITEM:
		{
			bt_profile.performClick();
			return true;
		}
		case MAIL_MENU_ITEM:
		{
			//
			return true;
		}
		case LOGOUT_MENU_ITEM:
		{
			//bt_add.performClick();
			return true;
		}
		}
		return super.onOptionsItemSelected(item);
	}

}
