package turgen.output;

import javax.swing.*;

import turgen.playlist.*;
import turgen.*;
import turgen.generator.SignalGenerator;
import turgen.plugin.*;
import turgen.config.*;
import turgen.generator.TapeImageGenerator;


/**Na starosti vystup obrazu kazety*/
public class TapeImageOutput {


    public static ConfigClass cfcTapeImageOutput;
    public static BooleanConfigEntry cfgTIOAutoRenameToCAS;

    public static TapeImageOutput self = new TapeImageOutput();
    
            
    public static void init() {

        /*Initialize configuration class*/
        cfcTapeImageOutput = new ConfigClass("TapeImageOutput","Tape image output");

        /*Initialize configuration entries*/
        cfgTIOAutoRenameToCAS= new BooleanConfigEntry("autoRenameToCAS","Change extension to .cas",true);

        /*Add entries to the class*/
        cfcTapeImageOutput.addEntry(cfgTIOAutoRenameToCAS);

        /*Register configuration class*/
        Configuration.registerClass(cfcTapeImageOutput);
        
    }
    
    
    
    
    /**Zpracuje polozky playlistu na urcenych indexech*/
    public static synchronized void processPlaylistItems(int[] playlistIndices,JComponent[] cmps,TurgenStatus st,boolean postProEnabled) {
       
        st.clearTapeImages();
        
        /*Vyradit komponenty*/
        ParallelGUIUpdater.setComponentsEnabled(cmps,false);
        
        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 was found !");
                continue;
            }

            /*Pokus ziskat instrukce*/
            int[] instr = p.getInstructions(api);
            
            /*kdyz nejsou, tak se pokracuje*/
            if (instr==null) {
                ParallelGUIUpdater.showMessage(p.getLastError());
                continue;
            }
            
            /*Zkusi pustit generator*/
            try {
                TapeImageGenerator g = new TapeImageGenerator(instr,api,cfgTIOAutoRenameToCAS.getAsBoolean(),postProEnabled);
                g.run();
                /*Pocka se az skonci*/
                if (g.getCancelledType()==SignalGenerator.CANCEL_ALL) break;
                if (g.getErrorMessage()!=null) ParallelGUIUpdater.showMessage(g.getErrorMessage());
            }
            
            catch(Exception e) {
                /*Ostatni vyjimky nejsou OK*/
                e.printStackTrace();
                ParallelGUIUpdater.showMessage(Utils.getTitledExceptionMessage("Tape image output",e));
            }
            
        }
        
        /*Povolit komponenty*/
        ParallelGUIUpdater.setComponentsEnabled(cmps,true);
        
        
    }
    
    
    
    
    
    
    

}
