package fishrungames.bashgid;

import java.util.Collections;

import java.util.Set;
import java.util.TreeSet;

import fishrungames.bashgid.core.ArticleManager;
import fishrungames.bashgid.core.ArticleManager.ArticleShortData;
import fishrungames.bashgid.core.DatabaseGetJournalTask;
import fishrungames.bashgid.core.JournalManager;
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.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.AdapterView;

import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class NewsListFragment extends Fragment
{

	ListView listView;

	ListAdapter listAdapter;

	JournalSemiFullData journal;

	ProgressBar progressBar;

	TextView footerTextView;

	DatabaseGetJournalTask databaseGetJournalTask = null;

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

		RestoreJournal();

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

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

		View header = inflater.inflate(R.layout.news_page_list_header, null);

		listView.addHeaderView(header);

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

		progressBar.setVisibility(View.VISIBLE);

		View footer = inflater.inflate(R.layout.news_page_list_footer, null);

		listView.addFooterView(footer);

		footerTextView = (TextView) footer.findViewById(R.id.footerTextView);

		listAdapter = new ListAdapter(getActivity());

		listView.setAdapter(listAdapter);

		listView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				// Add position + 1, because first element of list is always
				// title
				if (position == journal.articleArr.size() + 1)
				{
					footerTextView.setText(R.string.news_are_loading);
					LoadMoreNews();
				} else if (position > 0)
				{
					MainActivity.getInstance().OpenArticleScreen(journal.articleArr.get(position - 1).name, MainActivity.TAG_BACK_TO_NEWSLISTFRAGMENT);
				}

			}

		});

		return rootView;
	}

	@Override
	public void onDestroyView()
	{

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

		super.onDestroyView();
	}

	public void RefreshAdapter()
	{
		listAdapter.notifyDataSetChanged();
	}

	void RestoreJournal()
	{
		journal = JournalManager.CreateEmptyJournal();

		RefreshJournal();
	}

	void RefreshJournal()
	{
		databaseGetJournalTask = new DatabaseGetJournalTask();

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

		Bundle bundle = new Bundle();

		bundle.putString("journalName", "news");

		bundle.putInt("count", 20);

		databaseGetJournalTask.execute(bundle);
	}

	void LoadMoreNews()
	{
		if (databaseGetJournalTask != null)
		{
			databaseGetJournalTask.cancel(false);
		}

		databaseGetJournalTask = new DatabaseGetJournalTask();

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

		Bundle bundle = new Bundle();

		bundle.putString("journalName", "news");

		bundle.putString("startAfterArticleName", journal.articleArr.get(journal.articleArr.size() - 1).name);

		bundle.putInt("count", 10);

		databaseGetJournalTask.execute(bundle);
	}

	public void SetJournal(JournalSemiFullData journal)
	{
		progressBar.setVisibility(View.GONE);

		this.journal.articleArr.addAll(journal.articleArr);

		Set<ArticleShortData> articleSet = new TreeSet<ArticleShortData>(new ArticleManager.ArticleShortDataSortComparator());
		articleSet.addAll(this.journal.articleArr);
		this.journal.articleArr.clear();
		this.journal.articleArr.addAll(articleSet);

		Collections.sort(this.journal.articleArr, new ArticleManager.ArticleShortDataTimeSortComparator());

		footerTextView.setText(R.string.news_load_more);

		listAdapter.notifyDataSetChanged();

	}

	public class ListAdapter extends BaseAdapter
	{
		private Context mContext;

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

		public int getCount()
		{
			return journal.articleArr.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(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.news_page_list_element, null);

			} else
			{
				view = convertView;
			}

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

			titleTextView.setText(journal.articleArr.get(position).title);

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

			dateTextView.setText(journal.articleArr.get(position).dateTime.toString());

			return view;
		}

	}

}
