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

package com.azure.resourcemanager.notificationhubs.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.notificationhubs.fluent.models.NotificationHubResourceInner;
import com.azure.resourcemanager.notificationhubs.fluent.models.SharedAccessAuthorizationRuleProperties;
import com.azure.resourcemanager.notificationhubs.models.AdmCredential;
import com.azure.resourcemanager.notificationhubs.models.ApnsCredential;
import com.azure.resourcemanager.notificationhubs.models.BaiduCredential;
import com.azure.resourcemanager.notificationhubs.models.DebugSendResponse;
import com.azure.resourcemanager.notificationhubs.models.GcmCredential;
import com.azure.resourcemanager.notificationhubs.models.MpnsCredential;
import com.azure.resourcemanager.notificationhubs.models.NotificationHubCreateOrUpdateParameters;
import com.azure.resourcemanager.notificationhubs.models.NotificationHubPatchParameters;
import com.azure.resourcemanager.notificationhubs.models.NotificationHubResource;
import com.azure.resourcemanager.notificationhubs.models.PnsCredentialsResource;
import com.azure.resourcemanager.notificationhubs.models.Sku;
import com.azure.resourcemanager.notificationhubs.models.WnsCredential;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class NotificationHubResourceImpl
    implements NotificationHubResource, NotificationHubResource.Definition, NotificationHubResource.Update {
    private NotificationHubResourceInner innerObject;

    private final com.azure.resourcemanager.notificationhubs.NotificationHubsManager 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 Sku sku() {
        return this.innerModel().sku();
    }

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

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

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

    public ApnsCredential apnsCredential() {
        return this.innerModel().apnsCredential();
    }

    public WnsCredential wnsCredential() {
        return this.innerModel().wnsCredential();
    }

    public GcmCredential gcmCredential() {
        return this.innerModel().gcmCredential();
    }

    public MpnsCredential mpnsCredential() {
        return this.innerModel().mpnsCredential();
    }

    public AdmCredential admCredential() {
        return this.innerModel().admCredential();
    }

    public BaiduCredential baiduCredential() {
        return this.innerModel().baiduCredential();
    }

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

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

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

    private com.azure.resourcemanager.notificationhubs.NotificationHubsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String namespaceName;

    private String notificationHubName;

    private NotificationHubCreateOrUpdateParameters createParameters;

    private NotificationHubPatchParameters updateParameters;

    public NotificationHubResourceImpl withExistingNamespace(String resourceGroupName, String namespaceName) {
        this.resourceGroupName = resourceGroupName;
        this.namespaceName = namespaceName;
        return this;
    }

    public NotificationHubResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNotificationHubs()
                .createOrUpdateWithResponse(
                    resourceGroupName, namespaceName, notificationHubName, createParameters, Context.NONE)
                .getValue();
        return this;
    }

    public NotificationHubResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNotificationHubs()
                .createOrUpdateWithResponse(
                    resourceGroupName, namespaceName, notificationHubName, createParameters, context)
                .getValue();
        return this;
    }

    NotificationHubResourceImpl(
        String name, com.azure.resourcemanager.notificationhubs.NotificationHubsManager serviceManager) {
        this.innerObject = new NotificationHubResourceInner();
        this.serviceManager = serviceManager;
        this.notificationHubName = name;
        this.createParameters = new NotificationHubCreateOrUpdateParameters();
    }

    public NotificationHubResourceImpl update() {
        this.updateParameters = new NotificationHubPatchParameters();
        return this;
    }

    public NotificationHubResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNotificationHubs()
                .patchWithResponse(
                    resourceGroupName, namespaceName, notificationHubName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public NotificationHubResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNotificationHubs()
                .patchWithResponse(resourceGroupName, namespaceName, notificationHubName, updateParameters, context)
                .getValue();
        return this;
    }

    NotificationHubResourceImpl(
        NotificationHubResourceInner innerObject,
        com.azure.resourcemanager.notificationhubs.NotificationHubsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.namespaceName = Utils.getValueFromIdByName(innerObject.id(), "namespaces");
        this.notificationHubName = Utils.getValueFromIdByName(innerObject.id(), "notificationHubs");
    }

    public NotificationHubResource refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNotificationHubs()
                .getWithResponse(resourceGroupName, namespaceName, notificationHubName, Context.NONE)
                .getValue();
        return this;
    }

    public NotificationHubResource refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNotificationHubs()
                .getWithResponse(resourceGroupName, namespaceName, notificationHubName, context)
                .getValue();
        return this;
    }

    public DebugSendResponse debugSend() {
        return serviceManager.notificationHubs().debugSend(resourceGroupName, namespaceName, notificationHubName);
    }

    public Response<DebugSendResponse> debugSendWithResponse(Object parameters, Context context) {
        return serviceManager
            .notificationHubs()
            .debugSendWithResponse(resourceGroupName, namespaceName, notificationHubName, parameters, context);
    }

    public PnsCredentialsResource getPnsCredentials() {
        return serviceManager
            .notificationHubs()
            .getPnsCredentials(resourceGroupName, namespaceName, notificationHubName);
    }

    public Response<PnsCredentialsResource> getPnsCredentialsWithResponse(Context context) {
        return serviceManager
            .notificationHubs()
            .getPnsCredentialsWithResponse(resourceGroupName, namespaceName, notificationHubName, context);
    }

    public NotificationHubResourceImpl withRegion(Region location) {
        this.createParameters.withLocation(location.toString());
        return this;
    }

    public NotificationHubResourceImpl withRegion(String location) {
        this.createParameters.withLocation(location);
        return this;
    }

    public NotificationHubResourceImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.createParameters.withTags(tags);
            return this;
        } else {
            this.updateParameters.withTags(tags);
            return this;
        }
    }

    public NotificationHubResourceImpl withSku(Sku sku) {
        if (isInCreateMode()) {
            this.createParameters.withSku(sku);
            return this;
        } else {
            this.updateParameters.withSku(sku);
            return this;
        }
    }

    public NotificationHubResourceImpl withNamePropertiesName(String namePropertiesName) {
        if (isInCreateMode()) {
            this.createParameters.withNamePropertiesName(namePropertiesName);
            return this;
        } else {
            this.updateParameters.withNamePropertiesName(namePropertiesName);
            return this;
        }
    }

    public NotificationHubResourceImpl withRegistrationTtl(String registrationTtl) {
        if (isInCreateMode()) {
            this.createParameters.withRegistrationTtl(registrationTtl);
            return this;
        } else {
            this.updateParameters.withRegistrationTtl(registrationTtl);
            return this;
        }
    }

    public NotificationHubResourceImpl withAuthorizationRules(
        List<SharedAccessAuthorizationRuleProperties> authorizationRules) {
        if (isInCreateMode()) {
            this.createParameters.withAuthorizationRules(authorizationRules);
            return this;
        } else {
            this.updateParameters.withAuthorizationRules(authorizationRules);
            return this;
        }
    }

    public NotificationHubResourceImpl withApnsCredential(ApnsCredential apnsCredential) {
        if (isInCreateMode()) {
            this.createParameters.withApnsCredential(apnsCredential);
            return this;
        } else {
            this.updateParameters.withApnsCredential(apnsCredential);
            return this;
        }
    }

    public NotificationHubResourceImpl withWnsCredential(WnsCredential wnsCredential) {
        if (isInCreateMode()) {
            this.createParameters.withWnsCredential(wnsCredential);
            return this;
        } else {
            this.updateParameters.withWnsCredential(wnsCredential);
            return this;
        }
    }

    public NotificationHubResourceImpl withGcmCredential(GcmCredential gcmCredential) {
        if (isInCreateMode()) {
            this.createParameters.withGcmCredential(gcmCredential);
            return this;
        } else {
            this.updateParameters.withGcmCredential(gcmCredential);
            return this;
        }
    }

    public NotificationHubResourceImpl withMpnsCredential(MpnsCredential mpnsCredential) {
        if (isInCreateMode()) {
            this.createParameters.withMpnsCredential(mpnsCredential);
            return this;
        } else {
            this.updateParameters.withMpnsCredential(mpnsCredential);
            return this;
        }
    }

    public NotificationHubResourceImpl withAdmCredential(AdmCredential admCredential) {
        if (isInCreateMode()) {
            this.createParameters.withAdmCredential(admCredential);
            return this;
        } else {
            this.updateParameters.withAdmCredential(admCredential);
            return this;
        }
    }

    public NotificationHubResourceImpl withBaiduCredential(BaiduCredential baiduCredential) {
        if (isInCreateMode()) {
            this.createParameters.withBaiduCredential(baiduCredential);
            return this;
        } else {
            this.updateParameters.withBaiduCredential(baiduCredential);
            return this;
        }
    }

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