package turgen.tapeimageextractor;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import turgen.FileFormatException;
import turgen.Utils;
import turgen.decoder.FileDataCache;
import turgen.dtb.DOS2Binary;
import turgen.dtb.DOS2BinaryException;
import turgen.tapeimage.TapeImage;
import turgen.tapeimage.TapeImageChunk;

public class TapeImageModel extends AbstractTableModel {

    private TapeImage tapeImage;
    private Vector<ExtractorChunk> extractorChunks;
    private static final int CI_CHUNKID=0;
    private static final int CI_CHUNKNAME=1;
    private static final int CI_IRG=2;
    private static final int CI_LENGTH=3;
    private static final int CI_HEAD=4;
    private static final int CI_TAIL=5;
    private static final int CI_HINT=6;
    private static final String unknownData = "?";
    
    public TapeImageModel() {
        createEmptyModel();
    }
    
    public void clear() {
        createEmptyModel();
        fireTableDataChanged();
    }
    
    public void openTapeImage(String filespec) throws Exception {
        /*Clear extractor chunks*/
        extractorChunks.clear();
        fireTableDataChanged();
        
        /*Open tape image*/
        tapeImage = new TapeImage();
        tapeImage.parse(filespec);
        
        /*Create extractor chunks*/
        TapeImageChunk tic;
        int numChunks = tapeImage.getChunkCount();
        
        for(int i=0;i<numChunks;i++) {
            extractorChunks.add(new ExtractorChunk(tapeImage.getChunkAt(i),i));
        }
        
        fireTableDataChanged();
        
    }
    
    public int getRowCount() {
        return extractorChunks.size();
    }

    public int getColumnCount() {
        return colNames.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        ExtractorChunk chunk = extractorChunks.elementAt(rowIndex);
        
        if (chunk==null) return unknownData;
        
        switch(columnIndex) {
            
            case CI_CHUNKID: {
                return chunk.getId();
            }
            case CI_CHUNKNAME: {
                return chunk.getChunkName();
            }
            case CI_IRG: {
                return chunk.getIRGString();
            }
            case CI_LENGTH: {
                return chunk.getLengthString();
            }
            case CI_HEAD: {
                return chunk.getHeadString();
            }
            case CI_TAIL: {
                return chunk.getTailString();
            }
            case CI_HINT: {
                return chunk.getHintString();
            }
            default: {
                return unknownData;
            }
                
        }
    }
    
    private final String[] colNames = {"#","Chunk","IRG","Length","Head","Tail","Hint"};
    
    public String getColumnName(int index) {
        return colNames[index];
    }
    
    public Class getColumnClass(int index) {
         return String.class;
     }
    
    private void createEmptyModel() {
        tapeImage = new TapeImage();
        extractorChunks = new Vector<ExtractorChunk>();
    }

    ExtractorChunk elementAt(int i) {
        return extractorChunks.elementAt(i);
    }

    void updateExtractionHints(int[] indexes, HintsCrate crate,HintsCrate.Map map) {
        
        for(int i=0;i<indexes.length;i++) {
            int index = indexes[i];
            ExtractorChunk c = extractorChunks.elementAt(index);
            c.updateHintsCrate(crate,map);
        }
        
        fireTableDataChanged();
    }
    
