package fishrungames.bashgid;

import java.util.ArrayList;

import it.sephiroth.android.library.widget.HListView;
import fishrungames.bashgid.core.DatabaseGetJournalListTask;
import fishrungames.bashgid.core.JournalManager.JournalSemiFullData;
import android.annotation.SuppressLint;
import android.content.Context;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CatalogFragment extends Fragment
{

	ListView listView;
	ListAdapter listAdapter;
	ProgressBar progressBar;

	ArrayList<JournalSemiFullData> journalList;

	DatabaseGetJournalListTask databaseGetJournalListTask = null;

	public CatalogFragment()
	{
		RestoreJournalList();
	}

	public CatalogFragment(ArrayList<JournalSemiFullData> journalList)
	{
		this.journalList = journalList;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{

		View rootView = inflater.inflate(R.layout.fragment_catalog_page, container, false);

		progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);

		progressBar.setVisibility(View.VISIBLE);

		listView = (ListView) rootView.findViewById(R.id.listView);

		listAdapter = new ListAdapter(getActivity());

		listView.setAdapter(listAdapter);

		return rootView;
	}

	@Override
	public void onDestroyView()
	{

		if (databaseGetJournalListTask != null)
		{
			databaseGetJournalListTask.cancel(false);
			databaseGetJournalListTask = null;
		}

		super.onDestroyView();
	}

	public void RestoreJournalList()
	{
		journalList = new ArrayList<JournalSemiFullData>();

		databaseGetJournalListTask = new DatabaseGetJournalListTask();

		databaseGetJournalListTask.mHandler = MainActivity.getInstance().mHandler;

		databaseGetJournalListTask.execute();
	}

	public void SetJournalList(ArrayList<JournalSemiFullData> journalList)
	{
		databaseGetJournalListTask = null;

		progressBar.setVisibility(View.GONE);

		this.journalList = journalList;

		listAdapter.notifyDataSetChanged();
	}

	public class ListAdapter extends BaseAdapter
	{
		private Context mContext;

		public ListAdapter(Context c)
		{
			mContext = c;
		}

		public int getCount()
		{
			return journalList.size();
		}

		public Object getItem(int position)
		{
			return null;
		}

		public long getItemId(int position)
		{
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		@SuppressLint("InflateParams")
		public View getView(final int position, View convertView, ViewGroup parent)
		{

			View view;

			if (convertView == null)
			{ // if it's not recycled, initialize some attributes
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.main_page_list_element, null);

			} else
			{
				view = convertView;
			}

			TextView textView = (TextView) view.findViewById(R.id.dateTextView);

			TextView descriptionTextView = (TextView) view.findViewById(R.id.descriptionTextView);

			Button button = (Button) view.findViewById(R.id.button);
			ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);

			HListView hListView = (HListView) view.findViewById(R.id.hListView);

			hListView.setAdapter(new MainPageHListViewAdapters.JournalElementAdapter(getActivity(), journalList.get(position)));

			textView.setText(journalList.get(position).title);

			if (journalList.get(position).description.length() > 0)
			{
				descriptionTextView.setText(journalList.get(position).description);

				descriptionTextView.setVisibility(View.VISIBLE);
			} else
			{
				descriptionTextView.setVisibility(View.GONE);
			}

			button.setText(R.string.see_all);

			button.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					MainActivity.getInstance().OpenJournalScreen(journalList.get(position).name, MainActivity.TAG_BACK_TO_CATALOGFRAGMENT);
				}

			});

			imageButton.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					MainActivity.getInstance().OpenJournalScreen(journalList.get(position).name, MainActivity.TAG_BACK_TO_CATALOGFRAGMENT);
				}

			});
			return view;
		}

	}

}