/*
 * Copyright 2020, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.delivery;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.spine.base.EventMessage;
import io.spine.server.delivery.event.ShardProcessingRequested;
import io.spine.server.entity.Repository;
import io.spine.server.event.AbstractEventReactor;
import io.spine.server.event.React;
import io.spine.server.model.Nothing;
import io.spine.server.type.EventEnvelope;
import io.spine.type.TypeUrl;

import static io.spine.util.Exceptions.newIllegalStateException;
import static java.lang.String.format;

/**
 * The framework-internal process performing the maintenance of delivery shards.
 */
class ShardMaintenanceProcess extends AbstractEventReactor {

    static final TypeUrl TYPE = TypeUrl.of(ShardMaintenance.getDefaultInstance());

    private final Inbox<ShardIndex> inbox;

    ShardMaintenanceProcess(Delivery delivery) {
        super();
        Inbox.Builder<ShardIndex> builder = delivery.newInbox(TYPE);
        builder.addEventEndpoint(InboxLabel.REACT_UPON_EVENT, EventEndpoint::new);
        this.inbox = builder.build();
    }

    //TODO:2020-01-20:alex.tymchenko: describe the need.
    @React
    Nothing on(ShardProcessingRequested event) {
        System.out.println(
                format("Received `ShardProcessingRequested` for the shard #%d.",
                       event.getId()
                            .getIndex()));
        return nothing();
    }

    @Override
    public boolean canDispatch(EventEnvelope envelope) {
        EventMessage raw = envelope.message();
        return raw instanceof ShardEvent;
    }

    @CanIgnoreReturnValue
    @Override
    public void dispatch(EventEnvelope event) {
        ShardEvent message = (ShardEvent) event.message();
        inbox.send(event)
             .toReactor(message.getId());
    }

    private class EventEndpoint implements MessageEndpoint<ShardIndex, EventEnvelope> {

        private final EventEnvelope envelope;

        private EventEndpoint(EventEnvelope envelope) {
            this.envelope = envelope;
        }

        @Override
        public void dispatchTo(ShardIndex targetId) {
            ShardMaintenanceProcess.super.dispatch(envelope);
        }

        @Override
        public void onDuplicate(ShardIndex target, EventEnvelope envelope) {
            // do nothing.
        }

        @Override
        public Repository<ShardIndex, ?> repository() {
            throw newIllegalStateException("`ShardMaintenanceProcess` has no repository.");
        }
    }
}

@@ -1108,43 +1108,8 @@
 ue;%0A
-import io.spine.base.EventMessage;%0A
 impo
@@ -1168,16 +1168,16 @@
 uested;%0A
+
 import i
@@ -1477,47 +1477,8 @@
 ion;
-%0Aimport static java.lang.String.format;
 %0A%0A/*
@@ -2021,65 +2021,8 @@
  %7D%0A%0A
-    //TODO:2020-01-20:alex.tymchenko: describe the need.%0A
     
@@ -2089,380 +2089,24 @@
     
-System.out.println(%0A                format(%22Received %60ShardProcessingRequested%60 for the shard #%25d.%22,%0A                       event.getId()%0A                            .getIndex()));%0A        return nothing();%0A    %7D%0A%0A    @Override%0A    public boolean canDispatch(EventEnvelope envelope) %7B%0A        EventMessage raw = envelope.message();%0A        return raw instanceof ShardEvent
+return nothing()
 ;%0A  

Remove a redundant override and kill the debug logging.