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

package com.azure.resourcemanager.storagepool.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.storagepool.fluent.models.DiskPoolInner;
import com.azure.resourcemanager.storagepool.fluent.models.Sku;
import com.azure.resourcemanager.storagepool.models.Disk;
import com.azure.resourcemanager.storagepool.models.DiskPool;
import com.azure.resourcemanager.storagepool.models.DiskPoolCreate;
import com.azure.resourcemanager.storagepool.models.DiskPoolUpdate;
import com.azure.resourcemanager.storagepool.models.OperationalStatus;
import com.azure.resourcemanager.storagepool.models.ProvisioningStates;
import com.azure.resourcemanager.storagepool.models.SystemMetadata;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class DiskPoolImpl implements DiskPool, DiskPool.Definition, DiskPool.Update {
    private DiskPoolInner innerObject;

    private final com.azure.resourcemanager.storagepool.StoragePoolManager 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 String managedBy() {
        return this.innerModel().managedBy();
    }

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

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

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

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

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

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

    public OperationalStatus status() {
        return this.innerModel().status();
    }

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.storagepool.StoragePoolManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String diskPoolName;

    private DiskPoolCreate createDiskPoolCreatePayload;

    private DiskPoolUpdate updateDiskPoolUpdatePayload;

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

    public DiskPool create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDiskPools()
                .createOrUpdate(resourceGroupName, diskPoolName, createDiskPoolCreatePayload, Context.NONE);
        return this;
    }

    public DiskPool create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDiskPools()
                .createOrUpdate(resourceGroupName, diskPoolName, createDiskPoolCreatePayload, context);
        return this;
    }

    DiskPoolImpl(String name, com.azure.resourcemanager.storagepool.StoragePoolManager serviceManager) {
        this.innerObject = new DiskPoolInner();
        this.serviceManager = serviceManager;
        this.diskPoolName = name;
        this.createDiskPoolCreatePayload = new DiskPoolCreate();
    }

    public DiskPoolImpl update() {
        this.updateDiskPoolUpdatePayload = new DiskPoolUpdate();
        return this;
    }

    public DiskPool apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDiskPools()
                .update(resourceGroupName, diskPoolName, updateDiskPoolUpdatePayload, Context.NONE);
        return this;
    }

    public DiskPool apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDiskPools()
                .update(resourceGroupName, diskPoolName, updateDiskPoolUpdatePayload, context);
        return this;
    }

    DiskPoolImpl(DiskPoolInner innerObject, com.azure.resourcemanager.storagepool.StoragePoolManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.diskPoolName = Utils.getValueFromIdByName(innerObject.id(), "diskPools");
    }

    public DiskPool refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDiskPools()
                .getByResourceGroupWithResponse(resourceGroupName, diskPoolName, Context.NONE)
                .getValue();
        return this;
    }

    public DiskPool refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDiskPools()
                .getByResourceGroupWithResponse(resourceGroupName, diskPoolName, context)
                .getValue();
        return this;
    }

    public void start() {
        serviceManager.diskPools().start(resourceGroupName, diskPoolName);
    }

    public void start(Context context) {
        serviceManager.diskPools().start(resourceGroupName, diskPoolName, context);
    }

    public void deallocate() {
        serviceManager.diskPools().deallocate(resourceGroupName, diskPoolName);
    }

    public void deallocate(Context context) {
        serviceManager.diskPools().deallocate(resourceGroupName, diskPoolName, context);
    }

    public void upgrade() {
        serviceManager.diskPools().upgrade(resourceGroupName, diskPoolName);
    }

    public void upgrade(Context context) {
        serviceManager.diskPools().upgrade(resourceGroupName, diskPoolName, context);
    }

    public DiskPoolImpl withRegion(Region location) {
        this.createDiskPoolCreatePayload.withLocation(location.toString());
        return this;
    }

    public DiskPoolImpl withRegion(String location) {
        this.createDiskPoolCreatePayload.withLocation(location);
        return this;
    }

    public DiskPoolImpl withSku(Sku sku) {
        if (isInCreateMode()) {
            this.createDiskPoolCreatePayload.withSku(sku);
            return this;
        } else {
            this.updateDiskPoolUpdatePayload.withSku(sku);
            return this;
        }
    }

    public DiskPoolImpl withSubnetId(String subnetId) {
        this.createDiskPoolCreatePayload.withSubnetId(subnetId);
        return this;
    }

    public DiskPoolImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.createDiskPoolCreatePayload.withTags(tags);
            return this;
        } else {
            this.updateDiskPoolUpdatePayload.withTags(tags);
            return this;
        }
    }

    public DiskPoolImpl withManagedBy(String managedBy) {
        if (isInCreateMode()) {
            this.createDiskPoolCreatePayload.withManagedBy(managedBy);
            return this;
        } else {
            this.updateDiskPoolUpdatePayload.withManagedBy(managedBy);
            return this;
        }
    }

    public DiskPoolImpl withManagedByExtended(List<String> managedByExtended) {
        if (isInCreateMode()) {
            this.createDiskPoolCreatePayload.withManagedByExtended(managedByExtended);
            return this;
        } else {
            this.updateDiskPoolUpdatePayload.withManagedByExtended(managedByExtended);
            return this;
        }
    }

    public DiskPoolImpl withAvailabilityZones(List<String> availabilityZones) {
        this.createDiskPoolCreatePayload.withAvailabilityZones(availabilityZones);
        return this;
    }

    public DiskPoolImpl withDisks(List<Disk> disks) {
        if (isInCreateMode()) {
            this.createDiskPoolCreatePayload.withDisks(disks);
            return this;
        } else {
            this.updateDiskPoolUpdatePayload.withDisks(disks);
            return this;
        }
    }

    public DiskPoolImpl withAdditionalCapabilities(List<String> additionalCapabilities) {
        this.createDiskPoolCreatePayload.withAdditionalCapabilities(additionalCapabilities);
        return this;
    }

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