package backend;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

public class Song {
	private int id;
	private long size;
	private File file;
	
	public Song() {
		
	}
	
	public Song(int id) {
		this.setId(id);
	}
	
	public Song(int id, String loc) throws URISyntaxException {
		this(id);
		this.setLocation(loc);
	}
	
	public Song(int id, String loc, long size) throws URISyntaxException {
		this(id,loc);
		this.setSize(size);
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public void setLocation(String loc) throws URISyntaxException {
		URI u = new URI(loc);
		String s = u.getPath();
		this.file = new File(s);
	}
	
	public void setSize(long size) {
		//Debug.println("Song "+this.id+": size "+size);
		this.size = size;
	}
	
	public int getId() {
		return this.id;
	}
	
	/**
	 * Tests if Song is (protected) MP4 file
	 * @return true if song's file extension is mp4, otherwise false
	 */
	public boolean isMP4() {
		if (this.file.getName().lastIndexOf(".") >= 0) {
			String ext = this.file.getName().substring(this.file.getName().lastIndexOf("."), this.file.getName().length());
			return ".mp4".equalsIgnoreCase(ext); 
		}
		return false;
	}
	
	public String getLocation() {
		return this.file.getAbsolutePath();
	}
	
	public String getEncodedLocation() {
		try {
			return URLEncoder.encode(this.getLocation(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String getFilename() {
		return this.file.getName();
	}
	
	public long getSize() {
		return this.size;
	}
}
