package org.aohe.variable;

import java.util.ArrayList;
import java.util.List;

import org.aohe.core.twain.Twain;
import org.aohe.exceptions.TwainException;
import org.aohe.utils.TwainUtils;

public class TwainArray extends TwainContainer {

    int count;
    List<Object> items = new ArrayList<>();

    public TwainArray(int cap, byte[] container) {
        super(cap, container);
        count = TwainUtils.getINT32(container, 2);

        for (int i = 0, off = 6; i < count; i++) {
            items.add(getObjectAt(container, off));
            off += TYPE_SIZES[type];
        }
    }

    @Override
    public int getType() {
        return Twain.TWON_ARRAY;
    }

    @Override
    public byte[] getBytes() {
        int count = items.size();
        int len = 6 + count * TYPE_SIZES[type];
        byte[] container = new byte[len];
        TwainUtils.setINT16(container, 0, type);
        TwainUtils.setINT32(container, 2, count);

        for (int i = 0, off = 6; i < count; i++) {
            setObjectAt(container, off, items.get(i));
            off += TYPE_SIZES[type];
        }
        return container;
    }

    @Override
    public Object getCurrentValue() throws TwainException {
        throw new TwainException(getClass().getName() + ".getCurrentValue:\n\tnot applicable");
    }

    @Override
    public void setCurrentValue(Object obj) throws TwainException {
        throw new TwainException(getClass().getName() + ".setCurrentValue:\n\tnot applicable");
    }

    @Override
    public Object getDefaultValue() throws TwainException {
        throw new TwainException(getClass().getName() + ".getDefaultValue:\n\tnot applicable");
    }

    @Override
    public void setDefaultValue(Object obj) throws TwainException {
        throw new TwainException(getClass().getName() + ".setDefaultValue:\n\tnot applicable");
    }

    @Override
    public <T> T[] getItems() {
        return (T[]) items.toArray();
    }

}
