package turgen.output;

import java.util.Iterator;
import javax.swing.*;
import turgen.playlist.*;
import turgen.*;
import turgen.generator.SignalGenerator;
import turgen.plugin.*;
import turgen.config.*;



/**Na starosti vystup na zvukovy system*/
public class AudioOutput {
    
    
    static IntegerConfigEntry cfgInterRecordGap;
    static ConfigClass cfcAudioOutput;
    
    public static void init() {

        /*Initialize configuration class*/
        cfcAudioOutput = new ConfigClass("AudioOutput","Audio output");

        /*Initialize configuration entries*/
        cfgInterRecordGap = new IntegerConfigEntry("interRecordGap","Silence between files [ms]",500,0,5000);

        /*Add entries to the class*/
        cfcAudioOutput.addEntry(cfgInterRecordGap);

        /*Register configuration class*/
        Configuration.registerClass(cfcAudioOutput);
    }
    
    
    /**Zpracuje polozky playlistu na urcenych indexech*/
    public static synchronized void processPlaylistItems(Iterator<AbstractPlayListItem> items,JComponent[] cmps,TurgenStatus st) {
        
        /*Vyradit komponenty*/
        ParallelGUIUpdater.setComponentsEnabled(cmps,false);    
        
        st.clearDirect();
        
        
        AbstractPlayListItem api;
        
        
        /*pro kazdou polozku*/
        while (items.hasNext()) {
            api = items.next();

            /*Zjistit zasuvny modul*/
            Plugin p = Turgen.pluginManager.getPlugin(api.pluginClass);

            /*Kdyz pluginClass neni, tak se pokracuje*/
            if (p==null) {
                ParallelGUIUpdater.showMessage("<HTML>Plugin \""+api.pluginClass+"\" <BR>specified in a playlist item not found.</HTML>");
                st.addFailedDirect();
                continue;
            }

            /*Convert an item and get SignalGenerator instructions*/
            int[] instr;
            try {
                instr = p.convertItem(api);
            }
            catch (Exception e) {
                ParallelGUIUpdater.showMessage(Utils.getTitledExceptionMessage("Conversion failed", e));
                st.addFailedDirect();
                e.printStackTrace();
                continue;
            }
            
            /*Zkusi pustit generator*/
            try {
                SignalGenerator g = new SignalGenerator(api,instr,false,false,SignalGenerator.TYPE_AUDIO);
                g.run();
                /*Pocka se az skonci*/
                if (g.getCancelledType()==SignalGenerator.CANCEL_ALL) break;
                if (g.getErrorMessage()!=null) ParallelGUIUpdater.showMessage(g.getErrorMessage());
                if (items.hasNext()) Thread.sleep(cfgInterRecordGap.getAsInt());
            }
            catch(InterruptedException ie) {
                /*Nekdo stisknul STOP, nebo STOP ALL,
                 *coz je naprosto OK*/
            }
            catch(Exception e) {
                /*Ostatni vyjimky nejsou OK*/
                e.printStackTrace();
                ParallelGUIUpdater.showMessage(Utils.getTitledExceptionMessage("Audio output",e));
            }
            
        }

        /*Povoli zpet komponenty*/
        ParallelGUIUpdater.setComponentsEnabled(cmps,true);    

        
        
    }

}
