/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 accord.messages;

import javax.annotation.Nullable;

import accord.api.Data;
import accord.local.Command;
import accord.local.Node;
import accord.local.SafeCommandStore;
import accord.primitives.Participants;
import accord.primitives.Ranges;
import accord.primitives.Timestamp;
import accord.primitives.TxnId;
import accord.topology.Topologies;
import accord.utils.Invariants;

import static accord.messages.MessageType.StandardMessage.WAIT_UNTIL_APPLIED_REQ;
import static accord.primitives.SaveStatus.Applied;
import static accord.primitives.SaveStatus.Vestigial;

/**
 * Wait until the transaction has been applied locally
 */
public class WaitUntilApplied extends ReadData
{
    public static class SerializerSupport
    {
        public static WaitUntilApplied create(TxnId txnId, Participants<?> scope, long minEpoch, long executeAtEpoch)
        {
            return new WaitUntilApplied(txnId, scope, minEpoch, null, executeAtEpoch);
        }
    }

    private static final ExecuteOn EXECUTE_ON = new ExecuteOn(Applied, Vestigial);
    private final long minEpoch;
    private long retryInLaterEpoch;

    public WaitUntilApplied(Node.Id to, Topologies topologies, TxnId txnId, Participants<?> scope, long executeAtEpoch)
    {
        this(to, topologies, txnId, scope, null, executeAtEpoch);
    }

    public WaitUntilApplied(Node.Id to, Topologies topologies, TxnId txnId, Participants<?> scope, @Nullable Timestamp executeAt, long executeAtEpoch)
    {
        super(to, topologies, txnId, scope, null, executeAt, executeAtEpoch);
        this.minEpoch = topologies.oldestEpoch();
        Invariants.require(minEpoch <= executeAtEpoch);
    }

    protected WaitUntilApplied(TxnId txnId, Participants<?> scope, long minEpoch, @Nullable Timestamp executeAt, long executeAtEpoch)
    {
        super(txnId, scope, null, executeAt, executeAtEpoch);
        this.minEpoch = minEpoch;
    }

    @Override
    public long minEpoch()
    {
        return minEpoch;
    }

    @Override
    protected ExecuteOn executeOn()
    {
        return EXECUTE_ON;
    }

    @Override
    public ReadType kind()
    {
        return ReadType.waitUntilApplied;
    }

    @Override
    protected void read(SafeCommandStore safeStore, Command command)
    {
        long retryInLaterEpoch = ReadEphemeralTxnData.retryInLaterEpoch(executeAtEpoch, safeStore, command);
        if (retryInLaterEpoch > this.retryInLaterEpoch)
            this.retryInLaterEpoch = retryInLaterEpoch;
        onOneSuccess(safeStore.commandStore().id(), unavailable(safeStore, command));
    }

    @Override
    protected void reply(Ranges unavailable, Data data, long uniqueHlc)
    {
        if (data != null) data.validateReply(txnId, executeAt, uniqueHlc);
        ReadReply reply = retryInLaterEpoch > 0 ? new ReadOkWithFutureEpoch(unavailable, data, retryInLaterEpoch)
                                                : new ReadOk(unavailable, data, uniqueHlc);
        reply(reply, null);
    }

    @Override
    public MessageType type()
    {
        return WAIT_UNTIL_APPLIED_REQ;
    }

    @Override
    public String toString()
    {
        return "WaitUntilApplied{" +
               "txnId:" + txnId +
               '}';
    }
}
