/*
 * Copyright 2019, 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.stand;

import io.spine.base.EventMessage;
import io.spine.client.Subscription;
import io.spine.client.Topic;
import io.spine.system.server.EntityStateChanged;
import io.spine.type.TypeUrl;

final class SubscriptionRecordFactory {

    private SubscriptionRecordFactory() {
    }

    /**
     * ...
     *
     * <p>By default assumes that all non-event subscriptions are entity subscriptions.
     *
     * @param subscription
     * @return
     */
    static SubscriptionRecord newRecordFor(Subscription subscription) {
        TypeUrl type = getType(subscription);
        if (isEvent(type)) {
            return createEventRecord(subscription, type);
        }
        return createEntityRecord(subscription);
    }

    private static SubscriptionRecord createEntityRecord(Subscription subscription) {
        TypeUrl type = TypeUrl.of(EntityStateChanged.class);
        SubscriptionMatcher matcher = EntitySubscriptionMatcher.createFor(subscription);
        SubscriptionCallback callback = new EntitySubscriptionCallback(subscription);
        return new SubscriptionRecord(subscription, type, matcher, callback);
    }

    private static SubscriptionRecord createEventRecord(Subscription subscription, TypeUrl type) {
        SubscriptionMatcher matcher = EventSubscriptionMatcher.createFor(subscription);
        SubscriptionCallback callback = new EventSubscriptionCallback(subscription);
        return new SubscriptionRecord(subscription, type, matcher, callback);
    }

    private static TypeUrl getType(Subscription subscription) {
        Topic topic = subscription.getTopic();
        String typeAsString = topic.getTarget()
                                   .getType();
        TypeUrl result = TypeUrl.parse(typeAsString);
        return result;
    }

    private static boolean isEvent(TypeUrl type) {
        Class<?> javaClass = type.getJavaClass();
        boolean result = EventMessage.class.isAssignableFrom(javaClass);
        return result;
    }
}

@@ -1566,30 +1566,144 @@
     
-TypeUrl type = getType
+if (isEventSubscription(subscription)) %7B%0A            return createEventRecord(subscription);%0A        %7D%0A        return createEntityRecord
 (sub
@@ -1722,69 +1722,220 @@
     
+%7D%0A%0A
     
-if (isEvent(type)) %7B%0A            return createEventRecord
+private static SubscriptionRecord createEventRecord(Subscription subscription) %7B%0A        TypeUrl type = getSubscriptionType(subscription);%0A        SubscriptionMatcher matcher = new EventSubscriptionMatcher
 (sub
@@ -1943,22 +1943,16 @@
 cription
-, type
 );%0A     
@@ -1958,56 +1958,160 @@
     
-%7D%0A        return createEntityRecord(subscription
+SubscriptionCallback callback = new EventSubscriptionCallback(subscription);%0A        return new SubscriptionRecord(subscription, type, matcher, callback
 );%0A 
@@ -2300,16 +2300,20 @@
 atcher =
+ new
  EntityS
@@ -2326,34 +2326,24 @@
 ptionMatcher
-.createFor
 (subscriptio
@@ -2540,337 +2540,267 @@
 tic 
-SubscriptionRecord createEventRecord(Subscription subscription, TypeUrl type) %7B%0A        SubscriptionMatcher matcher = EventSubscriptionMatcher.createFor(subscription);%0A        SubscriptionCallback callback = new EventSubscriptionCallback(subscription);%0A        return new SubscriptionRecord(subscription, type, matcher, callback)
+boolean isEventSubscription(Subscription subscription) %7B%0A        TypeUrl type = getSubscriptionType(subscription);%0A        Class%3C?%3E javaClass = type.getJavaClass();%0A        boolean result = EventMessage.class.isAssignableFrom(javaClass);%0A        return result
 ;%0A  
@@ -2834,16 +2834,28 @@
 eUrl get
+Subscription
 Type(Sub
@@ -3108,211 +3108,7 @@
    %7D
-%0A%0A    private static boolean isEvent(TypeUrl type) %7B%0A        Class%3C?%3E javaClass = type.getJavaClass();%0A        boolean result = EventMessage.class.isAssignableFrom(javaClass);%0A        return result;%0A    %7D
 %0A%7D%0A

Reorganize methods in subscription record factory