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

package com.azure.resourcemanager.security.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.security.fluent.models.AutomationInner;
import com.azure.resourcemanager.security.models.Automation;
import com.azure.resourcemanager.security.models.AutomationAction;
import com.azure.resourcemanager.security.models.AutomationScope;
import com.azure.resourcemanager.security.models.AutomationSource;
import com.azure.resourcemanager.security.models.AutomationValidationStatus;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class AutomationImpl implements Automation, Automation.Definition, Automation.Update {
    private AutomationInner innerObject;

    private final com.azure.resourcemanager.security.SecurityManager serviceManager;

    public String id() {
        return this.innerModel().id();
    }

    public String name() {
        return this.innerModel().name();
    }

    public String type() {
        return this.innerModel().type();
    }

    public String location() {
        return this.innerModel().location();
    }

    public Map<String, String> tags() {
        Map<String, String> inner = this.innerModel().tags();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

    public String description() {
        return this.innerModel().description();
    }

    public Boolean isEnabled() {
        return this.innerModel().isEnabled();
    }

    public List<AutomationScope> scopes() {
        List<AutomationScope> inner = this.innerModel().scopes();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public List<AutomationSource> sources() {
        List<AutomationSource> inner = this.innerModel().sources();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public List<AutomationAction> actions() {
        List<AutomationAction> inner = this.innerModel().actions();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public Region region() {
        return Region.fromName(this.regionName());
    }

    public String regionName() {
        return this.location();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

    public AutomationInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.security.SecurityManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String automationName;

    public AutomationImpl withExistingResourceGroup(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

    public Automation create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAutomations()
                .createOrUpdateWithResponse(resourceGroupName, automationName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Automation create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAutomations()
                .createOrUpdateWithResponse(resourceGroupName, automationName, this.innerModel(), context)
                .getValue();
        return this;
    }

    AutomationImpl(String name, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = new AutomationInner();
        this.serviceManager = serviceManager;
        this.automationName = name;
    }

    public AutomationImpl update() {
        return this;
    }

    public Automation apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAutomations()
                .createOrUpdateWithResponse(resourceGroupName, automationName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Automation apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAutomations()
                .createOrUpdateWithResponse(resourceGroupName, automationName, this.innerModel(), context)
                .getValue();
        return this;
    }

    AutomationImpl(AutomationInner innerObject, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.automationName = Utils.getValueFromIdByName(innerObject.id(), "automations");
    }

    public Automation refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAutomations()
                .getByResourceGroupWithResponse(resourceGroupName, automationName, Context.NONE)
                .getValue();
        return this;
    }

    public Automation refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAutomations()
                .getByResourceGroupWithResponse(resourceGroupName, automationName, context)
                .getValue();
        return this;
    }

    public Response<AutomationValidationStatus> validateWithResponse(AutomationInner automation, Context context) {
        return serviceManager
            .automations()
            .validateWithResponse(resourceGroupName, automationName, automation, context);
    }

    public AutomationValidationStatus validate(AutomationInner automation) {
        return serviceManager.automations().validate(resourceGroupName, automationName, automation);
    }

    public AutomationImpl withRegion(Region location) {
        this.innerModel().withLocation(location.toString());
        return this;
    }

    public AutomationImpl withRegion(String location) {
        this.innerModel().withLocation(location);
        return this;
    }

    public AutomationImpl withTags(Map<String, String> tags) {
        this.innerModel().withTags(tags);
        return this;
    }

    public AutomationImpl withDescription(String description) {
        this.innerModel().withDescription(description);
        return this;
    }

    public AutomationImpl withIsEnabled(Boolean isEnabled) {
        this.innerModel().withIsEnabled(isEnabled);
        return this;
    }

    public AutomationImpl withScopes(List<AutomationScope> scopes) {
        this.innerModel().withScopes(scopes);
        return this;
    }

    public AutomationImpl withSources(List<AutomationSource> sources) {
        this.innerModel().withSources(sources);
        return this;
    }

    public AutomationImpl withActions(List<AutomationAction> actions) {
        this.innerModel().withActions(actions);
        return this;
    }
}
