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

package com.azure.resourcemanager.managednetworkfabric.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.managednetworkfabric.fluent.models.NetworkInterfaceInner;
import com.azure.resourcemanager.managednetworkfabric.models.EnabledDisabledState;
import com.azure.resourcemanager.managednetworkfabric.models.InterfaceStatus;
import com.azure.resourcemanager.managednetworkfabric.models.InterfaceType;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkInterface;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkInterfacePatch;
import com.azure.resourcemanager.managednetworkfabric.models.ProvisioningState;
import com.azure.resourcemanager.managednetworkfabric.models.UpdateAdministrativeState;

public final class NetworkInterfaceImpl
    implements NetworkInterface, NetworkInterface.Definition, NetworkInterface.Update {
    private NetworkInterfaceInner innerObject;

    private final com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager 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 physicalIdentifier() {
        return this.innerModel().physicalIdentifier();
    }

    public EnabledDisabledState administrativeState() {
        return this.innerModel().administrativeState();
    }

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

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

    public InterfaceType interfaceType() {
        return this.innerModel().interfaceType();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String networkDeviceName;

    private String networkInterfaceName;

    private NetworkInterfacePatch updateBody;

    public NetworkInterfaceImpl withExistingNetworkDevice(String resourceGroupName, String networkDeviceName) {
        this.resourceGroupName = resourceGroupName;
        this.networkDeviceName = networkDeviceName;
        return this;
    }

    public NetworkInterface create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkInterfaces()
                .create(resourceGroupName, networkDeviceName, networkInterfaceName, this.innerModel(), Context.NONE);
        return this;
    }

    public NetworkInterface create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkInterfaces()
                .create(resourceGroupName, networkDeviceName, networkInterfaceName, this.innerModel(), context);
        return this;
    }

    NetworkInterfaceImpl(
        String name, com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = new NetworkInterfaceInner();
        this.serviceManager = serviceManager;
        this.networkInterfaceName = name;
    }

    public NetworkInterfaceImpl update() {
        this.updateBody = new NetworkInterfacePatch();
        return this;
    }

    public NetworkInterface apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkInterfaces()
                .update(resourceGroupName, networkDeviceName, networkInterfaceName, updateBody, Context.NONE);
        return this;
    }

    public NetworkInterface apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkInterfaces()
                .update(resourceGroupName, networkDeviceName, networkInterfaceName, updateBody, context);
        return this;
    }

    NetworkInterfaceImpl(
        NetworkInterfaceInner innerObject,
        com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.networkDeviceName = Utils.getValueFromIdByName(innerObject.id(), "networkDevices");
        this.networkInterfaceName = Utils.getValueFromIdByName(innerObject.id(), "networkInterfaces");
    }

    public NetworkInterface refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkInterfaces()
                .getWithResponse(resourceGroupName, networkDeviceName, networkInterfaceName, Context.NONE)
                .getValue();
        return this;
    }

    public NetworkInterface refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkInterfaces()
                .getWithResponse(resourceGroupName, networkDeviceName, networkInterfaceName, context)
                .getValue();
        return this;
    }

    public InterfaceStatus getStatus() {
        return serviceManager.networkInterfaces().getStatus(resourceGroupName, networkDeviceName, networkInterfaceName);
    }

    public InterfaceStatus getStatus(Context context) {
        return serviceManager
            .networkInterfaces()
            .getStatus(resourceGroupName, networkDeviceName, networkInterfaceName, context);
    }

    public void updateAdministrativeState(UpdateAdministrativeState body) {
        serviceManager
            .networkInterfaces()
            .updateAdministrativeState(resourceGroupName, networkDeviceName, networkInterfaceName, body);
    }

    public void updateAdministrativeState(UpdateAdministrativeState body, Context context) {
        serviceManager
            .networkInterfaces()
            .updateAdministrativeState(resourceGroupName, networkDeviceName, networkInterfaceName, body, context);
    }

    public NetworkInterfaceImpl withAnnotation(String annotation) {
        if (isInCreateMode()) {
            this.innerModel().withAnnotation(annotation);
            return this;
        } else {
            this.updateBody.withAnnotation(annotation);
            return this;
        }
    }

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