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

package com.azure.resourcemanager.videoanalyzer.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.videoanalyzer.fluent.models.PrivateEndpointConnectionInner;
import com.azure.resourcemanager.videoanalyzer.fluent.models.VideoAnalyzerInner;
import com.azure.resourcemanager.videoanalyzer.models.AccountEncryption;
import com.azure.resourcemanager.videoanalyzer.models.Endpoint;
import com.azure.resourcemanager.videoanalyzer.models.IotHub;
import com.azure.resourcemanager.videoanalyzer.models.NetworkAccessControl;
import com.azure.resourcemanager.videoanalyzer.models.PrivateEndpointConnection;
import com.azure.resourcemanager.videoanalyzer.models.ProvisioningState;
import com.azure.resourcemanager.videoanalyzer.models.PublicNetworkAccess;
import com.azure.resourcemanager.videoanalyzer.models.StorageAccount;
import com.azure.resourcemanager.videoanalyzer.models.VideoAnalyzer;
import com.azure.resourcemanager.videoanalyzer.models.VideoAnalyzerIdentity;
import com.azure.resourcemanager.videoanalyzer.models.VideoAnalyzerUpdate;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class VideoAnalyzerImpl implements VideoAnalyzer, VideoAnalyzer.Definition, VideoAnalyzer.Update {
    private VideoAnalyzerInner innerObject;

    private final com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager 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 VideoAnalyzerIdentity identity() {
        return this.innerModel().identity();
    }

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

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

    public AccountEncryption encryption() {
        return this.innerModel().encryption();
    }

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

    public PublicNetworkAccess publicNetworkAccess() {
        return this.innerModel().publicNetworkAccess();
    }

    public NetworkAccessControl networkAccessControl() {
        return this.innerModel().networkAccessControl();
    }

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String accountName;

    private VideoAnalyzerUpdate updateParameters;

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

    public VideoAnalyzer create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVideoAnalyzers()
                .createOrUpdate(resourceGroupName, accountName, this.innerModel(), Context.NONE);
        return this;
    }

    public VideoAnalyzer create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVideoAnalyzers()
                .createOrUpdate(resourceGroupName, accountName, this.innerModel(), context);
        return this;
    }

    VideoAnalyzerImpl(String name, com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager serviceManager) {
        this.innerObject = new VideoAnalyzerInner();
        this.serviceManager = serviceManager;
        this.accountName = name;
    }

    public VideoAnalyzerImpl update() {
        this.updateParameters = new VideoAnalyzerUpdate();
        return this;
    }

    public VideoAnalyzer apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVideoAnalyzers()
                .update(resourceGroupName, accountName, updateParameters, Context.NONE);
        return this;
    }

    public VideoAnalyzer apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVideoAnalyzers()
                .update(resourceGroupName, accountName, updateParameters, context);
        return this;
    }

    VideoAnalyzerImpl(
        VideoAnalyzerInner innerObject, com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.accountName = Utils.getValueFromIdByName(innerObject.id(), "videoAnalyzers");
    }

    public VideoAnalyzer refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVideoAnalyzers()
                .getByResourceGroupWithResponse(resourceGroupName, accountName, Context.NONE)
                .getValue();
        return this;
    }

    public VideoAnalyzer refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVideoAnalyzers()
                .getByResourceGroupWithResponse(resourceGroupName, accountName, context)
                .getValue();
        return this;
    }

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

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

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

    public VideoAnalyzerImpl withIdentity(VideoAnalyzerIdentity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateParameters.withIdentity(identity);
            return this;
        }
    }

    public VideoAnalyzerImpl withStorageAccounts(List<StorageAccount> storageAccounts) {
        if (isInCreateMode()) {
            this.innerModel().withStorageAccounts(storageAccounts);
            return this;
        } else {
            this.updateParameters.withStorageAccounts(storageAccounts);
            return this;
        }
    }

    public VideoAnalyzerImpl withEncryption(AccountEncryption encryption) {
        if (isInCreateMode()) {
            this.innerModel().withEncryption(encryption);
            return this;
        } else {
            this.updateParameters.withEncryption(encryption);
            return this;
        }
    }

    public VideoAnalyzerImpl withIotHubs(List<IotHub> iotHubs) {
        if (isInCreateMode()) {
            this.innerModel().withIotHubs(iotHubs);
            return this;
        } else {
            this.updateParameters.withIotHubs(iotHubs);
            return this;
        }
    }

    public VideoAnalyzerImpl withPublicNetworkAccess(PublicNetworkAccess publicNetworkAccess) {
        if (isInCreateMode()) {
            this.innerModel().withPublicNetworkAccess(publicNetworkAccess);
            return this;
        } else {
            this.updateParameters.withPublicNetworkAccess(publicNetworkAccess);
            return this;
        }
    }

    public VideoAnalyzerImpl withNetworkAccessControl(NetworkAccessControl networkAccessControl) {
        if (isInCreateMode()) {
            this.innerModel().withNetworkAccessControl(networkAccessControl);
            return this;
        } else {
            this.updateParameters.withNetworkAccessControl(networkAccessControl);
            return this;
        }
    }

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