package org.aohe.variable;


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


public class TwainRange extends TwainContainer {

    private final Object minValue;
    private final Object maxValue;
    private final Object stepSize;
    private Object defaultValue;
    private Object currentValue;

    public TwainRange(int cap, byte[] container) {
        super(cap, container);
        minValue = get32BitObjectAt(container, 2);
        maxValue = get32BitObjectAt(container, 6);
        stepSize = get32BitObjectAt(container, 10);
        defaultValue = get32BitObjectAt(container, 14);
        currentValue = get32BitObjectAt(container, 18);
    }

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

    @Override
    public byte[] getBytes() {
        byte[] container = new byte[22];
        TwainUtils.setINT16(container, 0, type);
        set32BitObjectAt(container, 2, minValue);
        set32BitObjectAt(container, 6, maxValue);
        set32BitObjectAt(container, 10, stepSize);
        set32BitObjectAt(container, 14, defaultValue);
        set32BitObjectAt(container, 18, currentValue);
        return container;
    }

    @Override
    public Object getCurrentValue() throws TwainException {
        return currentValue;
    }

    @Override
    public void setCurrentValue(Object obj) throws TwainException {
        currentValue = obj;
    }

    @Override
    public Object getDefaultValue() throws TwainException {
        return defaultValue;
    }

    @Override
    public void setDefaultValue(Object obj) throws TwainException {
        defaultValue = obj;
    }

    @Override
    public Object[] getItems() {
        Object[] items = new Object[1];
        items[0] = currentValue;
        return items;
    }

}
