package fishrungames.bashgid;

import java.util.ArrayList;


import fishrungames.bashgid.core.ImageManager;
import fishrungames.bashgid.core.NewsManager;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.NewsUpdatedCallbackInterface;
import fishrungames.bashgid.core.RemoveCallbackInterface;
import fishrungames.bashgid.core.db.NewsDataSource;
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.TextView;


public class NewsListFragment extends Fragment implements NewsUpdatedCallbackInterface
{

	ListView listView;
	
	ListAdapter listAdapter;
	
	ArrayList<NewsRecord> newsRecordArr;
	
	RemoveCallbackInterface downloadNewsCanceller = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_news_page, container, false);
		
		newsRecordArr = NewsManager.GetInstance().getNews();
		
		downloadNewsCanceller = NewsManager.GetInstance().DownloadNews(this);
			
		listView = (ListView) rootView.findViewById(R.id.listView);
		
		listAdapter = new ListAdapter(getActivity());
		
		listView.setAdapter(listAdapter);
		
		View header = inflater.inflate(R.layout.news_page_list_header, null);
		
		listView.addHeaderView(header);
	
		return rootView;
	}
	
	@Override
	public void onDestroyView()
	{
		if (downloadNewsCanceller != null)
		{
			downloadNewsCanceller.RemoveCallback();
			downloadNewsCanceller = null;
		}
		super.onDestroyView();
	}
	
	public void RefreshNewsList()
	{
		newsRecordArr = NewsManager.GetInstance().getNews();
		listAdapter.notifyDataSetChanged();
	}
	
	public class ListAdapter extends BaseAdapter {
	    private Context mContext;

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

	    public int getCount() {
	        return newsRecordArr.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
	    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;
	        }
	        
	        ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);
	        
	        Button button = (Button) view.findViewById(R.id.button);
	        
	        TextView textView = (TextView) view.findViewById(R.id.textView);
	        
	        ImageManager.getInstance().ApplyImageToImageView(imageButton, newsRecordArr.get(position).imageId);
	        
	        button.setText(newsRecordArr.get(position).title);
	        
	        textView.setText(NewsDataSource.iso8601Format.format(newsRecordArr.get(position).pubDate));
	        
	        button.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position)));
	        
	        imageButton.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position)));
	        
	        return view;
	    }

	}
	
	
	@Override
	public void OnNewsUpdated()
	{
		getActivity().runOnUiThread(new Runnable() {

			@Override
			public void run()
			{
				RefreshNewsList();
			}
			
		});
	}
	
	public static class ButtonOnClickListener implements OnClickListener
	{
		
		NewsRecord newsRecord;
		
		public ButtonOnClickListener(NewsRecord newsRecord)
		{
			this.newsRecord = newsRecord;
		}

		@Override
		public void onClick(View v)
		{
			MainActivity.getInstance().OpenNewsRecordScreen(newsRecord, MainActivity.TAG_FROM_NEWSLISTFRAGMENT_TO_NEWSRECORDFRAGMENT);
		}
		
	}
	
}
