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.*;
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(Iterator<AbstractPlayListItem> items,JComponent[] cmps,TurgenStatus st,boolean postProEnabled) {
       
        st.clearTapeImages();
        
        /*Vyradit komponenty*/
        ParallelGUIUpdater.setComponentsEnabled(cmps,false);
        
        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>");
                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.addFailedTapeImages();
                e.printStackTrace();
                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);
        
        
    }
    
    
    
    
    
    
    

}
