package turgen.plugin.csturbo;

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


public class ChainloadingBinaryConvertor implements BinaryConvertor {

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

    public ChainloadingBinaryConvertor(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 (getNumberOfFiles(bf)>254) return false;

        return true;

    }

    private int getNumberOfFiles(DOS2Binary bf) {

        /*Presuming one file for each segment*/
        bf.prepareForDumbLoader();
        int cnt = bf.getDataSegmentCount() + bf.getJumpSegmentCount();

        ArrayList segments = bf.allSegments;
        int l = segments.size();

        Segment s=null;

        /*Number of files can be reduced*/
        for (int i=1;i<l;i++) {
            s=(Segment)segments.get(i);
            if ( (s.type==Segment.INIT_SEGMENT || s.type==Segment.RUN_SEGMENT)  && ((Segment)(segments.get(i-1))).type==Segment.DATA_SEGMENT) cnt--;
        }

        return cnt;
    }

    public int getPilotToneCount(DOS2Binary bf) {

        /*Number of files*/
        int cnt = getNumberOfFiles(bf);
        cnt*=2;

        /*Bound*/
        if (cnt<0) cnt=0;

        return cnt;

    }

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

    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_CHAIN,Utils.parseSilenceList(""));
        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;
    }
}