    ExtractionReportCrate extractChunks(int[] indexes,String filespec,ExtractionReporter rep) throws Exception {
        
        ExtractionReportCrate reportCrate = new ExtractionReportCrate();
        ExtractorChunk.ExtractionCrate eCrate;
        
        FileOutputStream fos = new FileOutputStream(filespec);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        
        
        
        for(int i=0;i<indexes.length;i++) {
            ExtractorChunk chunk = elementAt(indexes[i]);
            reportCrate.totalChunks++;
            
            String id = Utils.padZeros(chunk.getId(), 4);
            
            /*We skip chunks that cannot be extracted*/
            if (chunk.isExtractible()==false) {
                reportCrate.skippedChunks++;
                rep.addMessage(id+":Not extractible");
                continue;
            }
            
            /*Extraction of one chunk*/
            eCrate = chunk.extractData();
            if (eCrate.completionCode!=ExtractorChunk.CC_OK) {
                switch (eCrate.completionCode) {
                    case ExtractorChunk.CC_BAD_HINT: {
                        rep.addMessage(id+":Bad hints");
                        reportCrate.badHintsChunks++;
                        break;
                    }
                    case ExtractorChunk.CC_BAD_LENGTH: {
                        rep.addMessage(id+":Bad length");
                        reportCrate.badHintsChunks++;
                        break;
                    }
                }
                
            }
            
            for(int k=0;k<eCrate.data.length;k++) {
                bos.write(eCrate.data[k]);
            }
            
        }
        
        /*Close output file*/
        bos.flush();
        bos.close();
        
        /*Report*/
        return reportCrate;
        
    }

    void resetHints(int[] indexes) {
        for(int i=0;i<indexes.length;i++) {
            int index = indexes[i];
            ExtractorChunk c = extractorChunks.elementAt(index);
            c.resetHints();
        }
        fireTableDataChanged();
    }
    
    /** Check if selected chunks can represent a boot file
     * 
     * @param indexes Indexes of chunks selected
     * @return true When selected chunks appear to contain a boot file
     */
    boolean isSelectionBootFile(int[] indexes) {
        
        try {
            getSelectionBootData(indexes);
        }
        catch (FileFormatException ffe) {
            return false;
        }
        return true;
        
    }
    
    private int[] getSelectionBootData(int[] indexes) throws FileFormatException {
        
        final String EXCP_PFX="Not a boot file. ";
                
        /*At least one chunk selected?*/
        if (indexes.length<1) {
            throw new FileFormatException(EXCP_PFX+"No chunks.");
        }

        /*Get first chunk*/
        ExtractorChunk firstChunk = elementAt(indexes[0]);
        if (firstChunk.getRawLength()!=132) {
            throw new FileFormatException(EXCP_PFX+"First record is not a standard record.");
        }
        
        FileDataCache cache = new FileDataCache();
        
        /*Prepare crate and get data*/
        ExtractorChunk.ExtractionCrate eCrate;
        eCrate = firstChunk.extractData();
        
        /*If not extracted OK, bail out*/
        if (eCrate.completionCode!=ExtractorChunk.CC_OK) {
            throw new FileFormatException(EXCP_PFX+"Data of first chunk extracted with errors.");
        }
        
        /*Is there enough data*/
        if (eCrate.data.length<128) {
            throw new FileFormatException(EXCP_PFX+"First record is not a full record.");
        }
        
        /*Determine total boot records*/
        int totalBootRecords = eCrate.data[1];
        if (totalBootRecords==0) totalBootRecords=256;
        int numRecords=1;
        cache.add(eCrate.data);
        
        /*Check if there is enough chunks*/
        for(int i=1;i<indexes.length;i++) {
            ExtractorChunk chunk = elementAt(indexes[i]);
            if (chunk.getRawLength()!=132) {
                throw new FileFormatException(EXCP_PFX+"Record "+chunk.getId()+" is not a standard record.");
            }
            if (chunk.isExtractible()==false) continue;
            
            eCrate = chunk.extractData();
            if (eCrate.completionCode!=ExtractorChunk.CC_OK) {
                throw new FileFormatException(EXCP_PFX+"Record "+chunk.getId()+" extracted with errors.");
            }
            numRecords++;
            cache.add(eCrate.data);
        }
        
        if (numRecords==totalBootRecords) return cache.getBytes();
        throw new FileFormatException(EXCP_PFX+"Total number of boot records does not match boot header. (t "+Integer.toString(numRecords)+",h "+Integer.toString(totalBootRecords)+")");
    }

    boolean isSelectionBinaryFile(int[] selectedIndexes) {
        
        int[] data = getSelectionData(selectedIndexes);
        
        DOS2Binary dtb = new DOS2Binary("");
        try {
           dtb.analyzeFromData(data);
           return true;
        }
        catch(IOException e) {
            return false;
        }
        catch(DOS2BinaryException dtbe) {
            return false;
        }
    }
    
