package it.unipr.mobdev;

import it.unipr.dsg.mobdev.R;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class BookmarkListFragment extends Fragment{


	private ListView listView = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(MainActivity.TAG, "BookmarkListFragment --> onCreate()");
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onDestroy() {
		Log.d(MainActivity.TAG, "BookmarkListFragment --> onDestroy()");
		super.onDestroy();
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)  {
		
		Log.d(MainActivity.TAG,"BookmarkListFragment --> onCreateView()");
		
		View view = inflater.inflate(R.layout.default_list_view,container, false);
		
		listView  = (ListView)view.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,"BookmarkListFragment --> listView.setOnItemClickListener: " + position);
				
				String url = BookmarkManager.getInstance(getActivity()).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(getActivity(), AddEditBookmarkActivity.class);
				newIntent.putExtras(bundle);
				startActivity(newIntent);
				
				return false;
			}
			
		});
		
		updateListView();
		
		return view;
	}
	
	
	
	@Override
	public void onResume() {
		super.onResume();
		Log.d(MainActivity.TAG,"BookmarkListActivity --> onResume()");		
		updateListView();
	}

	@Override
	public void onViewStateRestored(Bundle savedInstanceState) {
		super.onViewStateRestored(savedInstanceState);	
		Log.d(MainActivity.TAG,"BookmarkListActivity --> onViewStateRestored()");
	}

	private void updateListView()
	{
		Log.d(MainActivity.TAG, "BookmarkListFragment --> Updating List View ...");
		
		BookmarkListAdapter bookmarkListAdapter = new BookmarkListAdapter(getActivity(), R.layout.custom_list_element, BookmarkManager.getInstance(getActivity()).getBookmarkList());
		listView.setAdapter(bookmarkListAdapter);
		//listView.refreshDrawableState();
	}
	
}
