package turgen.playlist;
import java.io.*;
import turgen.generator.PulseCrate;

/**
 * Abstract playlist item
 */

public abstract class AbstractPlayListItem  {
    
    /**  Input file*/
    public String inFile=null; 
    /**  Output file*/
    public String outFile=null;
    /** Plugin class*/
    public String pluginClass=null;
    /**Pulses*/
    public PulseCrate pulses=null;
    /**Silence list*/
    public int[] silenceList=null;
    
    /**Version*/
    public int version = 0;
    
    
    /**Get String representation
     *@return String representation of playlist item
     */
    public abstract String toString();
    
    /*Validation of playlist item
     * @return true when playlist item is valid, otherwise false
     */
    public abstract boolean validate();
    
    /** Get deep copy of the playlist item 
     * 
     * @return Copy of the playlist item
     */
    public abstract AbstractPlayListItem getClonedCopy();
    
    protected void copyBasics(AbstractPlayListItem newItem) {
        newItem.pluginClass=pluginClass;
    }
    
    /** Save playlist item to a stream
     * 
     * @param dos Stream
     * @throws Exception 
     */
    public abstract void saveToStream(DataOutputStream dos) throws Exception;
    
    /** Load playlist item from a stream
     * 
     * @param dos Output stream
     * @param version Version of the playlist item
     * @throws Exception 
     */
    public abstract void loadFromStream(DataInputStream dos,int version) throws Exception;
    
    
    protected final void saveBasics(DataOutputStream dos) throws Exception {
        
        /*Basic information*/
        dos.writeInt(version);
        dos.writeUTF(pluginClass);
        dos.writeUTF(inFile);
        dos.writeUTF(outFile);
        
        /*Pulses*/
        if (pulses==null) {
            PulseCrate nullPulses = new PulseCrate();
            dos.writeInt(0);
            nullPulses.saveToStream(dos);
        }
        else {
            dos.writeInt(0xFFFFFFFF);
            pulses.saveToStream(dos);
        }
        
        /*Silence list*/
        if (silenceList==null) {
            dos.writeInt(0);
            dos.writeInt(0);
        }
        else {
            dos.writeInt(0xFFFFFFFF);
            dos.writeInt(silenceList.length);
            for(int i=0;i<silenceList.length;i++) {
                dos.writeInt(silenceList[i]);
            }
        }
               
    }
    
    protected final void loadBasics(DataInputStream dis) throws Exception {
        
        /*Basic information*/
        pluginClass = dis.readUTF();
        inFile=dis.readUTF();
        outFile=dis.readUTF();
        
        /*Pulses*/
        int nullFlag = dis.readInt();
        PulseCrate loadedPulses = new PulseCrate();
        loadedPulses.loadFromStream(dis);
        
        if (nullFlag!=0) {
           pulses=loadedPulses;
        }
        else {
            pulses=null;
        }
        
        /*Silence list*/
        nullFlag = dis.readInt();
        if (nullFlag == 0) {
            dis.readInt();
            silenceList=null;
        }
        else {
            int silenceListLength = dis.readInt();
            silenceList = new int[silenceListLength];
            for(int i=0;i<silenceListLength;i++) {
                silenceList[i]=dis.readInt();
            }
        }
        
    }
    
}
