package turgen.output;

import javax.swing.*;
import turgen.playlist.*;
import turgen.*;
import turgen.generator.*;
import turgen.plugin.*;
import turgen.config.*;

/**Na starosti vystup do WAVE souboru system*/
public class WaveOutput {

    public static ConfigClass cfcWaveOutput;
    public static BooleanConfigEntry cfgWOAutoRenameToWAV;
    
    public static void init() {

        /*Initialize configuration class*/
        cfcWaveOutput = new ConfigClass("WaveOutput","WAVE output");

        /*Initialize configuration entries*/
        cfgWOAutoRenameToWAV= new BooleanConfigEntry("autoRenameToWAV","Change extension to .wav",true);

        /*Add entries to the class*/
        cfcWaveOutput.addEntry(cfgWOAutoRenameToWAV);

        /*Register configuration class*/
        Configuration.registerClass(cfcWaveOutput);
        
    }

    /**Zpracuje polozky playlistu na urcenych indexech*/
    public static synchronized void processPlaylistItems(int[] playlistIndices,JComponent[] cmps,TurgenStatus st,boolean postProEnabled) {
        
        ParallelGUIUpdater.setComponentsEnabled(cmps, false);
        
        st.clearWaves();
        
        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.addFailedWaves();
                continue;
            }

            /*Pokus ziskat instrukce*/
            int[] instr = p.getInstructions(api);
            
            /*kdyz nejsou, tak se pokracuje*/
            if (instr==null) {
                ParallelGUIUpdater.showMessage(p.getLastError());
                st.addFailedWaves();
                continue;
            }
            
            /*Zkusi pustit generator*/
            try {
                SignalGenerator g = new SignalGenerator(api,instr,cfgWOAutoRenameToWAV.getAsBoolean(),postProEnabled,SignalGenerator.TYPE_WAVE);
                
                /*Kazdy generator bezi v samostatnem vlakne*/
                Thread t = new Thread((Runnable)g);
                t.start();
                
            }
            catch(Exception e) {
                /*Ostatni vyjimky nejsou OK*/
                e.printStackTrace();
                ParallelGUIUpdater.showMessage(Utils.getTitledExceptionMessage("WAVE output",e));
            }
            
        }
        
        ParallelGUIUpdater.setComponentsEnabled(cmps, true);
        
    }

}
