package org.aohe.variable;

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

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

public class TwainEnumeration extends TwainContainer {

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

    public TwainEnumeration(int cap, byte[] container) {
        super(cap, container);
        count = TwainUtils.getINT32(container, 2);
        currentIndex = TwainUtils.getINT32(container, 6);
        defaultIndex = TwainUtils.getINT32(container, 10);

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

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

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

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

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

    @Override
    public Object getCurrentValue() throws TwainException {
        return items.get(currentIndex);
    }

    @Override
    public void setCurrentValue(Object obj) throws TwainException {
        int count = items.size();
        for (int i = 0; i < count; i++) {
            Object item = items.get(i);
            if (obj.equals(item)) {
                currentIndex = i;
                return;
            }
        }
        throw new TwainException(getClass().getName() + ".setCurrentValue:\n\tCould not find " + obj.toString());
    }

    @Override
    public Object getDefaultValue() throws TwainException {
        return items.get(defaultIndex);
    }

    @Override
    public void setDefaultValue(Object obj) throws TwainException {
        int count = items.size();
        for (int i = 0; i < count; i++) {
            Object item = items.get(i);
            if (obj.equals(item)) {
                defaultIndex = i;
                return;
            }
        }
        throw new TwainException(getClass().getName() + ".setDefaultValue:\n\tCould not find " + obj.toString());
    }

}
