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

package com.azure.resourcemanager.providerhub.implementation;

import com.azure.core.util.Context;
import com.azure.resourcemanager.providerhub.fluent.models.CustomRolloutInner;
import com.azure.resourcemanager.providerhub.models.CustomRollout;
import com.azure.resourcemanager.providerhub.models.CustomRolloutProperties;

public final class CustomRolloutImpl implements CustomRollout, CustomRollout.Definition, CustomRollout.Update {
    private CustomRolloutInner innerObject;

    private final com.azure.resourcemanager.providerhub.ProviderHubManager serviceManager;

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

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

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

    public CustomRolloutProperties properties() {
        return this.innerModel().properties();
    }

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

    private com.azure.resourcemanager.providerhub.ProviderHubManager manager() {
        return this.serviceManager;
    }

    private String providerNamespace;

    private String rolloutName;

    public CustomRolloutImpl withExistingProviderRegistration(String providerNamespace) {
        this.providerNamespace = providerNamespace;
        return this;
    }

    public CustomRollout create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomRollouts()
                .createOrUpdateWithResponse(providerNamespace, rolloutName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public CustomRollout create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomRollouts()
                .createOrUpdateWithResponse(providerNamespace, rolloutName, this.innerModel(), context)
                .getValue();
        return this;
    }

    CustomRolloutImpl(String name, com.azure.resourcemanager.providerhub.ProviderHubManager serviceManager) {
        this.innerObject = new CustomRolloutInner();
        this.serviceManager = serviceManager;
        this.rolloutName = name;
    }

    public CustomRolloutImpl update() {
        return this;
    }

    public CustomRollout apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomRollouts()
                .createOrUpdateWithResponse(providerNamespace, rolloutName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public CustomRollout apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomRollouts()
                .createOrUpdateWithResponse(providerNamespace, rolloutName, this.innerModel(), context)
                .getValue();
        return this;
    }

    CustomRolloutImpl(
        CustomRolloutInner innerObject, com.azure.resourcemanager.providerhub.ProviderHubManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.providerNamespace = Utils.getValueFromIdByName(innerObject.id(), "providerRegistrations");
        this.rolloutName = Utils.getValueFromIdByName(innerObject.id(), "customRollouts");
    }

    public CustomRollout refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomRollouts()
                .getWithResponse(providerNamespace, rolloutName, Context.NONE)
                .getValue();
        return this;
    }

    public CustomRollout refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomRollouts()
                .getWithResponse(providerNamespace, rolloutName, context)
                .getValue();
        return this;
    }

    public CustomRolloutImpl withProperties(CustomRolloutProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }
}
