package turgen.output;

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(int[] playlistIndices,JComponent[] cmps,TurgenStatus st) {
        
        /*Vyradit komponenty*/
        ParallelGUIUpdater.setComponentsEnabled(cmps,false);    
        
        st.clearDirect();
        
        int l = playlistIndices.length;
        AbstractPlayListItem api;
        
        
        /*pro kazdou polozku*/
        for(int i=0;i<l;i++) {
            api = Turgen.playList.getItem(playlistIndices[i]);

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

            /*Kdyz pluginClass neni, tak se pokracuje*/
            if (p==null) {
                ParallelGUIUpdater.showMessage("Plugin specified in playlist item not found !");
                st.addFailedDirect();
                continue;
            }

            /*Pokus ziskat instrukce*/
            int[] instr = p.getInstructions(api);
            
            /*kdyz nejsou, tak se pokracuje*/
            if (instr==null) {
                ParallelGUIUpdater.showMessage(p.getLastError());
                st.addFailedDirect();
                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 (i!=l-1) 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);    

        
        
    }

}
