package zall.bean;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.fileupload.FileItem;

import zutil.Hasher;
import zutil.db.DBConnection;
import zutil.db.bean.DBBean;
import zutil.log.LogUtil;

public abstract class Media extends DBBean implements Comparable<Media>{
	private static final Logger logger = LogUtil.getLogger();
	
	public static enum Size{
		ORIGINAL, LARGE, MEDIUM, SMALL
	}

	protected Folder folder;
	protected String filename;
	/** owner */
	protected User user;	
	protected String title;
	protected String description;
	protected Timestamp date;
	protected float rating;
	
	
	/**
	 * Loads all the media in a folder
	 */
	public final static List<Media> load(DBConnection db, Folder folder) throws SQLException{
		List<Image> image = Image.loadFolder(db, folder);
		List<Video> video = Video.loadFolder(db, folder);
		
		List<Media> media = new LinkedList<Media>();
		media.addAll(image);
		media.addAll(video);
		Collections.sort( media );
		return media;
	}
	
	public static Media load(DBConnection db, String type, long id) throws SQLException{
		if( type.equals(Image.type) )
			return Image.load(db, id);
		else if( type.equals(Video.type) )
			return Video.load(db, id);
		return null;
	}
	
	
	public Media(){
		setCurrentDate();
	}
	

	public String getFilename() {
		return filename;
	}
	public Folder getFolder() {
		return folder;
	}
	public void setFolder(Folder folder) {
		this.folder = folder;
	}
	public void setImageUrl(String filename) {
		this.filename = filename;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String desc) {
		this.description = desc;
	}
	public Timestamp getDate() {
		return date;
	}
	public void setDate(Timestamp date) {
		this.date = date;
	}
	public void setCurrentDate() {
		this.date = new Timestamp( System.currentTimeMillis() );
	}

	public String genFileName(String str){
		return Hasher.MD5( ""+this.getId()+str+user.getName()+date.getTime()+System.currentTimeMillis() );
	}
	
	
	@Override
	public int compareTo(Media m) {
		return this.date.compareTo( m.date );
	}
	
	public abstract LinkedList<Comment> getComments();
	public abstract void addComment(Comment cm);
	
	public abstract void setFile(FileItem item) throws Exception;
	
	public abstract File getFile(Size size) throws IOException;
	
	public abstract String getType();
	
	public void delete(DBConnection db) throws SQLException{
		super.delete(db);
		for( Size s : Size.values() )
			try {
				this.getFile(s).delete();
				logger.finer("Removed media(id: "+this.getId()+") file(size: "+s+")");
			} catch (IOException e) {
				logger.log(Level.SEVERE, "Can not delete \""+s+"\" file!", e);
			}
	}
}
