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

package com.azure.resourcemanager.communication.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.communication.fluent.models.SenderUsernameResourceInner;
import com.azure.resourcemanager.communication.models.ProvisioningState;
import com.azure.resourcemanager.communication.models.SenderUsernameResource;

public final class SenderUsernameResourceImpl
    implements SenderUsernameResource, SenderUsernameResource.Definition, SenderUsernameResource.Update {
    private SenderUsernameResourceInner innerObject;

    private final com.azure.resourcemanager.communication.CommunicationManager serviceManager;

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

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

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

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.communication.CommunicationManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String emailServiceName;

    private String domainName;

    private String senderUsername;

    public SenderUsernameResourceImpl withExistingDomain(
        String resourceGroupName, String emailServiceName, String domainName) {
        this.resourceGroupName = resourceGroupName;
        this.emailServiceName = emailServiceName;
        this.domainName = domainName;
        return this;
    }

    public SenderUsernameResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSenderUsernames()
                .createOrUpdateWithResponse(
                    resourceGroupName, emailServiceName, domainName, senderUsername, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public SenderUsernameResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSenderUsernames()
                .createOrUpdateWithResponse(
                    resourceGroupName, emailServiceName, domainName, senderUsername, this.innerModel(), context)
                .getValue();
        return this;
    }

    SenderUsernameResourceImpl(
        String name, com.azure.resourcemanager.communication.CommunicationManager serviceManager) {
        this.innerObject = new SenderUsernameResourceInner();
        this.serviceManager = serviceManager;
        this.senderUsername = name;
    }

    public SenderUsernameResourceImpl update() {
        return this;
    }

    public SenderUsernameResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSenderUsernames()
                .createOrUpdateWithResponse(
                    resourceGroupName, emailServiceName, domainName, senderUsername, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public SenderUsernameResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSenderUsernames()
                .createOrUpdateWithResponse(
                    resourceGroupName, emailServiceName, domainName, senderUsername, this.innerModel(), context)
                .getValue();
        return this;
    }

    SenderUsernameResourceImpl(
        SenderUsernameResourceInner innerObject,
        com.azure.resourcemanager.communication.CommunicationManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.emailServiceName = Utils.getValueFromIdByName(innerObject.id(), "emailServices");
        this.domainName = Utils.getValueFromIdByName(innerObject.id(), "domains");
        this.senderUsername = Utils.getValueFromIdByName(innerObject.id(), "senderUsernames");
    }

    public SenderUsernameResource refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSenderUsernames()
                .getWithResponse(resourceGroupName, emailServiceName, domainName, senderUsername, Context.NONE)
                .getValue();
        return this;
    }

    public SenderUsernameResource refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSenderUsernames()
                .getWithResponse(resourceGroupName, emailServiceName, domainName, senderUsername, context)
                .getValue();
        return this;
    }

    public SenderUsernameResourceImpl withUsername(String username) {
        this.innerModel().withUsername(username);
        return this;
    }

    public SenderUsernameResourceImpl withDisplayName(String displayName) {
        this.innerModel().withDisplayName(displayName);
        return this;
    }
}
