/*
 * Copyright 2017, TeamDev Ltd. 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.integration;

import com.google.protobuf.Any;
import com.google.protobuf.Message;
import io.spine.core.BoundedContextName;
import io.spine.core.Event;
import io.spine.core.EventClass;
import io.spine.core.EventContext;
import io.spine.core.EventEnvelope;
import io.spine.core.Events;
import io.spine.protobuf.AnyPacker;
import io.spine.server.event.EventBus;
import io.spine.server.event.EventDispatcher;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * * An adapter for {@link EventBus} to use it along with {@link IntegrationBus}.
 *
 * @author Alex Tymchenko
 */
class EventBusAdapter extends BusAdapter<EventEnvelope, EventDispatcher<?>> {

    EventBusAdapter(Builder builder) {
        super(builder);
    }

    static Builder builderWith(EventBus eventBus, BoundedContextName boundedContextName) {
        checkNotNull(eventBus);
        checkNotNull(boundedContextName);
        return new Builder(eventBus, boundedContextName);
    }

    @Override
    ExternalMessageEnvelope toExternalEnvelope(ExternalMessage message) {
        final Message unpacked = AnyPacker.unpack(message.getOriginalMessage());
        final Event event = (Event) unpacked;
        final ExternalMessageEnvelope result =
                ExternalMessageEnvelope.of(message, Events.getMessage(event));
        return result;
    }

    @Override
    ExternalMessageEnvelope markExternal(ExternalMessage externalMsg) {
        final Any packedEvent = externalMsg.getOriginalMessage();
        final Event event = AnyPacker.unpack(packedEvent);
        final Event.Builder eventBuilder = event.toBuilder();
        final EventContext modifiedContext = eventBuilder.getContext()
                                                         .toBuilder()
                                                         .setExternal(true)
                                                         .build();

        final Event marked = eventBuilder.setContext(modifiedContext)
                                         .build();
        final ExternalMessage result = ExternalMessages.of(marked,
                                                           externalMsg.getBoundedContextName());
        return ExternalMessageEnvelope.of(result, Events.getMessage(event));
    }

    @Override
    boolean accepts(Class<? extends Message> messageClass) {
        return Event.class == messageClass;
    }

    @Override
    EventDispatcher<?> createDispatcher(Class<? extends Message> messageClass) {
        final DomesticEventPublisher result =
                new DomesticEventPublisher(getBoundedContextName(),
                                           getPublisherHub(),
                                           EventClass.of(messageClass));
        return result;
    }

    static class Builder extends AbstractBuilder<Builder, EventEnvelope, EventDispatcher<?>> {

        Builder(EventBus eventBus, BoundedContextName boundedContextName) {
            super(eventBus, boundedContextName);
        }

        @Override
        protected EventBusAdapter doBuild() {
            return new EventBusAdapter(this);
        }

        @Override
        protected Builder self() {
            return this;
        }
    }
}

@@ -1511,18 +1511,16 @@
 %0A%0A/**%0A *
- *
  An adap

Remove a redundant `*` symbol.