/*
 * Copyright (C) 2020-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.hedera.node.app.service.mono.fees.calculation.consensus.queries;

import static com.hedera.node.app.hapi.utils.fee.ConsensusServiceFeeBuilder.computeVariableSizedFieldsUsage;
import static com.hedera.node.app.hapi.utils.fee.FeeBuilder.BASIC_ENTITY_ID_SIZE;
import static com.hedera.node.app.hapi.utils.fee.FeeBuilder.BASIC_QUERY_HEADER;
import static com.hedera.node.app.hapi.utils.fee.FeeBuilder.BASIC_QUERY_RES_HEADER;
import static com.hedera.node.app.hapi.utils.fee.FeeBuilder.LONG_SIZE;
import static com.hedera.node.app.hapi.utils.fee.FeeBuilder.TX_HASH_SIZE;
import static com.hedera.node.app.hapi.utils.fee.FeeBuilder.getQueryFeeDataMatrices;
import static com.hedera.node.app.hapi.utils.fee.FeeBuilder.getStateProofSize;
import static com.hedera.node.app.service.mono.utils.EntityNum.fromTopicId;
import static com.hedera.node.app.service.mono.utils.MiscUtils.asKeyUnchecked;

import com.hedera.node.app.service.mono.context.primitives.StateView;
import com.hedera.node.app.service.mono.fees.calculation.QueryResourceUsageEstimator;
import com.hedera.node.app.service.mono.state.merkle.MerkleTopic;
import com.hederahashgraph.api.proto.java.FeeComponents;
import com.hederahashgraph.api.proto.java.FeeData;
import com.hederahashgraph.api.proto.java.Query;
import com.hederahashgraph.api.proto.java.ResponseType;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class GetTopicInfoResourceUsage implements QueryResourceUsageEstimator {
    @Inject
    public GetTopicInfoResourceUsage() {
        /* No-op */
    }

    @Override
    public boolean applicableTo(final Query query) {
        return query.hasConsensusGetTopicInfo();
    }

    @Override
    public FeeData usageGiven(final Query query, final StateView view, final Map<String, Object> ignoreCtx) {
        return usageGivenType(
                query, view, query.getConsensusGetTopicInfo().getHeader().getResponseType());
    }

    @Override
    public FeeData usageGivenType(final Query query, final StateView view, final ResponseType responseType) {
        final var merkleTopic =
                view.topics().get(fromTopicId(query.getConsensusGetTopicInfo().getTopicID()));
        return usageGivenTypeAndTopic(merkleTopic, responseType);
    }

    public FeeData usageGivenTypeAndTopic(@Nullable final MerkleTopic topic, final ResponseType responseType) {
        if (topic == null) {
            return FeeData.getDefaultInstance();
        }

        final long bpr = BASIC_QUERY_RES_HEADER
                + getStateProofSize(responseType)
                + BASIC_ENTITY_ID_SIZE
                + getTopicInfoSize(topic);
        final var feeMatrices = FeeComponents.newBuilder()
                .setBpt(BASIC_QUERY_HEADER + BASIC_ENTITY_ID_SIZE)
                .setVpt(0)
                .setRbh(0)
                .setSbh(0)
                .setGas(0)
                .setTv(0)
                .setBpr(bpr)
                .setSbpr(0)
                .build();
        return getQueryFeeDataMatrices(feeMatrices);
    }

    private static int getTopicInfoSize(final MerkleTopic merkleTopic) {
        /* Three longs in a topic representation: sequenceNumber, expirationTime, autoRenewPeriod */
        return TX_HASH_SIZE
                + 3 * LONG_SIZE
                + computeVariableSizedFieldsUsage(
                        asKeyUnchecked(merkleTopic.getAdminKey()),
                        asKeyUnchecked(merkleTopic.getSubmitKey()),
                        merkleTopic.getMemo(),
                        merkleTopic.hasAutoRenewAccountId());
    }
}
