package zall.bean;

import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.fileupload.FileItem;

import zall.transcoder.ZalleryTranscoder;
import zutil.db.DBConnection;
import zutil.db.bean.DBBean;
import zutil.db.bean.DBBeanSQLResultHandler;
import zutil.db.bean.DBBean.*;
import zutil.io.file.FileUtil;

@DBTable(value="Video", superBean = true)
public class Video extends Media{
	public static final String type = "video";
	public static final String VIDEO_FORMAT = "mp4";
	
	/** The progress of a transcoding in %, 0 if there is no transcoding running **/
	protected transient int transcodingProgress;
	/** If the Video is transcoded **/
	protected boolean transcoded;
	/** the time length of the video in seconds **/
	protected long length;
	
	
	@DBLinkTable(table="Comments", beanClass=Comment.class, idColumn="video")
	private LinkedList<Comment> comments;
	
	/**
	 * Loads all the Videos from a folder
	 */
	public static List<Video> loadFolder(DBConnection db, Folder folder) throws SQLException{
		if( folder == null || folder.getId() == null )
			return new LinkedList<Video>();
		PreparedStatement sql = db.getPreparedStatement("SELECT * FROM Video WHERE folder=? ORDER BY date DESC");
		sql.setLong(1, folder.getId() );
		return DBConnection.exec(sql, DBBeanSQLResultHandler.createList(Video.class, db));
	}
	public static List<Video> loadUntransoded(DBConnection db) throws SQLException {
		PreparedStatement sql = db.getPreparedStatement("SELECT * FROM Video WHERE transcoded=0 ORDER BY date DESC");
		return DBConnection.exec(sql, DBBeanSQLResultHandler.createList(Video.class, db));
	}
	public static Video load(DBConnection db, long id) throws SQLException{
		return DBBean.load(db, Video.class, id);
	}
	
	
	public Video(){
		super();
		transcoded = false;
		comments = new LinkedList<Comment>();
	}
	
	public boolean isTranscoded(){
		return transcoded;
	}
	
	public void setTranscoded(boolean transcoded){
		this.transcoded = transcoded;
	}
	
	public void setTranscodingProgress(int trans){
		transcodingProgress = trans;
	}
	
	public int getTranscodingProgress(){
		return transcodingProgress;
	}
	
	public LinkedList<Comment> getComments() {
		return comments;
	}
	public void addComment(Comment cm){
		comments.add( cm );
	}
	
	public void setFile(FileItem item) throws Exception{
		if( folder == null )
			throw new Exception("Folder not set for Video!");
		// Generate unique filename
		filename = this.genFileName( item.getName() );
		filename += "."+FileUtil.getFileExtension( item.getName() );
		// Move uploaded file
		item.write( folder.getFile( filename, Size.ORIGINAL ) );
		item.delete();
		ZalleryTranscoder.addVideo( this );
	}
	
	/**
	 * Returns the file for the video
	 * 
	 * @param 		size		is the size of the image
	 */
	public File getFile(Size size) throws IOException{
		if( filename != null ){
			if( size == Size.SMALL )
				return folder.getFile( FileUtil.replaceExtension(filename, Image.IMAGE_FORMAT), size );
			else if( size == Size.MEDIUM )
				return folder.getFile( FileUtil.replaceExtension(filename, VIDEO_FORMAT), size );
			else
				return folder.getFile( filename, size );
		}
		return null;
	}
	@Override
	public String getType() {
		return type;
	}
}
