package com.swandiggy.poe4j.data.readers.value;

import com.swandiggy.poe4j.Poe4jException;
import com.swandiggy.poe4j.data.Constants;
import com.swandiggy.poe4j.data.DatFileReader;
import com.swandiggy.poe4j.data.DatFileReaderFactory;
import com.swandiggy.poe4j.data.rows.AbstractRow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * @author Jacob Swanson
 * @since 12/15/2015
 */
@Service
public class RowReader extends BaseValueReader<AbstractRow> {

    @Autowired
    private DatFileReaderFactory datFileReaderFactory;

    @Override
    public boolean supports(Class clazz) {
        return DatFileReader.entityClasses.containsValue(clazz);
    }

    @Override
    protected AbstractRow readInternal(DatFileReader reader, Class clazz) {
        long index = reader.getBr().readLong();
        if (index == Constants.MAGIC_NULL) {
            return null;
        }

        // Get the referenced .dat file
        try (DatFileReader datFileReader = datFileReaderFactory.create(Paths.get(reader.getFile().getParent(), reader.getEntityClasses().inverse().get(clazz) + ".dat").toFile())) {
            return datFileReader.read(index);
        } catch (IOException e) {
            throw new Poe4jException(e);
        }
    }

    @Override
    public int size(Class clazz) {
        return 8;
    }
}

@@ -342,16 +342,116 @@
 owired;%0A
+import org.springframework.cglib.proxy.Enhancer;%0Aimport org.springframework.cglib.proxy.LazyLoader;%0A
 import o
@@ -489,16 +489,16 @@
 ervice;%0A
-
 %0Aimport 
@@ -1088,24 +1088,161 @@
 %0A        %7D%0A%0A
+        Enhancer enhancer = new Enhancer();%0A        enhancer.setSuperclass(clazz);%0A        enhancer.setCallback((LazyLoader) () -%3E %7B%0A    
         // G
@@ -1277,16 +1277,20 @@
         
+    
 try (Dat
@@ -1462,24 +1462,28 @@
             
+    
 return datFi
@@ -1512,16 +1512,20 @@
         
+    
 %7D catch 
@@ -1538,24 +1538,28 @@
 eption e) %7B%0A
+    
             
@@ -1595,17 +1595,82 @@
         
-%7D
+    %7D%0A        %7D);%0A%0A        return (AbstractRow) enhancer.create();
 %0A    %7D%0A%0A

Make rows load lazily