/*
 * Copyright 2018, 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.base;

import com.google.protobuf.DescriptorProtos.DescriptorProto;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Message;
import io.spine.protobuf.Messages;

import java.util.List;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkState;
import static io.spine.base.UuidValue.FIELD_NAME;
import static io.spine.util.Exceptions.newIllegalArgumentException;
import static io.spine.util.Preconditions2.checkNotEmptyOrBlank;

/**
 * A factory for creating UUID-based identifiers of the given {@link Message} type.
 *
 * <p>The created messages must contain a single {@code string} field named 'uuid'.
 *
 * @param <I>
 *         the type of created messages
 */
class UuidFactory<I extends Message> {

    private final Class<I> idClass;
    private final FieldDescriptor uuidField;

    private UuidFactory(Class<I> idClass, FieldDescriptor uuidField) {
        this.idClass = idClass;
        this.uuidField = uuidField;
    }

    /**
     * Creates a new factory for the specified {@code Message} class.
     *
     * @param idClass
     *         the class of the Protobuf message
     * @param <I>
     *         the type of the Protobuf message
     * @return a new factory instance
     * @throws IllegalStateException
     *         if a passed ID class does not obey {@link UuidValue} contract
     */
    static <I extends Message> UuidFactory<I> forClass(Class<I> idClass) {
        Descriptor message = Messages.newInstance(idClass)
                                     .getDescriptorForType();
        checkState(isUuidValue(message),
                   "A UUID message should have a single string field named %s.", FIELD_NAME);
        List<FieldDescriptor> fields = message.getFields();
        FieldDescriptor uuidField = fields.get(0);
        return new UuidFactory<>(idClass, uuidField);
    }

    /**
     * Generates an instance of the UUID message using a random string.
     *
     * @return a message instance with the initialized {@code uuid} field
     */
    I newUuid() {
        return newUuidOf(Identifier.newUuid());
    }

    /**
     * Creates an instance of the UUID message from the passed value.
     *
     * @param value
     *         a value to use
     * @return a new instance with the {@code uuid} field initialized to the given value
     */
    @SuppressWarnings("unchecked") // It is OK as the builder is obtained by the specified class.
    I newUuidOf(String value) {
        checkIsUuid(value);
        Message initializedId = Messages.builderFor(idClass)
                                        .setField(uuidField, value)
                                        .build();
        return (I) initializedId;
    }

    private static boolean isUuidValue(Descriptor message) {
        DescriptorProto messageProto = message.toProto();
        return new UuidValue.Matcher().test(messageProto);
    }

    /**
     * Checks that the given value is UUID-based.
     *
     * <p>The check utilizes the Standard Java {@code UUID}
     * {@linkplain java.util.UUID#fromString(String) check on construction}.
     *
     * @throws IllegalArgumentException
     *         if the passed value is not a valid UUID string
     */
    @SuppressWarnings({"CheckReturnValue", "ResultOfMethodCallIgnored"})
    // Just verify that object is constructed without errors.
    private static void checkIsUuid(String value) {
        checkNotEmptyOrBlank(value);
        try {
            UUID.fromString(value);
        } catch (NumberFormatException e) {
            throw newIllegalArgumentException(e, "Invalid UUID string: %s", value);
        }
    }
}

@@ -1840,16 +1840,139 @@
 age%3E %7B%0A%0A
+    private static final String ERROR_MESSAGE =%0A            %22A UUID message should have a single string field named %25s.%22;%0A%0A
     priv
@@ -2798,103 +2798,39 @@
 Uuid
-Value(message),%0A                   %22A UUID message should have a single string field named %25s.%22
+Message(message), ERROR_MESSAGE
 , FI
@@ -3897,20 +3897,22 @@
 n isUuid
-Valu
+Messag
 e(Descri

Refactor checking for UUID messages