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

package com.azure.resourcemanager.automation.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.automation.fluent.models.WatcherInner;
import com.azure.resourcemanager.automation.models.Watcher;
import com.azure.resourcemanager.automation.models.WatcherUpdateParameters;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

public final class WatcherImpl implements Watcher, Watcher.Definition, Watcher.Update {
    private WatcherInner innerObject;

    private final com.azure.resourcemanager.automation.AutomationManager serviceManager;

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

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

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

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

    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 location() {
        return this.innerModel().location();
    }

    public Long executionFrequencyInSeconds() {
        return this.innerModel().executionFrequencyInSeconds();
    }

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

    public Map<String, String> scriptParameters() {
        Map<String, String> inner = this.innerModel().scriptParameters();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

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

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

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.automation.AutomationManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String automationAccountName;

    private String watcherName;

    private WatcherUpdateParameters updateParameters;

    public WatcherImpl withExistingAutomationAccount(String resourceGroupName, String automationAccountName) {
        this.resourceGroupName = resourceGroupName;
        this.automationAccountName = automationAccountName;
        return this;
    }

    public Watcher create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWatchers()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, watcherName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Watcher create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWatchers()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, watcherName, this.innerModel(), context)
                .getValue();
        return this;
    }

    WatcherImpl(String name, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = new WatcherInner();
        this.serviceManager = serviceManager;
        this.watcherName = name;
    }

    public WatcherImpl update() {
        this.updateParameters = new WatcherUpdateParameters();
        return this;
    }

    public Watcher apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWatchers()
                .updateWithResponse(
                    resourceGroupName, automationAccountName, watcherName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Watcher apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWatchers()
                .updateWithResponse(resourceGroupName, automationAccountName, watcherName, updateParameters, context)
                .getValue();
        return this;
    }

    WatcherImpl(WatcherInner innerObject, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.automationAccountName = Utils.getValueFromIdByName(innerObject.id(), "automationAccounts");
        this.watcherName = Utils.getValueFromIdByName(innerObject.id(), "watchers");
    }

    public Watcher refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWatchers()
                .getWithResponse(resourceGroupName, automationAccountName, watcherName, Context.NONE)
                .getValue();
        return this;
    }

    public Watcher refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWatchers()
                .getWithResponse(resourceGroupName, automationAccountName, watcherName, context)
                .getValue();
        return this;
    }

    public void start() {
        serviceManager.watchers().start(resourceGroupName, automationAccountName, watcherName);
    }

    public Response<Void> startWithResponse(Context context) {
        return serviceManager
            .watchers()
            .startWithResponse(resourceGroupName, automationAccountName, watcherName, context);
    }

    public void stop() {
        serviceManager.watchers().stop(resourceGroupName, automationAccountName, watcherName);
    }

    public Response<Void> stopWithResponse(Context context) {
        return serviceManager
            .watchers()
            .stopWithResponse(resourceGroupName, automationAccountName, watcherName, context);
    }

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

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

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

    public WatcherImpl withEtag(String etag) {
        this.innerModel().withEtag(etag);
        return this;
    }

    public WatcherImpl withExecutionFrequencyInSeconds(Long executionFrequencyInSeconds) {
        if (isInCreateMode()) {
            this.innerModel().withExecutionFrequencyInSeconds(executionFrequencyInSeconds);
            return this;
        } else {
            this.updateParameters.withExecutionFrequencyInSeconds(executionFrequencyInSeconds);
            return this;
        }
    }

    public WatcherImpl withScriptName(String scriptName) {
        this.innerModel().withScriptName(scriptName);
        return this;
    }

    public WatcherImpl withScriptParameters(Map<String, String> scriptParameters) {
        this.innerModel().withScriptParameters(scriptParameters);
        return this;
    }

    public WatcherImpl withScriptRunOn(String scriptRunOn) {
        this.innerModel().withScriptRunOn(scriptRunOn);
        return this;
    }

    public WatcherImpl withDescription(String description) {
        this.innerModel().withDescription(description);
        return this;
    }

    public WatcherImpl withName(String name) {
        this.updateParameters.withName(name);
        return this;
    }

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