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

package com.azure.resourcemanager.labservices.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.labservices.fluent.models.UserInner;
import com.azure.resourcemanager.labservices.models.InvitationState;
import com.azure.resourcemanager.labservices.models.InviteBody;
import com.azure.resourcemanager.labservices.models.ProvisioningState;
import com.azure.resourcemanager.labservices.models.RegistrationState;
import com.azure.resourcemanager.labservices.models.User;
import com.azure.resourcemanager.labservices.models.UserUpdate;
import java.time.Duration;
import java.time.OffsetDateTime;

public final class UserImpl implements User, User.Definition, User.Update {
    private UserInner innerObject;

    private final com.azure.resourcemanager.labservices.LabServicesManager 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 ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

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

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

    public RegistrationState registrationState() {
        return this.innerModel().registrationState();
    }

    public InvitationState invitationState() {
        return this.innerModel().invitationState();
    }

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

    public Duration totalUsage() {
        return this.innerModel().totalUsage();
    }

    public Duration additionalUsageQuota() {
        return this.innerModel().additionalUsageQuota();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.labservices.LabServicesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String labName;

    private String username;

    private UserUpdate updateBody;

    public UserImpl withExistingLab(String resourceGroupName, String labName) {
        this.resourceGroupName = resourceGroupName;
        this.labName = labName;
        return this;
    }

    public User create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUsers()
                .createOrUpdate(resourceGroupName, labName, username, this.innerModel(), Context.NONE);
        return this;
    }

    public User create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUsers()
                .createOrUpdate(resourceGroupName, labName, username, this.innerModel(), context);
        return this;
    }

    UserImpl(String name, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerObject = new UserInner();
        this.serviceManager = serviceManager;
        this.username = name;
    }

    public UserImpl update() {
        this.updateBody = new UserUpdate();
        return this;
    }

    public User apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUsers()
                .update(resourceGroupName, labName, username, updateBody, Context.NONE);
        return this;
    }

    public User apply(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getUsers().update(resourceGroupName, labName, username, updateBody, context);
        return this;
    }

    UserImpl(UserInner innerObject, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.labName = Utils.getValueFromIdByName(innerObject.id(), "labs");
        this.username = Utils.getValueFromIdByName(innerObject.id(), "users");
    }

    public User refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUsers()
                .getWithResponse(resourceGroupName, labName, username, Context.NONE)
                .getValue();
        return this;
    }

    public User refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUsers()
                .getWithResponse(resourceGroupName, labName, username, context)
                .getValue();
        return this;
    }

    public void invite(InviteBody body) {
        serviceManager.users().invite(resourceGroupName, labName, username, body);
    }

    public void invite(InviteBody body, Context context) {
        serviceManager.users().invite(resourceGroupName, labName, username, body, context);
    }

    public UserImpl withEmail(String email) {
        this.innerModel().withEmail(email);
        return this;
    }

    public UserImpl withAdditionalUsageQuota(Duration additionalUsageQuota) {
        if (isInCreateMode()) {
            this.innerModel().withAdditionalUsageQuota(additionalUsageQuota);
            return this;
        } else {
            this.updateBody.withAdditionalUsageQuota(additionalUsageQuota);
            return this;
        }
    }

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