package org.meteogroup.grib_library.grib1;

import org.meteogroup.grib_library.exception.BinaryNumberConversionException;
import org.meteogroup.grib_library.exception.GribReaderException;
import org.meteogroup.grib_library.grib1.model.Grib1Record;
import org.meteogroup.grib_library.util.FileChannelPartReader;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by roijen on 20-Oct-15.
 */
public class Grib1CollectionReaderService {

    //TODO FIX MEMBER VARIABLE!!

    public Grib1RecordReaderService recordReader;
    public FileChannelPartReader partReader;

    public long gribRecordOffset = -1;
    public long fileLength = -1;
    public FileChannel fc = null;

    private static final int HEADER_LENGTH = 8;

    public List<Grib1Record> readFileFromFileName(String url) throws IOException, BinaryNumberConversionException, GribReaderException {
        this.fc = this.getFileChannelFromURL(url);
        return this.readAllRecords(fc);
    }

    public FileChannel getFileChannelFromURL(String url) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(url, "r");
        fileLength = raf.length();
        gribRecordOffset = 0;
        return raf.getChannel();
    };

    public long getGribRecordOffset() {
        return gribRecordOffset;
    }

    public long getFileLength() {
        return fileLength;
    }

    public List<Grib1Record> readAllRecords(FileChannel fileChannel) throws IOException, GribReaderException, BinaryNumberConversionException {
        ArrayList<Grib1Record> response = new ArrayList<Grib1Record>();
        while (gribRecordOffset < fileLength){
            byte[] recordHeader = partReader.readPartOfFileChannel(fileChannel, gribRecordOffset, HEADER_LENGTH);
            if (!recordReader.checkIfGribFileIsValidGrib1(recordHeader)){
                throw new GribReaderException ("Attempted to read invalid grib record");
            }
            Grib1Record record = new Grib1Record();
            record.setLength(recordReader.readRecordLength(recordHeader));
            byte[] recordAsByteArray = partReader.readPartOfFileChannel(fileChannel,gribRecordOffset,record.getLength());
            record = recordReader.readCompleteRecord(record,recordAsByteArray, HEADER_LENGTH);
            response.add(record);
            gribRecordOffset += recordReader.readRecordLength(recordHeader);
        }
        return response;
    }
}

@@ -547,53 +547,16 @@
 e %7B%0A
-%0A
     
-//TODO FIX MEMBER VARIABLE!!%0A%0A    public
+%0A   
  Gri
@@ -591,31 +591,24 @@
 Reader;%0A    
-public 
 FileChannelP
@@ -630,31 +630,24 @@
 eader;%0A%0A    
-public 
 long gribRec
@@ -662,31 +662,24 @@
 t = -1;%0A    
-public 
 long fileLen
@@ -688,31 +688,24 @@
 h = -1;%0A    
-public 
 FileChannel 

Refactor (part II...)