package fishrungames.bashgid;


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.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;


import fishrungames.bashgid.core.DatabaseGetJournalTask;
import fishrungames.bashgid.core.JournalManager;
import fishrungames.bashgid.core.JournalManager.JournalSemiFullData;
import fishrungames.networkutils.ImageManager;

public class JournalFragment extends Fragment
{
	
	JournalSemiFullData journal;
	String journalName;
	
	ListAdapter listAdapter;
	ProgressBar progressBar;
	TextView headerTextView;
	
	DatabaseGetJournalTask databaseGetJournalTask = null;
	
	public JournalFragment()
	{
		journal = null;
		journalName = "";
	}
	
	public JournalFragment(String journalName)
	{
		this.journalName = journalName;
		RestoreJournal();
	}
	
	public JournalFragment(JournalSemiFullData journal)
	{
		this.journalName = journal.name;
		this.journal = journal;
	}
	
	@Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("journalName", journalName);
    }
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		if (savedInstanceState != null) {
			journalName = savedInstanceState.getString("journalName");
			RestoreJournal();
        }
		
		View rootView = inflater.inflate(R.layout.fragment_photo_album_page, container, false);
		
		progressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
		
		progressBar.setVisibility(View.VISIBLE);

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

		View header = inflater.inflate(R.layout.journal_page_header, null);
		
		
		headerTextView = (TextView)header.findViewById(R.id.headerTextView);
		
		listView.addHeaderView(header);
		
		
		listAdapter = new ListAdapter(getActivity());
		
		
		listView.setAdapter(listAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				MainActivity.getInstance().SetLastJournalName(journalName);
				
				if (position == 0)
				{
					if (journal.geoCategory == 1)
					{
						MainActivity.getInstance().OpenMapScreen(journalName, MainActivity.TAG_BACK_TO_JOURNALFRAGMENT);
					}
				}
				else
				{
				
					MainActivity.getInstance().OpenArticleScreen(journal.articleArr.get(position-1).name, MainActivity.TAG_BACK_TO_JOURNALFRAGMENT);
				}
			}
		});
		
		return rootView;
	}
	
	@Override
	public void onDestroyView ()
	{
	
		if (databaseGetJournalTask != null)
		{
			databaseGetJournalTask.cancel(false);
			databaseGetJournalTask = null;
		}
		
		super.onDestroyView();
	}
	

	void RestoreJournal()
	{
		if (databaseGetJournalTask != null)
		{
			databaseGetJournalTask.cancel(false);
			databaseGetJournalTask = null;
		}
		
		journal = JournalManager.CreateEmptyJournal();
		
		databaseGetJournalTask = new DatabaseGetJournalTask();

		databaseGetJournalTask.mHandler = MainActivity.getInstance().mHandler;
		
		Bundle bundle = new Bundle();
		
		bundle.putString("journalName", journalName);

		databaseGetJournalTask.execute(bundle);
	}
	
	public void SetJournal(JournalSemiFullData journal)
	{
		databaseGetJournalTask = null;
		
		progressBar.setVisibility(View.GONE);


		this.journal = journal;
		
		if (journal.geoCategory == 1)
		{
			headerTextView.setText(R.string.journal_open_map);
			
		}
		else
		{
			headerTextView.setText(R.string.category_is_loaded);
		}
		
		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.journal_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());
	        	
	        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
	        
	        if (journal.articleArr.get(position).previewImageUrl != null)
	        {
	        	ImageManager.getInstance().ApplyImageToImageView(imageView, journal.articleArr.get(position).previewImageUrl);
	        }
	        else
	        {
	        	imageView.setImageResource(R.drawable.transparent);
	        }
	        return view;
	    }

	}
	
	
}