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

package com.azure.resourcemanager.operationsmanagement.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.operationsmanagement.fluent.models.ManagementConfigurationInner;
import com.azure.resourcemanager.operationsmanagement.models.ManagementConfiguration;
import com.azure.resourcemanager.operationsmanagement.models.ManagementConfigurationProperties;

public final class ManagementConfigurationImpl
    implements ManagementConfiguration, ManagementConfiguration.Definition, ManagementConfiguration.Update {
    private ManagementConfigurationInner innerObject;

    private final com.azure.resourcemanager.operationsmanagement.OperationsManagementManager 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 ManagementConfigurationProperties properties() {
        return this.innerModel().properties();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.operationsmanagement.OperationsManagementManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String managementConfigurationName;

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

    public ManagementConfiguration create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getManagementConfigurations()
                .createOrUpdateWithResponse(
                    resourceGroupName, managementConfigurationName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ManagementConfiguration create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getManagementConfigurations()
                .createOrUpdateWithResponse(resourceGroupName, managementConfigurationName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ManagementConfigurationImpl(
        String name, com.azure.resourcemanager.operationsmanagement.OperationsManagementManager serviceManager) {
        this.innerObject = new ManagementConfigurationInner();
        this.serviceManager = serviceManager;
        this.managementConfigurationName = name;
    }

    public ManagementConfigurationImpl update() {
        return this;
    }

    public ManagementConfiguration apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getManagementConfigurations()
                .createOrUpdateWithResponse(
                    resourceGroupName, managementConfigurationName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ManagementConfiguration apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getManagementConfigurations()
                .createOrUpdateWithResponse(resourceGroupName, managementConfigurationName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ManagementConfigurationImpl(
        ManagementConfigurationInner innerObject,
        com.azure.resourcemanager.operationsmanagement.OperationsManagementManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourcegroups");
        this.managementConfigurationName = Utils.getValueFromIdByName(innerObject.id(), "ManagementConfigurations");
    }

    public ManagementConfiguration refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getManagementConfigurations()
                .getByResourceGroupWithResponse(resourceGroupName, managementConfigurationName, Context.NONE)
                .getValue();
        return this;
    }

    public ManagementConfiguration refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getManagementConfigurations()
                .getByResourceGroupWithResponse(resourceGroupName, managementConfigurationName, context)
                .getValue();
        return this;
    }

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

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

    public ManagementConfigurationImpl withProperties(ManagementConfigurationProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }
}
