/*-
 * Copyright (C) 2021 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.hfsexplorer.types.dsstore;

import java.io.PrintStream;
import org.catacombae.csjc.DynamicStruct;
import org.catacombae.util.Util;

/** This class was generated by CStructToJavaClass. */
public class DSStoreTableOfContentsEntry implements DynamicStruct {
    /*
     * struct DSStoreTableOfContentsEntry
     * size: 260 bytes
     * description:
     *
     * BP   Size     Type       Identifier     Description
     * ---------------------------------------------------
     * 0    1        u8         tocNameLength
     * 1    1 * ?    char[?]    tocName
     * ?    4        be32       tocValue
     */

    public static final int MAX_STRUCTSIZE = 260;
    public static final int MIN_STRUCTSIZE = 5;

    private byte tocNameLength;
    private final byte[] tocName;
    private int tocValue;

    public DSStoreTableOfContentsEntry(byte[] data, int offset) {
        this.tocNameLength = Util.readByteBE(data, offset + 0);
        this.tocName = new byte[this.tocNameLength];
        System.arraycopy(data, offset + 1, this.tocName, 0, this.tocNameLength);
        this.tocValue = Util.readIntBE(data, offset + 1 + this.tocNameLength);
    }

    public int maxSize() {
        return MAX_STRUCTSIZE;
    }

    public int occupiedSize() {
        return MIN_STRUCTSIZE + this.tocNameLength;
    }

    /**  */
    public final short getTocNameLength() {
        return Util.unsign(getRawTocNameLength());
    }

    /**  */
    public final byte[] getTocName() {
        return Util.readByteArrayBE(this.tocName);
    }

    /**  */
    public final long getTocValue() {
        return Util.unsign(getRawTocValue());
    }

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

    /**
     * <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 getRawTocValue() {
        return this.tocValue;
    }

    public void printFields(PrintStream ps, String prefix) {
        ps.println(prefix + " tocNameLength: " + getTocNameLength());
        ps.println(prefix + " tocName: " + Util.toASCIIString(getTocName()));
        ps.println(prefix + " tocValue: " + getTocValue());
    }

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

    public byte[] getBytes() {
        byte[] result = new byte[occupiedSize()];
        getBytes(result, 0);
        return result;
    }

    public int getBytes(byte[] result, int offset) {
        final int startOffset = offset;

        Util.arrayPutBE(result, offset, this.tocNameLength); offset += 1;
        System.arraycopy(this.tocName, 0, result, offset, this.tocName.length); offset += this.tocName.length;
        Util.arrayPutBE(result, offset, this.tocValue); offset += 4;

        return offset - startOffset;
    }
}
