package turgen.plugin.plturbo;

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

public class PlASTBinaryConvertor implements BinaryConvertor {


    private Plugin plugin;
    private String description;
    private int convertorType;


    public PlASTBinaryConvertor(Plugin p,String description,int convType) {
        this.description=description;
        this.plugin=p;
        convertorType=convType;
    }


    public boolean acceptsBinary(DOS2Binary bf) {

        switch (convertorType) {
            case PlASTPlaylistItem.CV_BINARY_AST: {
                if (bf.getTotalSegmentCount()>44 || bf.hasInitSegment()==true) return false; else return true;

            }
            case PlASTPlaylistItem.CV_BINARY_BUT: {
                if (bf.coversMemory( 0x786, 0x889)) return false;
                if (bf.getTotalSegmentCount()>254) return false; else return true;
            }
        }
        
        return false;
        
    }

    public boolean acceptsRecorderModification(int recorderModification) {
        if (recorderModification==BinaryConvertor.REC_MOD_PL_AST) {
            return true;
        }
        return false;
    }

    public boolean exhaustsRecorderModification(int recorderModification) {
        return acceptsRecorderModification(recorderModification);
    }

    public int getPilotToneCount(DOS2Binary bf) {

        switch (convertorType) {
            case PlASTPlaylistItem.CV_BINARY_AST: {
                return bf.getTotalSegmentCount()+1;
            }
            case PlASTPlaylistItem.CV_BINARY_BUT: {
                return bf.getTotalSegmentCount()*2+2;
            }
        }
        return 0;

    }

    public String getDescription() {
        return description;
    }

    public boolean hasLoader() {
        return true;
    }

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

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

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

        PlASTPlaylistItem ri = new PlASTPlaylistItem(inFile,outFile,speed,sName,convertorType,new int[0]);
        ri.pluginClass=ri.pluginClass=Turgen.pluginManager.getPluginClassName(plugin);

        return ri;

    }

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

    public boolean requiresPulses() {
        return true;
    }


}