    private int[] getSelectionData(int[] selectedIndexes) {
        
        FileDataCache cache = new FileDataCache();
        ExtractorChunk chunk;
        ExtractorChunk.ExtractionCrate eCrate;
        
        for (int i=0;i<selectedIndexes.length;i++) {
            chunk = elementAt(selectedIndexes[i]);
            if (chunk.isExtractible()==false) continue;
            eCrate = chunk.extractData();
            cache.add(eCrate.data);
        }
        
        return cache.getBytes();
    }
    
    void extractBinaryFile(int[] selectedIndexes,String filespec) throws Exception {
        
        /*Get data from selected chunks*/
        int[] data = getSelectionData(selectedIndexes);
        
        /*Check if really binary file. If not, an exception is thrown*/
        DOS2Binary dtb = new DOS2Binary("");
        dtb.analyzeFromData(data);
        
        /*Store data to a real file*/
        FileOutputStream fos = new FileOutputStream(filespec);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        
        for(int i=0;i<data.length;i++) {
            bos.write(data[i]);
        }
        
        /*Close output stream*/
        bos.flush();
        bos.close();
    }
    
    void extractBootFile(int[] selectedIndexes,String filespec) throws Exception {
        
        /* Let us extract boot data*/
        int[] bootData = getSelectionBootData(selectedIndexes);
        if (bootData==null || bootData.length<6) {
            throw new FileFormatException("Selected chunks do not contain a boot file");
        }
        
        if (bootData.length<6) {
            throw new FileFormatException("Selected chunks do not contain a boot file");
        }
        
        /*Store data to a real file*/
        FileOutputStream fos = new FileOutputStream(filespec);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        
        /*Write binary file header*/
        bos.write(255);
        bos.write(255);
        
        /*First segment - header*/
        bos.write(bootData[2]);
        bos.write(bootData[3]);
        int firstAddr=bootData[2]+256*bootData[3];
        int lastAddr=(firstAddr)+bootData.length-1;
        bos.write(lastAddr % 256);
        bos.write(lastAddr / 256);
        
        /*First segment - data*/
        for(int i=0;i<bootData.length;i++) {
            bos.write(bootData[i]);
        }
        
        /*INIT segment - header*/
        bos.write(0xE2);
        bos.write(0x02);
        bos.write(0xE3);
        bos.write(0x02);
        /*INIT segment - data*/
        bos.write((firstAddr+6) % 256);
        bos.write((firstAddr+6) / 256);
        
        /*RUN segment - header*/
        bos.write(0xE0);
        bos.write(0x02);
        bos.write(0xE1);
        bos.write(0x02);
        /*RUN segment - data */
        bos.write(bootData[4]);
        bos.write(bootData[5]);
        
        bos.flush();
        bos.close();
    }
    
    void extractPlainDataFile(int[] selectedIndexes,String filespec,int loadAddr,int runAddr) throws Exception {
        
        int[] data = getSelectionData(selectedIndexes);
        
        /*Store data to a real file*/
        FileOutputStream fos = new FileOutputStream(filespec);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        
        /*Write binary file header*/
        bos.write(255);
        bos.write(255);
        
        /*Data segment - header*/
        bos.write(loadAddr % 256);
        bos.write(loadAddr / 256);
        int lastAddr = loadAddr+data.length-1;
        bos.write(lastAddr % 256);
        bos.write(lastAddr / 256);
        
        /*Data segment - data*/
        for(int i=0;i<data.length;i++) {
            bos.write(data[i]);
        }
        if (runAddr!=-1) {
            /*RUN segment - header*/
            bos.write(0xE0);
            bos.write(0x02);
            bos.write(0xE1);
            bos.write(0x02);
            /*RUN segment - data */
            bos.write(runAddr % 256);
            bos.write(runAddr / 256);
        }
        
        bos.flush();
        bos.close();
        
    }
    
    
   


}
