package skadistats.clarity.model;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class Entity {

    private final int index;
    private final int serial;
    private final DTClass dtClass;
    private PVS pvs;
    private final Object[] state;

    public Entity(int index, int serial, DTClass dtClass, PVS pvs, Object[] state) {
        this.index = index;
        this.serial = serial;
        this.dtClass = dtClass;
        this.pvs = pvs;
        this.state = state;
    }

    public int getIndex() {
        return index;
    }

    public int getSerial() {
        return serial;
    }
    
    public int getHandle() {
        return Handle.forIndexAndSerial(index, serial);
    }

    public DTClass getDtClass() {
        return dtClass;
    }

    public PVS getPvs() {
        return pvs;
    }

    public void setPvs(PVS pvs) {
        this.pvs = pvs;
    }

    public Object[] getState() {
        return state;
    }

    @SuppressWarnings("unchecked")
    public <T> T getProperty(String property) {
        Integer index = dtClass.getPropertyIndex(property);
        if (index == null) {
            throw new IllegalArgumentException(String.format("property %s not found on entity of class %s", property, getDtClass().getDtName()));
        }
        return (T) state[index.intValue()];
    }

    @SuppressWarnings("unchecked")
    public <T> T[] getArrayProperty(Class<T> clazz, String property) {
        List<T> result = new ArrayList<T>();
        int i = 0;
        while (true) {
            Integer idx = dtClass.getPropertyIndex(property + String.format(".%04d", i));
            if (idx == null) {
                break;
            }
            result.add((T) state[idx.intValue()]);
            i++;
        }
        return (T[]) result.toArray((T[]) Array.newInstance(clazz, 0));
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("\n\n-- Entity [index=");
        builder.append(index);
        builder.append(", serial=");
        builder.append(serial);
        builder.append(", dtClass=");
        builder.append(dtClass.getDtName());
        builder.append("]");
        for (int i = 0; i < state.length; i++) {
            builder.append("\n");
            builder.append(dtClass.getReceiveProps()[i].getVarName());
            builder.append(" = ");
            builder.append(state[i]);
        }

        return builder.toString();
    }

}

@@ -30,16 +30,61 @@
 del;%0D%0A%0D%0A
+import skadistats.clarity.util.TextTable;%0D%0A%0D%0A
 import j
@@ -2041,42 +2041,36 @@
     
-StringBuilder builder = new String
+TextTable t = new TextTable.
 Buil
@@ -2074,17 +2074,16 @@
 uilder()
-;
 %0D%0A      
@@ -2088,80 +2088,98 @@
     
-builder.append(%22%5Cn%5Cn-- Entity %5Bindex=%22);%0D%0A        builder.append(index);
+    .setTitle(%22idx: %22 + index + %22, serial: %22 + serial + %22, class: %22 + dtClass.getDtName())
 %0D%0A  
@@ -2188,108 +2188,141 @@
     
+ 
-builder.append(%22, serial=%22);%0D%0A        builder.append(serial);%0D%0A        builder.append(%22, dtClass=
+   .setFrame(TextTable.FRAME_COMPAT)%0D%0A            .addColumn(%22Index%22, TextTable.Alignment.RIGHT)%0D%0A            .addColumn(%22Property
 %22)
-;
 %0D%0A  
@@ -2331,54 +2331,45 @@
     
-builder.append(dtClass.getDtName());%0D%0A
+    .addColumn(%22Value%22)%0D%0A    
         
 buil
@@ -2368,26 +2368,15 @@
     
+.
 build
-er.append(%22%5D%22
+(
 );%0D%0A
@@ -2441,58 +2441,57 @@
     
-builder.append(%22%5Cn%22);%0D%0A            builder.append(
+t.setData(i, 0, i);%0D%0A            t.setData(i, 1, 
 dtCl
@@ -2547,59 +2547,24 @@
     
-builder.append(%22 = %22);%0D%0A            builder.append(
+t.setData(i, 2, 
 stat
@@ -2578,26 +2578,24 @@
 %0A        %7D%0D%0A
-%0D%0A
         retu
@@ -2601,15 +2601,9 @@
 urn 
-builder
+t
 .toS

use TextTable for toString()-methos