package net.katsuster.strview.media.riff;

import java.util.*;

import net.katsuster.strview.util.*;
import net.katsuster.strview.media.*;

/**
 * <p>
 * idx1 chunk
 * </p>
 *
 * <p>
 * 参考規格
 * </p>
 * <ul>
 * <li>MSDN: AVIOLDINDEX struct</li>
 * </ul>
 *
 * @author katsuhiro
 */
public class RIFFHeaderIdx1 extends RIFFHeader
        implements Cloneable {
    public List<IndexEntry> entries;

    public RIFFHeaderIdx1() {
        entries = new ArrayList<>();
    }

    @Override
    public RIFFHeaderIdx1 clone()
            throws CloneNotSupportedException {
        RIFFHeaderIdx1 obj = (RIFFHeaderIdx1)super.clone();

        obj.entries = new ArrayList<>();
        for (IndexEntry e : entries) {
            obj.entries.add(e.clone());
        }

        return obj;
    }

    @Override
    public void read(PacketReader<?> c) {
        read(c, this);
    }

    public static void read(PacketReader<?> c,
                            RIFFHeaderIdx1 d) {
        c.enterBlock("idx1 chunk");

        RIFFHeader.read(c, d);

        //16 is size of IndexEntry
        int cnt = d.ckSize.intValue() / 16;

        d.entries.clear();
        for (int i = 0; i < cnt; i++) {
            IndexEntry e = new IndexEntry();
            e.read(c);
            d.entries.add(e);
        }

        c.leaveBlock();
    }

    @Override
    public void write(PacketWriter<?> c) {
        write(c, this);
    }

    public static void write(PacketWriter<?> c,
                             RIFFHeaderIdx1 d) {
        c.enterBlock("idx1 chunk");

        RIFFHeader.write(c, d);

        for (int i = 0; i < d.entries.size(); i++) {
            c.mark("entry[" + i + "]", i);
            d.entries.get(i).write(c);
        }

        c.leaveBlock();
    }
}

@@ -380,23 +380,22 @@
 xEntry%3E 
-entries
+aIndex
 ;%0A%0A    p
@@ -427,23 +427,22 @@
         
-entries
+aIndex
  = new A
@@ -631,23 +631,22 @@
     obj.
-entries
+aIndex
  = new A
@@ -687,23 +687,22 @@
 try e : 
-entries
+aIndex
 ) %7B%0A    
@@ -713,23 +713,22 @@
     obj.
-entries
+aIndex
 .add(e.c
@@ -1103,17 +1103,16 @@
 ) / 16;%0A
-%0A
         
@@ -1117,172 +1117,84 @@
   d.
-entries.clear();%0A        for (int i = 0; i %3C cnt; i++) %7B%0A            IndexEntry e = new IndexEntry();%0A            e.read(c);%0A            d.entries.add(e);%0A        %7D
+aIndex = readObjectList(c, cnt, d.aIndex,%0A                IndexEntry.class);
 %0A%0A  
@@ -1489,144 +1489,80 @@
     
-for (int i = 0; i %3C d.entries.size(); i++) %7B%0A            c.mark(%22entry%5B%22 + i + %22%5D%22, i);%0A            d.entries.get(i).write(c);%0A        %7D
+writeObjectList(c, d.aIndex.size(), d.aIndex,%0A                %22aIndex%22);
 %0A%0A  

Remove duplicated codes for processing list data.