// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.logic.generated;

import com.azure.core.management.serializer.SerializerFactory;
import com.azure.core.util.serializer.SerializerEncoding;
import com.azure.resourcemanager.logic.models.ResourceReference;
import com.azure.resourcemanager.logic.models.WorkflowParameter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/** Samples for Workflows CreateOrUpdate. */
public final class WorkflowsCreateOrUpdateSamples {
    /*
     * x-ms-original-file: specification/logic/resource-manager/Microsoft.Logic/stable/2019-05-01/examples/Workflows_CreateOrUpdate.json
     */
    /**
     * Sample code: Create or update a workflow.
     *
     * @param manager Entry point to LogicManager.
     */
    public static void createOrUpdateAWorkflow(com.azure.resourcemanager.logic.LogicManager manager)
        throws IOException {
        manager
            .workflows()
            .define("test-workflow")
            .withRegion("brazilsouth")
            .withExistingResourceGroup("test-resource-group")
            .withTags(mapOf())
            .withIntegrationAccount(
                new ResourceReference()
                    .withId(
                        "/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourceGroups/test-resource-group/providers/Microsoft.Logic/integrationAccounts/test-integration-account"))
            .withDefinition(
                SerializerFactory
                    .createDefaultManagementSerializerAdapter()
                    .deserialize(
                        "{\"$schema\":\"https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#\",\"actions\":{\"Find_pet_by_ID\":{\"type\":\"ApiConnection\",\"inputs\":{\"path\":\"/pet/@{encodeURIComponent('1')}\",\"method\":\"get\",\"host\":{\"connection\":{\"name\":\"@parameters('$connections')['test-custom-connector']['connectionId']\"}}},\"runAfter\":{}}},\"contentVersion\":\"1.0.0.0\",\"outputs\":{},\"parameters\":{\"$connections\":{\"type\":\"Object\",\"defaultValue\":{}}},\"triggers\":{\"manual\":{\"type\":\"Request\",\"inputs\":{\"schema\":{}},\"kind\":\"Http\"}}}",
                        Object.class,
                        SerializerEncoding.JSON))
            .withParameters(
                mapOf(
                    "$connections",
                    new WorkflowParameter()
                        .withValue(
                            SerializerFactory
                                .createDefaultManagementSerializerAdapter()
                                .deserialize(
                                    "{\"test-custom-connector\":{\"connectionId\":\"/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourceGroups/test-resource-group/providers/Microsoft.Web/connections/test-custom-connector\",\"connectionName\":\"test-custom-connector\",\"id\":\"/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/providers/Microsoft.Web/locations/brazilsouth/managedApis/test-custom-connector\"}}",
                                    Object.class,
                                    SerializerEncoding.JSON))))
            .create();
    }

    @SuppressWarnings("unchecked")
    private static <T> Map<String, T> mapOf(Object... inputs) {
        Map<String, T> map = new HashMap<>();
        for (int i = 0; i < inputs.length; i += 2) {
            String key = (String) inputs[i];
            T value = (T) inputs[i + 1];
            map.put(key, value);
        }
        return map;
    }
}
