package turgen.plugin.csturbo;
import java.io.File;
import turgen.Turgen;
import turgen.Utils;
import turgen.dtb.DOS2Binary;
import turgen.generator.PulseCrate;
import turgen.generator.PulseCreator;
import turgen.playlist.AbstractPlayListItem;
import turgen.plugin.BinaryConvertor;
import turgen.plugin.Plugin;


public class MonolithicBinaryConvertor implements BinaryConvertor {

    private boolean superTurbo;
    private Plugin plugin;

    public MonolithicBinaryConvertor(boolean superTurbo,Plugin p) {
        this.superTurbo=superTurbo;
        this.plugin=p;
    }


    public boolean acceptsBinary(DOS2Binary bf) {
       return bf.isMonolithic();
    }

    public int getPilotToneCount(DOS2Binary bf) {
        return 2;
    }

    public String getDescription() {
        if (superTurbo==false) {
            return "Turbo 2000 - monolithic binary";
        }
        else {
            return "Super Turbo - monolithic binary";
        }
    }

    public boolean hasLoader() {
        return true;
    }

    public AbstractPlayListItem getPlaylistItem(DOS2Binary bf, String inFile, String outFile,PulseCrate speed) {


        int cd = bf.getDataSegmentCount();
        int cr = bf.getJumpSegmentCount();
        if (cd!=1) return null;

        String sLoad=Integer.toString(bf.getFirstDataSegment().start);
        String sRun=(cr==0)?sLoad:Integer.toString(bf.getFirstJumpSegment().jump);
        String sType="3";
        String sLength=Integer.toString(bf.getFirstDataSegment().data.length);

        File f = new File(inFile);
        String sName = f.getName();

        if (superTurbo==false) {
            if (sName.length()>10) sName=sName.substring(0,10);
        }
        else {
            if (sName.length()>20) sName=sName.substring(0,20);
        }
        
        CsTurboPlaylistItem ri = new CsTurboPlaylistItem(inFile,outFile,speed,sType,sName,sLoad,sRun,sLength,CsTurboPlaylistItem.CV_MONOLITHIC,Utils.parseNumberList(""));
        ri.pluginClass=ri.pluginClass=Turgen.pluginManager.getPluginClassName(plugin);
        return ri;

    }

    public PulseCrate[] getSpeeds(int recorderModification) {
        if (superTurbo==false) {
            return Turgen.pulseCache.getPulsesForPlugin("turgen.plugin.csturbo.Turbo2000Plugin");
        }
        else {
            return Turgen.pulseCache.getPulsesForPlugin("turgen.plugin.csturbo.SuperTurboPlugin");
        }
    }

     public boolean acceptsRecorderModification(int recorderModification) {

        /*Super turbo is accepted always*/
        if (recorderModification==BinaryConvertor.REC_MOD_CS_ST) return true;

        /*Turbo 2000 only if not for Super Turbo*/
        if (recorderModification==BinaryConvertor.REC_MOD_CS_T2000 && superTurbo==false) return true;

        /*Ohters are not*/
        return false;


    }

    public boolean exhaustsRecorderModification(int recorderModification) {

        /*Turbo 2000 is exhausted always*/
        if (recorderModification==REC_MOD_CS_T2000) return true;

        /*Super Turbo only when used for Super Turbo*/
        if (recorderModification==BinaryConvertor.REC_MOD_CS_ST && superTurbo==true) return true;

        /*Ohters are not*/
        return false;

    }

    public boolean requiresPulses() {
        return true;
    }


}
