package audiobook;

//~--- non-JDK imports --------------------------------------------------------

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
import Dating.Time;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.TagException;

//~--- JDK imports ------------------------------------------------------------

import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;

/**
 * This class represents a chapter in a audiobook
 * @author Kim
 */
public class Chapter implements Comparable<Chapter> {
    private File        file;
    private Dating.Time time;
    private String      title;

    /**
     * Creates a new empty <code>Chapter</code>
     */
    public Chapter() {}

    /**
     * Creates a new Chapter with the specified Values
     * @param time the Duration of this track
     * @param title the name of this chapter
     * @param file the audiofile this chapter represents
     */
    public Chapter(Time time, String title, File file) {
        this.time  = time;
        this.title = title;
        this.file  = file;
    }

    @Override
    public String toString() {
        return title;
    }

    /**
     * Returns the audiofile of this chapter
     */
    public File getFile() {
        return file;
    }

    /**
     * Returns a <code>Time</code> Object containing the information about the duration of this chapter
     */
    public Time getTime() {
        return time;
    }

    /**
     * Returns the title of this chapter
     */
    public String getTitle() {
        return title;
    }

    /**
     * Sets the audiofile of this chapter
     * @param file a file.
     * @param calc wether the time should be calculated after setting the file.
     * @throws IllegalArgumentException If the file is not a m4a file.
     * 
     * @see #calculateTime() Chapter.calculateTime()
     * @see File
     */
    public void setFile(File file, boolean calc) throws IllegalArgumentException, CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
        if(!file.getName().endsWith(".m4a")){
            throw new IllegalArgumentException("Not a m4a File");
        }
        this.file = file;
        if(calc){
            calculateTime();
        }
    }
    
    /**
     * Sets the audiofile of this chapter. The Time will NOT be calculated automatically
     * @param f a File
     * @throws IllegalArgumentException If the file is not a m4a file.
     * 
     * @see File
     */
    public void setFile(File f) throws IllegalArgumentException{
        try{
            setFile(f, false);
        } catch(CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException ex){}
    }

    /**
     * Sets the duration of this chapter. Should be calculated using {@link calculateTime()}.
     * 
     * @see #calculateTime() Chapter.calculateTime();
     * @see Time
     */
    public void setTime(Time time) {
        this.time = time;
    }

    /**
     * Sets the title of this chapter
     * @param title 
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Automatically calculates the audiofile's Duration and sets it for this chapter
     * @see org.jaudiotagger.audio.AudioFile
     * @see org.jaudiotagger.audio.AudioFileIO#read(java.io.File f);
     */
    public void calculateTime()
            throws CannotReadException, IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
        AudioFile aFile = AudioFileIO.read(file);
        setTime(new Time(0, 0, aFile.getAudioHeader().getTrackLength()));
    }

    @Override
    public int compareTo(Chapter o) {
        return getTitle().compareTo(o.getTitle());
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
