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

package com.azure.resourcemanager.sphere.implementation;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.sphere.fluent.models.CatalogInner;
import com.azure.resourcemanager.sphere.models.Catalog;
import com.azure.resourcemanager.sphere.models.CatalogUpdate;
import com.azure.resourcemanager.sphere.models.CountDeviceResponse;
import com.azure.resourcemanager.sphere.models.Deployment;
import com.azure.resourcemanager.sphere.models.Device;
import com.azure.resourcemanager.sphere.models.DeviceGroup;
import com.azure.resourcemanager.sphere.models.DeviceInsight;
import com.azure.resourcemanager.sphere.models.ListDeviceGroupsRequest;
import com.azure.resourcemanager.sphere.models.ProvisioningState;
import java.util.Collections;
import java.util.Map;

public final class CatalogImpl implements Catalog, Catalog.Definition, Catalog.Update {
    private CatalogInner innerObject;

    private final com.azure.resourcemanager.sphere.AzureSphereManager 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

    public ProvisioningState 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 CatalogInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.sphere.AzureSphereManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String catalogName;

    private CatalogUpdate updateProperties;

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

    public Catalog create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCatalogs()
                .createOrUpdate(resourceGroupName, catalogName, this.innerModel(), Context.NONE);
        return this;
    }

    public Catalog create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCatalogs()
                .createOrUpdate(resourceGroupName, catalogName, this.innerModel(), context);
        return this;
    }

    CatalogImpl(String name, com.azure.resourcemanager.sphere.AzureSphereManager serviceManager) {
        this.innerObject = new CatalogInner();
        this.serviceManager = serviceManager;
        this.catalogName = name;
    }

    public CatalogImpl update() {
        this.updateProperties = new CatalogUpdate();
        return this;
    }

    public Catalog apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCatalogs()
                .updateWithResponse(resourceGroupName, catalogName, updateProperties, Context.NONE)
                .getValue();
        return this;
    }

    public Catalog apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCatalogs()
                .updateWithResponse(resourceGroupName, catalogName, updateProperties, context)
                .getValue();
        return this;
    }

    CatalogImpl(CatalogInner innerObject, com.azure.resourcemanager.sphere.AzureSphereManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.catalogName = Utils.getValueFromIdByName(innerObject.id(), "catalogs");
    }

    public Catalog refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCatalogs()
                .getByResourceGroupWithResponse(resourceGroupName, catalogName, Context.NONE)
                .getValue();
        return this;
    }

    public Catalog refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCatalogs()
                .getByResourceGroupWithResponse(resourceGroupName, catalogName, context)
                .getValue();
        return this;
    }

    public Response<CountDeviceResponse> countDevicesWithResponse(Context context) {
        return serviceManager.catalogs().countDevicesWithResponse(resourceGroupName, catalogName, context);
    }

    public CountDeviceResponse countDevices() {
        return serviceManager.catalogs().countDevices(resourceGroupName, catalogName);
    }

    public PagedIterable<Deployment> listDeployments() {
        return serviceManager.catalogs().listDeployments(resourceGroupName, catalogName);
    }

    public PagedIterable<Deployment> listDeployments(
        String filter, Integer top, Integer skip, Integer maxpagesize, Context context) {
        return serviceManager
            .catalogs()
            .listDeployments(resourceGroupName, catalogName, filter, top, skip, maxpagesize, context);
    }

    public PagedIterable<DeviceGroup> listDeviceGroups(ListDeviceGroupsRequest listDeviceGroupsRequest) {
        return serviceManager.catalogs().listDeviceGroups(resourceGroupName, catalogName, listDeviceGroupsRequest);
    }

    public PagedIterable<DeviceGroup> listDeviceGroups(
        ListDeviceGroupsRequest listDeviceGroupsRequest,
        String filter,
        Integer top,
        Integer skip,
        Integer maxpagesize,
        Context context) {
        return serviceManager
            .catalogs()
            .listDeviceGroups(
                resourceGroupName, catalogName, listDeviceGroupsRequest, filter, top, skip, maxpagesize, context);
    }

    public PagedIterable<DeviceInsight> listDeviceInsights() {
        return serviceManager.catalogs().listDeviceInsights(resourceGroupName, catalogName);
    }

    public PagedIterable<DeviceInsight> listDeviceInsights(
        String filter, Integer top, Integer skip, Integer maxpagesize, Context context) {
        return serviceManager
            .catalogs()
            .listDeviceInsights(resourceGroupName, catalogName, filter, top, skip, maxpagesize, context);
    }

    public PagedIterable<Device> listDevices() {
        return serviceManager.catalogs().listDevices(resourceGroupName, catalogName);
    }

    public PagedIterable<Device> listDevices(
        String filter, Integer top, Integer skip, Integer maxpagesize, Context context) {
        return serviceManager
            .catalogs()
            .listDevices(resourceGroupName, catalogName, filter, top, skip, maxpagesize, context);
    }

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

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

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

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