package turgen.plugin.btape;

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


public class BTapeBinaryConvertor implements BinaryConvertor {

    private final BTapePlugin plugin;

    private final int ldrStart,ldrEnd;
    private final int blkStart,blkEnd;
    private final int ldrIndex;

    private final String description;


    public BTapeBinaryConvertor(BTapePlugin p,String description,int ldrStart,int ldrEnd, int blkStart,int blkEnd,int ldrIndex) {
        this.description=description;
        this.ldrStart=ldrStart;
        this.ldrEnd=ldrEnd;
        this.blkStart=blkStart;
        this.blkEnd=blkEnd;
        this.ldrIndex=ldrIndex;
        this.plugin=p;
    }



    public boolean acceptsBinary(DOS2Binary bf) {
        if (ldrIndex==BTapePlugin.BL_NONE) return true;
        if (bf.coversMemory(ldrStart, ldrEnd)) return false;
        if (bf.coversMemory(blkStart, blkEnd)) return false;
        return true;
    }

    public boolean acceptsRecorderModification(int recorderModification) {
        if (recorderModification==BinaryConvertor.REC_MOD_CS_ST || recorderModification==BinaryConvertor.REC_MOD_CS_T2000 ) {
            return true;
        }
        else return false;
    }

    public boolean exhaustsRecorderModification(int recorderModification) {
        if (recorderModification==BinaryConvertor.REC_MOD_CS_ST || recorderModification==BinaryConvertor.REC_MOD_CS_T2000 ) {
            return true;
        }
        else return false;
    }

    public int getPilotToneCount(DOS2Binary bf) {

        /*One pilot tone per kilobyte*/
        int ln = bf.getFileStatistics()[0];
        int blocks = ln/1008;
        if ((ln%1008)!=0) blocks++;

        /*First block always twice*/
        blocks++;

        return blocks;
    }

    public String getDescription() {
        return description;
    }

    public boolean hasLoader() {
        if (ldrIndex==BTapePlugin.BL_NONE) return false;
        return true;
    }

    public AbstractPlayListItem getPlaylistItem(DOS2Binary bf, String inFile, String outFile, PulseCrate speed,int[] silenceList) {

       

        /*Extract name*/
        File f = new File(inFile);
        BTapePlayListItem ri = new BTapePlayListItem(inFile,outFile,speed,plugin.get83Filename(f.getName()),silenceList,false,false,ldrIndex);
        ri.pluginClass=ri.pluginClass=Turgen.pluginManager.getPluginClassName(plugin);

        return ri;

    }

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

    }

    public boolean requiresPulses() {
        return true;
    }

}
