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

package com.azure.resourcemanager.devcenter.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.devcenter.fluent.models.CatalogInner;
import com.azure.resourcemanager.devcenter.models.Catalog;
import com.azure.resourcemanager.devcenter.models.CatalogSyncState;
import com.azure.resourcemanager.devcenter.models.CatalogUpdate;
import com.azure.resourcemanager.devcenter.models.GitCatalog;
import com.azure.resourcemanager.devcenter.models.ProvisioningState;
import java.time.OffsetDateTime;
import java.util.Map;

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

    private final com.azure.resourcemanager.devcenter.DevCenterManager 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 ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

    public CatalogSyncState syncState() {
        return this.innerModel().syncState();
    }

    public OffsetDateTime lastSyncTime() {
        return this.innerModel().lastSyncTime();
    }

    public GitCatalog gitHub() {
        return this.innerModel().gitHub();
    }

    public GitCatalog adoGit() {
        return this.innerModel().adoGit();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.devcenter.DevCenterManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String devCenterName;

    private String catalogName;

    private CatalogUpdate updateBody;

    public CatalogImpl withExistingDevcenter(String resourceGroupName, String devCenterName) {
        this.resourceGroupName = resourceGroupName;
        this.devCenterName = devCenterName;
        return this;
    }

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

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

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

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

    public Catalog apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCatalogs()
                .update(resourceGroupName, devCenterName, catalogName, updateBody, Context.NONE);
        return this;
    }

    public Catalog apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCatalogs()
                .update(resourceGroupName, devCenterName, catalogName, updateBody, context);
        return this;
    }

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

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

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

    public void sync() {
        serviceManager.catalogs().sync(resourceGroupName, devCenterName, catalogName);
    }

    public void sync(Context context) {
        serviceManager.catalogs().sync(resourceGroupName, devCenterName, catalogName, context);
    }

    public CatalogImpl withGitHub(GitCatalog gitHub) {
        if (isInCreateMode()) {
            this.innerModel().withGitHub(gitHub);
            return this;
        } else {
            this.updateBody.withGitHub(gitHub);
            return this;
        }
    }

    public CatalogImpl withAdoGit(GitCatalog adoGit) {
        if (isInCreateMode()) {
            this.innerModel().withAdoGit(adoGit);
            return this;
        } else {
            this.updateBody.withAdoGit(adoGit);
            return this;
        }
    }

    public CatalogImpl withTags(Map<String, String> tags) {
        this.updateBody.withTags(tags);
        return this;
    }

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