// 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.util.Context;
import com.azure.resourcemanager.automation.fluent.models.ScheduleInner;
import com.azure.resourcemanager.automation.models.AdvancedSchedule;
import com.azure.resourcemanager.automation.models.Schedule;
import com.azure.resourcemanager.automation.models.ScheduleCreateOrUpdateParameters;
import com.azure.resourcemanager.automation.models.ScheduleFrequency;
import com.azure.resourcemanager.automation.models.ScheduleUpdateParameters;
import java.time.OffsetDateTime;

public final class ScheduleImpl implements Schedule, Schedule.Definition, Schedule.Update {
    private ScheduleInner 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 OffsetDateTime startTime() {
        return this.innerModel().startTime();
    }

    public Double startTimeOffsetMinutes() {
        return this.innerModel().startTimeOffsetMinutes();
    }

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

    public Double expiryTimeOffsetMinutes() {
        return this.innerModel().expiryTimeOffsetMinutes();
    }

    public Boolean isEnabled() {
        return this.innerModel().isEnabled();
    }

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

    public Double nextRunOffsetMinutes() {
        return this.innerModel().nextRunOffsetMinutes();
    }

    public Object interval() {
        return this.innerModel().interval();
    }

    public ScheduleFrequency frequency() {
        return this.innerModel().frequency();
    }

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

    public AdvancedSchedule advancedSchedule() {
        return this.innerModel().advancedSchedule();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String automationAccountName;

    private String scheduleName;

    private ScheduleCreateOrUpdateParameters createParameters;

    private ScheduleUpdateParameters updateParameters;

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

    public Schedule create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSchedules()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, scheduleName, createParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Schedule create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSchedules()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, scheduleName, createParameters, context)
                .getValue();
        return this;
    }

    ScheduleImpl(String name, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = new ScheduleInner();
        this.serviceManager = serviceManager;
        this.scheduleName = name;
        this.createParameters = new ScheduleCreateOrUpdateParameters();
    }

    public ScheduleImpl update() {
        this.updateParameters = new ScheduleUpdateParameters();
        return this;
    }

    public Schedule apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSchedules()
                .updateWithResponse(
                    resourceGroupName, automationAccountName, scheduleName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Schedule apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSchedules()
                .updateWithResponse(resourceGroupName, automationAccountName, scheduleName, updateParameters, context)
                .getValue();
        return this;
    }

    ScheduleImpl(ScheduleInner 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.scheduleName = Utils.getValueFromIdByName(innerObject.id(), "schedules");
    }

    public Schedule refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSchedules()
                .getWithResponse(resourceGroupName, automationAccountName, scheduleName, Context.NONE)
                .getValue();
        return this;
    }

    public Schedule refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSchedules()
                .getWithResponse(resourceGroupName, automationAccountName, scheduleName, context)
                .getValue();
        return this;
    }

    public ScheduleImpl withName(String name) {
        if (isInCreateMode()) {
            this.createParameters.withName(name);
            return this;
        } else {
            this.updateParameters.withName(name);
            return this;
        }
    }

    public ScheduleImpl withStartTime(OffsetDateTime startTime) {
        this.createParameters.withStartTime(startTime);
        return this;
    }

    public ScheduleImpl withFrequency(ScheduleFrequency frequency) {
        this.createParameters.withFrequency(frequency);
        return this;
    }

    public ScheduleImpl withDescription(String description) {
        if (isInCreateMode()) {
            this.createParameters.withDescription(description);
            return this;
        } else {
            this.updateParameters.withDescription(description);
            return this;
        }
    }

    public ScheduleImpl withExpiryTime(OffsetDateTime expiryTime) {
        this.createParameters.withExpiryTime(expiryTime);
        return this;
    }

    public ScheduleImpl withInterval(Object interval) {
        this.createParameters.withInterval(interval);
        return this;
    }

    public ScheduleImpl withTimeZone(String timeZone) {
        this.createParameters.withTimeZone(timeZone);
        return this;
    }

    public ScheduleImpl withAdvancedSchedule(AdvancedSchedule advancedSchedule) {
        this.createParameters.withAdvancedSchedule(advancedSchedule);
        return this;
    }

    public ScheduleImpl withIsEnabled(Boolean isEnabled) {
        this.updateParameters.withIsEnabled(isEnabled);
        return this;
    }

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