package fishrungames.bashgid;



import fishrungames.bashgid.core.ChannelManager;
import fishrungames.bashgid.core.ChannelManager.ChannelFullData;

import fishrungames.networkutils.ImageManager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;

import android.widget.TextView;


public class VideoListFragment extends Fragment
{

	ListAdapter listAdapter;

	GridView gridView;
	ProgressBar progressBar;

	String channelName;
	ChannelFullData channelFullData;
	
	public VideoListFragment()
	{
		channelName = "";
		channelFullData = null;
	}

	public VideoListFragment(String channelName)
	{
		this.channelName = channelName;
		RestoreChannel();
	}
	
	public VideoListFragment(ChannelFullData channelFullData)
	{
		this.channelName = channelFullData.name;
		this.channelFullData = channelFullData;
	}
	
	@Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("channelName", channelName);
    }

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

		
		if (savedInstanceState != null) {
			channelName = savedInstanceState.getString("channelName");
			RestoreChannel();
        }

		View rootView = inflater.inflate(R.layout.fragment_video_page, container, false);
		
		progressBar = (ProgressBar)rootView.findViewById(R.id.progressBar);
		
		progressBar.setVisibility(View.VISIBLE);


		gridView = (GridView) rootView.findViewById(R.id.gridView);

		listAdapter = new ListAdapter(getActivity());

		gridView.setAdapter(listAdapter);

		gridView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				
				MainActivity.getInstance().SetLastChannelName(channelName);
				
				MainActivity.getInstance().OpenVideoRecordScreen(channelFullData.videoRecordArr.get(position));
			}

		});

		return rootView;
	}
	
	void RestoreChannel()
	{
		channelFullData = ChannelManager.CreateEmptyChannel();
		
		DatabaseGetChannelTask task = new DatabaseGetChannelTask();

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

		task.execute(channelName);
	}
	
	void SetChannel(ChannelFullData channelFullData)
	{
		progressBar.setVisibility(View.GONE);

		this.channelFullData = channelFullData;
		listAdapter.notifyDataSetChanged();
	}
	
	
	
	public class DatabaseGetChannelTask extends AsyncTask<String, Integer, Long>
	{

		ChannelFullData channel = null;
		
		public Handler mHandler;

		protected Long doInBackground(String... queryArr)
		{
			String channelName = queryArr[0];
			
			channel = MainActivity.getInstance().channelDataSource.GetChannelFullData(channelName);
			
			return (long) 0;
		}

		protected void onProgressUpdate(Integer... progress)
		{
			Message completeMessage = mHandler.obtainMessage(MainActivity.DATABASE_GET_CHANNEL_STATE_UPDATE, channel);

			completeMessage.sendToTarget();

		}

		protected void onPostExecute(Long result)
		{
			Message completeMessage = mHandler.obtainMessage(MainActivity.DATABASE_GET_CHANNEL_STATE_FINISHED, channel);

			completeMessage.sendToTarget();
		}
	}

	
	public class ListAdapter extends BaseAdapter {
		private Context mContext;

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

		public int getCount() {
			return channelFullData.videoRecordArr.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.video_page_list_element, null);


			} else {
				view = convertView;
			}

			ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
			TextView nameTextView = (TextView) view.findViewById(R.id.nameTextView);

			ImageManager.getInstance().ApplyImageToImageView(imageView, channelFullData.videoRecordArr.get(position).previewImageUrl);

			nameTextView.setText(channelFullData.videoRecordArr.get(position).title);

			return view;
		}

	}
	
	
}
