package turgen.plugin.csturbo;

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


public class BlockloadingBinaryConvertor implements BinaryConvertor {

    private boolean superTurbo;
    private int ldrStart;
    private int ldrEnd;
    private Plugin plugin;


    public BlockloadingBinaryConvertor(boolean superTurbo,int ldrStart,int ldrEnd,Plugin p) {
        this.superTurbo=superTurbo;
        this.ldrStart=ldrStart;
        this.ldrEnd=ldrEnd;
        this.plugin=p;
    }

    public boolean acceptsBinary(DOS2Binary bf) {

        if (bf.coversMemory(ldrStart, ldrEnd)) return false;
        if ((bf.getDataSegmentCount()+bf.getJumpSegmentCount())>62) return false;
        
        return true;

    }

    public int getPilotToneCount(DOS2Binary bf) {

        int cnt = bf.getDataSegmentCount() + bf.getJumpSegmentCount();
        
        /*Bound*/
        if (cnt<0) cnt=0;

        return cnt;

    }

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

    public boolean hasLoader() {
        return true;
    }

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

        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,"",sName,"","","",CsTurboPlaylistItem.CV_SEGMENTED_BLOCK,Utils.parseNumberList(""));
        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;
    }
}
