package fishrungames.bashgid;


import fishrungames.bashgid.core.VideoManager;

import fishrungames.bashgid.core.VideoManager.VideoRecordData;
import fishrungames.networkutils.ImageManager;

import android.content.Intent;
import android.net.Uri;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class VideoRecordFragment extends Fragment
{

	String videoUrl;

	VideoRecordData recordData;

	TextView nameTextView;
	TextView descriptionTextView;
	ImageView imageView;
	ProgressBar progressBar;

	public VideoRecordFragment()
	{
		this.videoUrl = "";
		this.recordData = null;
	}

	public VideoRecordFragment(String videoUrl)
	{
		this.recordData = null;
		this.videoUrl = videoUrl;
		RestoreVideo();
	}

	public VideoRecordFragment(VideoRecordData recordData)
	{
		this.videoUrl = recordData.videoUrl;
		this.recordData = recordData;
	}

	@Override
	public void onSaveInstanceState(final Bundle outState)
	{
		super.onSaveInstanceState(outState);
		outState.putString("videoUrl", videoUrl);
	}

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

		if (savedInstanceState != null)
		{
			videoUrl = savedInstanceState.getString("videoUrl");
			RestoreVideo();
		}

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

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

		if (recordData == null)
		{
			progressBar.setVisibility(View.VISIBLE);

		} else
		{
			progressBar.setVisibility(View.GONE);
		}

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

		RefreshView();

		return rootView;
	}

	void RestoreVideo()
	{
		// recordData =
		// MainActivity.getInstance().videoDataSource.GetVideoByVideoUrl(videoUrl);
		recordData = VideoManager.CreateEmptyVideo();

		DatabaseGetVideoTask task = new DatabaseGetVideoTask();

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

		task.execute(videoUrl);
	}

	void SetVideo(VideoRecordData recordData)
	{
		progressBar.setVisibility(View.GONE);
		this.recordData = recordData;
		RefreshView();
	}

	void RefreshView()
	{
		nameTextView.setText(recordData.title);

		descriptionTextView.setText(recordData.description);

		ImageManager.getInstance().ApplyImageToImageView(imageView, recordData.previewImageUrl);

		imageView.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				if (recordData.type == VideoManager.VIDEO_TYPE_YOUTUBE)
				{
					MainActivity.getInstance().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl)));
				}
			}

		});
	}

	public static class DatabaseGetVideoTask extends AsyncTask<String, Integer, Long>
	{

		VideoRecordData recordData = null;

		public Handler mHandler;

		protected Long doInBackground(String... queryArr)
		{
			String videoUrl = queryArr[0];

			recordData = MainActivity.getInstance().videoDataSource.GetVideoByVideoUrl(videoUrl);

			return (long) 0;
		}

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

			completeMessage.sendToTarget();

		}

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

			completeMessage.sendToTarget();
		}
	}

}
