/*
 * 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.tools.protojs;

import io.spine.tools.gradle.SpinePlugin;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;

import java.io.File;
import java.nio.file.Path;

import static io.spine.tools.gradle.TaskName.COMPILE_PROTO_TO_JS;
import static io.spine.tools.gradle.TaskName.COPY_MODULE_SOURCES;
import static io.spine.tools.gradle.TaskName.GENERATE_FROM_JSON;
import static io.spine.tools.protojs.ProtoFromJsonWriter.createFor;
import static io.spine.tools.protojs.files.ProjectFiles.mainDescriptorSetFile;
import static io.spine.tools.protojs.files.ProjectFiles.mainProtoJsLocation;
import static io.spine.tools.protojs.files.ProjectFiles.testDescriptorSetFile;
import static io.spine.tools.protojs.files.ProjectFiles.testProtoJsLocation;

public class ProtoJsPlugin extends SpinePlugin {

    @SuppressWarnings({"CheckReturnValue", "ResultOfMethodCallIgnored"})
    // Method called to apply task.
    @Override
    public void apply(Project project) {
        Action<Task> task = newAction(project);
        newTask(GENERATE_FROM_JSON, task)
                .insertAfterTask(COMPILE_PROTO_TO_JS)
                .insertBeforeTask(COPY_MODULE_SOURCES)
                .applyNowTo(project);
    }

    private static Action<Task> newAction(Project project) {
        return task -> generateFromJsonForProto(project);
    }

    private static void generateFromJsonForProto(Project project) {
        fromJsonForMain(project);
        fromJsonForTest(project);
    }

    private static void fromJsonForMain(Project project) {
        Path protoJsLocation = mainProtoJsLocation(project);
        File descriptorSetFile = mainDescriptorSetFile(project);
        fromJsonFor(protoJsLocation, descriptorSetFile);
    }

    private static void fromJsonForTest(Project project) {
        Path protoJsLocation = testProtoJsLocation(project);
        File descriptorSetFile = testDescriptorSetFile(project);
        fromJsonFor(protoJsLocation, descriptorSetFile);
    }

    private static void fromJsonFor(Path protoJsLocation, File descriptorSetFile) {
        ProtoFromJsonWriter writer = createFor(protoJsLocation, descriptorSetFile);
        if (writer.hasFilesToProcess()) {
            writer.writeFromJsonForProtos();
        }
    }
}

@@ -2458,24 +2458,24 @@
         
-fromJson
+generate
 ForMain(
@@ -2492,24 +2492,24 @@
         
-fromJson
+generate
 ForTest(
@@ -2549,24 +2549,24 @@
 ic void 
-fromJson
+generate
 ForMain(
@@ -2714,32 +2714,32 @@
 t);%0A        
-fromJson
+generate
 For(protoJsL
@@ -2798,24 +2798,24 @@
 ic void 
-fromJson
+generate
 ForTest(
@@ -2967,24 +2967,24 @@
         
-fromJson
+generate
 For(prot
@@ -3051,16 +3051,16 @@
 oid 
-fromJson
+generate
 For(

Fix naming