/*-
 * Copyright (C) 2011-2012 Erik Larsson
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.catacombae.hfs.types.hfsplus;

import java.io.PrintStream;
import java.lang.reflect.Field;
import org.catacombae.csjc.PrintableStruct;
import org.catacombae.csjc.StaticStruct;
import org.catacombae.csjc.structelements.Dictionary;
import org.catacombae.csjc.structelements.IntegerFieldRepresentation;
import org.catacombae.util.Util;

/**
 * This class was generated by CStructToJavaClass.
 *
 * @author <a href="https://catacombae.org" target="_top">Erik Larsson</a>
 */
public class HFSPlusAttributesExtents extends HFSPlusAttributesLeafRecordData
        implements StaticStruct, PrintableStruct
{
    /*
     * struct HFSPlusAttrExtents
     * size: 72 bytes
     * description:
     *
     * BP  Size  Type                 Identifier  Description
     * ------------------------------------------------------
     * 0   4     UInt32               recordType
     * 4   4     UInt32               reserved
     * 8   64    HFSPlusExtentRecord  extents
     */

    public static final int STRUCTSIZE = 72;

    private int reserved;
    private final HFSPlusExtentRecord extents;

    public HFSPlusAttributesExtents(byte[] data, int offset) {
        super(data, offset);
        reserved = Util.readIntBE(data, offset+4);
        extents = new HFSPlusExtentRecord(data, offset+8);
    }

    public static int length() { return STRUCTSIZE; }

    public int size() { return length(); }

    /**  */
    public final long getReserved() { return Util.unsign(getRawReserved()); }
    /**  */
    public final HFSPlusExtentRecord getExtents() { return extents; }

    /** <b>Note that the return value from this function should be interpreted as an unsigned integer, for instance using Util.unsign(...).</b> */
    public final int getRawReserved() { return reserved; }

    public void printFields(PrintStream ps, String prefix) {
        ps.println(prefix + " recordType: " + getRecordType());
        ps.println(prefix + " reserved: " + getReserved());
        ps.println(prefix + " extents: ");
        getExtents().print(ps, prefix + "  ");
    }

    public void print(PrintStream ps, String prefix) {
        ps.println(prefix + "HFSPlusAttrExtents:");
        printFields(ps, prefix);
    }

    public byte[] getBytes() {
        byte[] result = new byte[length()];

        getBytes(result, 0);

        return result;
    }

    @Override
    protected int getBytes(byte[] result, int offset) {
        final int originalOffset = offset;

        offset += super.getBytes(result, offset);
        Util.arrayPutBE(result, offset, reserved); offset += 4;
        {
            byte[] tempData = this.extents.getBytes();
            System.arraycopy(tempData, 0, result, offset, tempData.length);
            offset += tempData.length;
        }

        return offset - originalOffset;
    }

    @Override
    public Dictionary getStructElements() {
        final Class thisClass = HFSPlusAttributesExtents.class;
        DictionaryBuilder db = new DictionaryBuilder(thisClass.getSimpleName(),
                "HFS+ attribute extents");

        try {
            final Field reservedField = thisClass.getDeclaredField("reserved");

            reservedField.setAccessible(true);

            db.addAll(super.getStructElements());
            db.addUIntBE("reserved", reservedField, this, "Reserved",
                    IntegerFieldRepresentation.HEXADECIMAL);
            db.add("extents", this.extents.getStructElement(), "Extents");

            return db.getResult();
        }
        catch(NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }
}
