/*-
 * 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 DSStoreTableOfContents implements DynamicStruct {
    /*
     * struct DSStoreTableOfContents
     * size: ? bytes
     * description:
     *
     * BP  Size       Type                            Identifier  Description
     * ----------------------------------------------------------------------
     * 0   4          be32                            tocCount
     * 4   ?          DSStoreTableOfContentsEntry[?]  tocEntries
     */

    public static final int MIN_STRUCTSIZE = 4;
    public static final int MAX_STRUCTSIZE = Integer.MAX_VALUE;

    private int tocCount;
    private final DSStoreTableOfContentsEntry[] tocEntries;

    public DSStoreTableOfContents(byte[] data, int offset) {
        this.tocCount = Util.readIntBE(data, offset + 0);
        offset += 4;
        this.tocEntries = new DSStoreTableOfContentsEntry[this.tocCount];
        for(int _i = 0; _i < this.tocCount; ++_i) {
            this.tocEntries[_i] = new DSStoreTableOfContentsEntry(data, offset);
            offset += this.tocEntries[_i].occupiedSize();
        }
    }

    public int maxSize() {
        return MAX_STRUCTSIZE;
    }

    public int occupiedSize() {
        int size = MIN_STRUCTSIZE;
        for(int i = 0; i < this.tocCount; ++i) {
            size += this.tocEntries[i].occupiedSize();
        }
        return size;
    }

    /**  */
    public final long getTocCount() {
        return Util.unsign(getRawTocCount());
    }

    /**  */
    public final DSStoreTableOfContentsEntry getTocEntry(int i) {
        return this.tocEntries[i];
    }

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

    public void printFields(PrintStream ps, String prefix) {
        ps.println(prefix + " tocCount: " + getTocCount());
        ps.println(prefix + " tocEntries: ");

        for(int _i = 0; _i < 1; ++_i) {
            ps.println(prefix + "   [" + _i + "]:");
            getTocEntry(_i).print(ps, prefix + "     ");
        }
    }

    public void print(PrintStream ps, String prefix) {
        ps.println(prefix + "DSStoreTableOfContents:");
        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.tocCount); offset += 4;
        for(int _i = 0; _i < this.tocEntries.length; ++_i) {
            offset += this.tocEntries[_i].getBytes(result, offset);
        }

        return offset - startOffset;
    }
}
