/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package turgen.plugin.kblock;

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

/**
 *
 * @author michael
 */
public class KBlockBinaryConvertor implements BinaryConvertor {

    
    private Plugin plugin;

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

    private String description;


    public KBlockBinaryConvertor(Plugin 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==KBlockPlugin.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_T2000) return true;
        return false;
    }

    public int getPilotToneCount(DOS2Binary bf) {

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

        /*Header*/
        blocks++;

        return blocks;
    }

    public String getDescription() {
        return description;
    }

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

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

        int hole[] = {0};

        /*Extract name*/
        File f = new File(inFile);
        String sName = f.getName();

        if (sName.length()>16) sName=sName.substring(0,16);

        KBlockPlayListItem ri = new KBlockPlayListItem(inFile,outFile,speed,sName,hole,ldrIndex);
        ri.pluginClass=ri.pluginClass=Turgen.pluginManager.getPluginClassName(plugin);

        return ri;

    }

    public PulseCrate[] getSpeeds(int recorderModification) {
        return Turgen.pulseCache.getPulsesForPlugin(plugin.getClass().getName());
    }

    public boolean requiresPulses() {
        return true;
    }


}
