package com.view;

import java.util.ArrayList;
import java.util.List;

import basicClass.EventVideo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class VideoList extends Activity {
	private static final int REQ_VIDEO_LIST_UPDATE = 0;
	Button[] sees=new Button[5];
	Button bt_prev,bt_next;
	Button bt_close;
	List<String> lNames=new ArrayList<String>();
	List<String> lPublishers=new ArrayList<String>();
	
	List<EventVideo> lVideos=new ArrayList<EventVideo>();
	
	int current_page=0;
	int total_page=1;
	
	public static void requestUpdate(Activity parent)
	{
		Intent login=new Intent(parent,VideoList.class);
		parent.startActivityForResult(login, REQ_VIDEO_LIST_UPDATE);
	}
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.videolist);
		
		bt_close=(Button)findViewById(R.id.video_list_close);
		bt_prev=(Button)findViewById(R.id.video_list_prev);
		bt_next=(Button)findViewById(R.id.video_list_next);
		
		sees[0]=(Button)findViewById(R.id.video_see_1);
		sees[1]=(Button)findViewById(R.id.video_see_2);
		sees[2]=(Button)findViewById(R.id.video_see_3);
		sees[3]=(Button)findViewById(R.id.video_see_4);
		sees[4]=(Button)findViewById(R.id.video_see_5);
		for(int i=0;i<5;i++)
			sees[i].setEnabled(false);
		bt_prev.setEnabled(false);
		bt_next.setEnabled(false);
		
		setVideoSeq(current_page);
		
		bt_close.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				setResult(Activity.RESULT_CANCELED);
				finish();
			}
		});
	}
	
	public void setVideos(List<EventVideo> videos)
	{
		for(int i=0;i<videos.size();i++)
			this.lVideos.add(videos.get(i));
		
		if(videos.size()<=5)
		{
			total_page=1;
			current_page=0;
			for(int i=0;i<videos.size();i++)
			{
				((TextView)findViewById(R.id.video_name_1+4*i)).setText(lVideos.get(i).getVideoName());
				((TextView)findViewById(R.id.video_publisher_1+4*i)).setText(lVideos.get(i).getVideoPublisher());
				sees[i].setEnabled(true);
			}
		}
		else
		{
			total_page=videos.size()/5+1;
			current_page=0;
			for(int i=0;i<5;i++)
			{
				((TextView)findViewById(R.id.video_name_1+4*i)).setText(lVideos.get(i).getVideoName());
				((TextView)findViewById(R.id.video_publisher_1+4*i)).setText(lVideos.get(i).getVideoPublisher());
				sees[i].setEnabled(true);
			}
			bt_next.setEnabled(true);
		}
	}
	
	public void setVideoSeq(int page)
	{
		for(int i=0;i<5;i++)
		{
			((TextView)findViewById(R.id.video_no_1+4*i)).setText(""+(page*5+i+1));
		}
		
	}
	
	OnClickListener click_next=new OnClickListener()
	{
		public void onClick(View view)
		{
			current_page++;
			if(current_page>=total_page-1) bt_next.setEnabled(false);
			if(current_page!=0) bt_prev.setEnabled(true);
			setVideoSeq(current_page);
			
			if(current_page==total_page-1)
			{
				for(int i=0;i<lVideos.size()%5;i++)
				{
					((TextView)findViewById(R.id.video_name_1+4*i)).setText(lVideos.get(i+current_page*5).getVideoName());
					((TextView)findViewById(R.id.video_publisher_1+4*i)).setText(lVideos.get(i+current_page*5).getVideoPublisher());
					sees[i].setEnabled(true);
				}
				for(int i=lVideos.size()%5;i<5;i++)
					sees[i].setEnabled(false);
			}
			else
			{
				for(int i=0;i<5;i++)
				{
					((TextView)findViewById(R.id.video_name_1+4*i)).setText(lVideos.get(i+current_page*5).getVideoName());
					((TextView)findViewById(R.id.video_publisher_1+4*i)).setText(lVideos.get(i+current_page*5).getVideoPublisher());
					sees[i].setEnabled(true);
				}
			}
		}
	};
	
	OnClickListener click_prev=new OnClickListener()
	{
		public void onClick(View view)
		{
			current_page--;
			if(current_page==0) bt_prev.setEnabled(false);
			if(current_page<total_page-1) bt_next.setEnabled(true);
			setVideoSeq(current_page);
			
			for(int i=0;i<5;i++)
			{
				((TextView)findViewById(R.id.video_name_1+4*i)).setText(lVideos.get(i+current_page*5).getVideoName());
				((TextView)findViewById(R.id.video_publisher_1+4*i)).setText(lVideos.get(i+current_page*5).getVideoPublisher());
				sees[i].setEnabled(true);
			}
		}
	};

}
