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

package com.azure.resourcemanager.security.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.security.fluent.models.JitNetworkAccessPolicyInner;
import com.azure.resourcemanager.security.fluent.models.JitNetworkAccessRequestInner;
import com.azure.resourcemanager.security.models.JitNetworkAccessPolicy;
import com.azure.resourcemanager.security.models.JitNetworkAccessPolicyVirtualMachine;
import com.azure.resourcemanager.security.models.JitNetworkAccessRequest;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public final class JitNetworkAccessPolicyImpl
    implements JitNetworkAccessPolicy, JitNetworkAccessPolicy.Definition, JitNetworkAccessPolicy.Update {
    private JitNetworkAccessPolicyInner innerObject;

    private final com.azure.resourcemanager.security.SecurityManager serviceManager;

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

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

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

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

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

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

    public List<JitNetworkAccessRequest> requests() {
        List<JitNetworkAccessRequestInner> inner = this.innerModel().requests();
        if (inner != null) {
            return Collections
                .unmodifiableList(
                    inner
                        .stream()
                        .map(inner1 -> new JitNetworkAccessRequestImpl(inner1, this.manager()))
                        .collect(Collectors.toList()));
        } else {
            return Collections.emptyList();
        }
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.security.SecurityManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String ascLocation;

    private String jitNetworkAccessPolicyName;

    public JitNetworkAccessPolicyImpl withExistingLocation(String resourceGroupName, String ascLocation) {
        this.resourceGroupName = resourceGroupName;
        this.ascLocation = ascLocation;
        return this;
    }

    public JitNetworkAccessPolicy create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJitNetworkAccessPolicies()
                .createOrUpdateWithResponse(
                    resourceGroupName, ascLocation, jitNetworkAccessPolicyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public JitNetworkAccessPolicy create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJitNetworkAccessPolicies()
                .createOrUpdateWithResponse(
                    resourceGroupName, ascLocation, jitNetworkAccessPolicyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    JitNetworkAccessPolicyImpl(String name, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = new JitNetworkAccessPolicyInner();
        this.serviceManager = serviceManager;
        this.jitNetworkAccessPolicyName = name;
    }

    public JitNetworkAccessPolicyImpl update() {
        return this;
    }

    public JitNetworkAccessPolicy apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJitNetworkAccessPolicies()
                .createOrUpdateWithResponse(
                    resourceGroupName, ascLocation, jitNetworkAccessPolicyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public JitNetworkAccessPolicy apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJitNetworkAccessPolicies()
                .createOrUpdateWithResponse(
                    resourceGroupName, ascLocation, jitNetworkAccessPolicyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    JitNetworkAccessPolicyImpl(
        JitNetworkAccessPolicyInner innerObject, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.ascLocation = Utils.getValueFromIdByName(innerObject.id(), "locations");
        this.jitNetworkAccessPolicyName = Utils.getValueFromIdByName(innerObject.id(), "jitNetworkAccessPolicies");
    }

    public JitNetworkAccessPolicy refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJitNetworkAccessPolicies()
                .getWithResponse(resourceGroupName, ascLocation, jitNetworkAccessPolicyName, Context.NONE)
                .getValue();
        return this;
    }

    public JitNetworkAccessPolicy refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJitNetworkAccessPolicies()
                .getWithResponse(resourceGroupName, ascLocation, jitNetworkAccessPolicyName, context)
                .getValue();
        return this;
    }

    public JitNetworkAccessPolicyImpl withVirtualMachines(List<JitNetworkAccessPolicyVirtualMachine> virtualMachines) {
        this.innerModel().withVirtualMachines(virtualMachines);
        return this;
    }

    public JitNetworkAccessPolicyImpl withKind(String kind) {
        this.innerModel().withKind(kind);
        return this;
    }

    public JitNetworkAccessPolicyImpl withRequests(List<JitNetworkAccessRequestInner> requests) {
        this.innerModel().withRequests(requests);
        return this;
    }
}
