/*-
 * 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 HFSPlusAttributesForkData extends HFSPlusAttributesLeafRecordData
        implements StaticStruct, PrintableStruct
{
    /*
     * struct HFSPlusAttrForkData
     * size: 88 bytes
     * description:
     *
     * BP  Size  Type             Identifier  Description
     * --------------------------------------------------
     * 0   4     UInt32           recordType
     * 4   4     UInt32           reserved
     * 8   80    HFSPlusForkData  theFork
     */

    public static final int STRUCTSIZE = 88;

    private int reserved;
    private final HFSPlusForkData theFork;

    public HFSPlusAttributesForkData(byte[] data, int offset) {
        super(data, offset);
        reserved = Util.readIntBE(data, offset+4);
        theFork = new HFSPlusForkData(data, offset+8);
    }

    public static int length() { return STRUCTSIZE; }

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

    /**  */
    public final long getReserved() { return Util.unsign(getRawReserved()); }
    /**  */
    public final HFSPlusForkData getTheFork() { return theFork; }

    /** <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 + " theFork: ");
        getTheFork().print(ps, prefix + "  ");
    }

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

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

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

        return result;
    }

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

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

            reservedField.setAccessible(true);

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

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