package it.unipr.dsg.mobdev;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class BookmarkListActivity extends Activity{


	private ListView listView = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		Log.d(MainActivity.TAG,"BookmarkListActivity onCreate()");
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.default_list_view);
		
		listView  = (ListView)findViewById(R.id.listView);
		listView.setTextFilterEnabled(true);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				Log.d(MainActivity.TAG,"BookmarkListActivity-->listView.setOnItemClickListener: " + position);
				
				String url = BookmarkManager.getInstance().getBookmarkList().get(position).getUrl();
				
				if(url != null)
				{
					Intent i = new Intent(Intent.ACTION_VIEW);
					i.setData(Uri.parse(url));
					startActivity(i);
				}
			}

		});
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
				Log.d(MainActivity.TAG,"BookmarkListActivity-->listView.setOnItemLongClickListener: " + position);
				
				//Start EditBookmarkActivity
				Bundle bundle = new Bundle();
				bundle.putInt("bookmarkPosition", position);

				Intent newIntent = new Intent(getApplicationContext(), EditBookmarkActivity.class);
				newIntent.putExtras(bundle);
				startActivity(newIntent);
				
				return false;
			}
			
		});
		
		updateListView();
		
	}
	
	private void updateListView()
	{
		Log.d(MainActivity.TAG, "Updating List View ...");
		
		BookmarkListAdapter bookmarkListAdapter = new BookmarkListAdapter(this, R.layout.custom_list_element, BookmarkManager.getInstance().getBookmarkList());
		listView.setAdapter(bookmarkListAdapter);
		//listView.refreshDrawableState();
	}
	
	@Override
	protected void onDestroy() {
		Log.d(MainActivity.TAG, "BookmarkListActivity onDestroy()");
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Log.d(MainActivity.TAG, "BookmarkListActivity onPause()");
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Log.d(MainActivity.TAG, "BookmarkListActivity onRestart()");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.d(MainActivity.TAG, "BookmarkListActivity onResume()");
		super.onResume();
		this.updateListView();
	}

	@Override
	protected void onStart() {
		Log.d(MainActivity.TAG, "BookmarkListActivity onStart()");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.d(MainActivity.TAG, "BookmarkListActivity onStop()");
		super.onStop();
	}
}
