package fishrungames.bashgid;

import java.util.ArrayList;

import fishrungames.bashgid.core.DatabaseGetJournalListTask;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import it.sephiroth.android.library.widget.HListView;

public class MainPageFragment extends Fragment
{

	ArrayList<JournalSemiFullData> journalList = new ArrayList<JournalSemiFullData>();

	ListView listView;
	ListAdapter listAdapter;
	View header;
	ProgressBar progressBar;
	EditText searchEditText;

	JournalSemiFullData newsJournal;

	DatabaseGetJournalTask databaseGetJournalTask = null;
	DatabaseGetJournalListTask databaseGetJournalListTask = null;

	public MainPageFragment()
	{
	}

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

		journalList.clear();

		RestoreJournalList();

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

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

		listAdapter = new ListAdapter(getActivity());

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

		RestoreJournal();
		MainActivity.getInstance().StartDownloadNewsTask();

		listView.addHeaderView(header);

		listView.setAdapter(listAdapter);

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

		progressBar.setVisibility(View.VISIBLE);

		Button moreNewsButton = (Button) header.findViewById(R.id.moreNewsButton);

		moreNewsButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				MainActivity.getInstance().OpenNewsScreen();
			}
		});

		searchEditText = (EditText) header.findViewById(R.id.searchEditText);

		Button searchButton = (Button) header.findViewById(R.id.searchButton);

		searchButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				String query = searchEditText.getText().toString();
				MainActivity.getInstance().OpenSearchScreen(query);
			}
		});

		return rootView;
	}

	@Override
	public void onDestroyView()
	{
		if (databaseGetJournalTask != null)
		{
			databaseGetJournalTask.cancel(false);
			databaseGetJournalTask = null;
		}

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

		super.onDestroyView();
	}

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

		RefreshJournal();
	}

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

		databaseGetJournalTask = new DatabaseGetJournalTask();

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

		Bundle bundle = new Bundle();

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

		bundle.putInt("count", 3);

		databaseGetJournalTask.execute(bundle);
	}

	public void SetJournal(JournalSemiFullData journal)
	{
		databaseGetJournalTask = null;

		// Xperimental
		if (journal != null)
		{
			progressBar.setVisibility(View.GONE);

			newsJournal = journal;

			Button news1Button = (Button) header.findViewById(R.id.news1Button);
			Button news2Button = (Button) header.findViewById(R.id.news2Button);
			Button news3Button = (Button) header.findViewById(R.id.news3Button);

			if (newsJournal.articleArr.size() > 0)
			{

				news1Button.setText(newsJournal.articleArr.get(0).title);

				news1Button.setOnClickListener(new NewsButtonOnClickListener(newsJournal.articleArr.get(0).name));
			} else
			{

				news1Button.setText(R.string.news_record_not_available);

				news1Button.setOnClickListener(null);
			}

			if (newsJournal.articleArr.size() > 1)
			{

				news2Button.setText(newsJournal.articleArr.get(1).title);

				news2Button.setOnClickListener(new NewsButtonOnClickListener(newsJournal.articleArr.get(1).name));
			} else
			{

				news2Button.setText(R.string.news_record_not_available);
				news2Button.setOnClickListener(null);
			}

			if (newsJournal.articleArr.size() > 2)
			{

				news3Button.setText(newsJournal.articleArr.get(2).title);
				news3Button.setOnClickListener(new NewsButtonOnClickListener(newsJournal.articleArr.get(2).name));
			} else
			{
	
				news3Button.setText(R.string.news_record_not_available);
				news3Button.setOnClickListener(null);
			}

			listAdapter.notifyDataSetChanged();

		}

	}

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

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

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

		databaseGetJournalListTask = new DatabaseGetJournalListTask();

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

		databaseGetJournalListTask.execute("featured");
	}

	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)
				{
					if (databaseGetJournalTask != null)
					{
						databaseGetJournalTask.cancel(false);
					}
					MainActivity.getInstance().OpenJournalScreen(journalList.get(position).name, MainActivity.TAG_BACK_TO_MAINFRAGMENT);
				}
			});

			imageButton.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					if (databaseGetJournalTask != null)
					{
						databaseGetJournalTask.cancel(false);
					}
					MainActivity.getInstance().OpenJournalScreen(journalList.get(position).name, MainActivity.TAG_BACK_TO_MAINFRAGMENT);
				}
			});

			return view;
		}

	}

	public class NewsButtonOnClickListener implements OnClickListener
	{

		String articleName;

		public NewsButtonOnClickListener(String articleName)
		{
			this.articleName = articleName;
		}

		@Override
		public void onClick(View v)
		{
			if (databaseGetJournalTask != null)
			{
				databaseGetJournalTask.cancel(false);
				databaseGetJournalTask = null;
			}
			
			MainActivity.getInstance().OpenArticleScreen(articleName, MainActivity.TAG_BACK_TO_MAINFRAGMENT);

		}

	}

}
