/*
 * Copyright (C) 2016-2023 Hedera Hashgraph, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.swirlds.common.merkle.synchronization.views;

import static com.swirlds.common.constructable.ClassIdFormatter.classIdString;

import com.swirlds.common.constructable.ConstructableRegistry;
import com.swirlds.common.crypto.CryptographyHolder;
import com.swirlds.common.crypto.Hash;
import com.swirlds.common.io.streams.SerializableDataInputStream;
import com.swirlds.common.merkle.MerkleInternal;
import com.swirlds.common.merkle.MerkleNode;
import com.swirlds.common.merkle.synchronization.internal.ExpectedLesson;
import com.swirlds.common.merkle.synchronization.utility.MerkleSynchronizationException;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Implementation for a view of a standard in memory merkle tree.
 */
public class StandardLearnerTreeView implements LearnerTreeView<MerkleNode> {

    private final MerkleNode originalRoot;

    private final Queue<ExpectedLesson<MerkleNode>> expectedLessons;
    private final LinkedList<MerkleInternal> nodesToInitialize;

    /**
     * Create a new standard tree view out of an in-memory merkle tree (or subtree).
     *
     * @param root
     * 		the root of the tree (or subtree)
     */
    public StandardLearnerTreeView(final MerkleNode root) {
        this.originalRoot = root;
        expectedLessons = new LinkedList<>();
        nodesToInitialize = new LinkedList<>();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isRootOfState() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MerkleNode getOriginalRoot() {
        return originalRoot;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isInternal(final MerkleNode node, final boolean isOriginal) {
        // This implementation can safely ignore "isOriginal"
        return node != null && !node.isLeaf();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getNumberOfChildren(final MerkleNode node) {
        if (node == null || node.isLeaf()) {
            throw new MerkleSynchronizationException("only internal nodes have a child count");
        }

        return node.asInternal().getNumberOfChildren();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Hash getNodeHash(final MerkleNode node) {
        if (node == null) {
            return CryptographyHolder.get().getNullHash();
        } else {
            return node.getHash();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getClassId(final MerkleNode node) {
        if (node == null) {
            throw new MerkleSynchronizationException("null does not have a class ID");
        }

        return node.getClassId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MerkleNode convertMerkleRootToViewType(final MerkleNode node) {
        return node;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MerkleNode getMerkleRoot(final MerkleNode node) {
        return node;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MerkleNode getChild(final MerkleNode parent, final int childIndex) {
        if (parent == null || parent.isLeaf()) {
            throw new MerkleSynchronizationException("can not get child of leaf node");
        }
        return parent.asInternal().getChild(childIndex);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setChild(final MerkleNode parent, final int childIndex, final MerkleNode child) {
        parent.asInternal().setChild(childIndex, child);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void expectLessonFor(
            final MerkleNode parent,
            final int childIndex,
            final MerkleNode original,
            final boolean nodeAlreadyPresent) {
        expectedLessons.add(new ExpectedLesson<>(parent, childIndex, original, nodeAlreadyPresent));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ExpectedLesson<MerkleNode> getNextExpectedLesson() {
        return expectedLessons.remove();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean hasNextExpectedLesson() {
        return !expectedLessons.isEmpty();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MerkleNode deserializeLeaf(final SerializableDataInputStream in) throws IOException {
        return in.readSerializable();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MerkleNode deserializeInternal(final SerializableDataInputStream in) throws IOException {

        final long classId = in.readLong();

        final MerkleInternal internal = ConstructableRegistry.getInstance().createObject(classId);
        if (internal == null) {
            throw new MerkleSynchronizationException(
                    "unable to construct object with class ID " + classIdString(classId));
        }

        return internal;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void markForInitialization(final MerkleNode node) {
        nodesToInitialize.add(node.asInternal());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void initialize() {
        // Internal nodes are encountered top down, but need to be initialized bottom up.
        final Iterator<MerkleInternal> iterator = nodesToInitialize.descendingIterator();

        while (iterator.hasNext()) {
            iterator.next().rebuild();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void releaseNode(final MerkleNode node) {
        if (node != null) {
            node.release();
        }
    }
}
