package turgen.plugin.btape;

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;


public class BTapeBinaryConvertor implements BinaryConvertor {

    private Plugin plugin;

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

    private String description;


    public BTapeBinaryConvertor(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==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 hole[] = {0};

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

        /*Create 8.3 filename if possible*/
        String nm = f.getName();

        String fn="";
        String fext="";

        int dotIndex=nm.indexOf('.');

        /*No dot*/
        if (dotIndex==-1) {
            sName=nm.substring(0,Math.min(8, nm.length()));

        }
        else {
            /*Dot exists*/
            fn = nm.substring(0,dotIndex);
            fext = nm.substring(dotIndex);
            sName= fn.substring(0,Math.min(8, fn.length()))+fext.substring(0,Math.min(4, fext.length()));
        }

        BTapePlayListItem ri = new BTapePlayListItem(inFile,outFile,speed,sName,hole,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;
    }

}
