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

package com.azure.resourcemanager.eventgrid.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.eventgrid.fluent.models.PartnerDestinationInner;
import com.azure.resourcemanager.eventgrid.models.PartnerDestination;
import com.azure.resourcemanager.eventgrid.models.PartnerDestinationActivationState;
import com.azure.resourcemanager.eventgrid.models.PartnerDestinationProvisioningState;
import com.azure.resourcemanager.eventgrid.models.PartnerDestinationUpdateParameters;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;

public final class PartnerDestinationImpl
    implements PartnerDestination, PartnerDestination.Definition, PartnerDestination.Update {
    private PartnerDestinationInner innerObject;

    private final com.azure.resourcemanager.eventgrid.EventGridManager 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

    public UUID partnerRegistrationImmutableId() {
        return this.innerModel().partnerRegistrationImmutableId();
    }

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

    public OffsetDateTime expirationTimeIfNotActivatedUtc() {
        return this.innerModel().expirationTimeIfNotActivatedUtc();
    }

    public PartnerDestinationProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

    public PartnerDestinationActivationState activationState() {
        return this.innerModel().activationState();
    }

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.eventgrid.EventGridManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String partnerDestinationName;

    private PartnerDestinationUpdateParameters updatePartnerDestinationUpdateParameters;

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

    public PartnerDestination create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPartnerDestinations()
                .createOrUpdate(resourceGroupName, partnerDestinationName, this.innerModel(), Context.NONE);
        return this;
    }

    public PartnerDestination create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPartnerDestinations()
                .createOrUpdate(resourceGroupName, partnerDestinationName, this.innerModel(), context);
        return this;
    }

    PartnerDestinationImpl(String name, com.azure.resourcemanager.eventgrid.EventGridManager serviceManager) {
        this.innerObject = new PartnerDestinationInner();
        this.serviceManager = serviceManager;
        this.partnerDestinationName = name;
    }

    public PartnerDestinationImpl update() {
        this.updatePartnerDestinationUpdateParameters = new PartnerDestinationUpdateParameters();
        return this;
    }

    public PartnerDestination apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPartnerDestinations()
                .update(
                    resourceGroupName, partnerDestinationName, updatePartnerDestinationUpdateParameters, Context.NONE);
        return this;
    }

    public PartnerDestination apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPartnerDestinations()
                .update(resourceGroupName, partnerDestinationName, updatePartnerDestinationUpdateParameters, context);
        return this;
    }

    PartnerDestinationImpl(
        PartnerDestinationInner innerObject, com.azure.resourcemanager.eventgrid.EventGridManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.partnerDestinationName = Utils.getValueFromIdByName(innerObject.id(), "partnerDestinations");
    }

    public PartnerDestination refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPartnerDestinations()
                .getByResourceGroupWithResponse(resourceGroupName, partnerDestinationName, Context.NONE)
                .getValue();
        return this;
    }

    public PartnerDestination refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPartnerDestinations()
                .getByResourceGroupWithResponse(resourceGroupName, partnerDestinationName, context)
                .getValue();
        return this;
    }

    public Response<PartnerDestination> activateWithResponse(Context context) {
        return serviceManager
            .partnerDestinations()
            .activateWithResponse(resourceGroupName, partnerDestinationName, context);
    }

    public PartnerDestination activate() {
        return serviceManager.partnerDestinations().activate(resourceGroupName, partnerDestinationName);
    }

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

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

    public PartnerDestinationImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.innerModel().withTags(tags);
            return this;
        } else {
            this.updatePartnerDestinationUpdateParameters.withTags(tags);
            return this;
        }
    }

    public PartnerDestinationImpl withPartnerRegistrationImmutableId(UUID partnerRegistrationImmutableId) {
        this.innerModel().withPartnerRegistrationImmutableId(partnerRegistrationImmutableId);
        return this;
    }

    public PartnerDestinationImpl withEndpointServiceContext(String endpointServiceContext) {
        this.innerModel().withEndpointServiceContext(endpointServiceContext);
        return this;
    }

    public PartnerDestinationImpl withExpirationTimeIfNotActivatedUtc(OffsetDateTime expirationTimeIfNotActivatedUtc) {
        this.innerModel().withExpirationTimeIfNotActivatedUtc(expirationTimeIfNotActivatedUtc);
        return this;
    }

    public PartnerDestinationImpl withActivationState(PartnerDestinationActivationState activationState) {
        this.innerModel().withActivationState(activationState);
        return this;
    }

    public PartnerDestinationImpl withEndpointBaseUrl(String endpointBaseUrl) {
        this.innerModel().withEndpointBaseUrl(endpointBaseUrl);
        return this;
    }

    public PartnerDestinationImpl withMessageForActivation(String messageForActivation) {
        this.innerModel().withMessageForActivation(messageForActivation);
        return this;
    }

    private boolean isInCreateMode() {
        return this.innerModel().id() == null;
    }
}
