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

package com.azure.resourcemanager.containerservicefleet.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.containerservicefleet.fluent.models.FleetMemberInner;
import com.azure.resourcemanager.containerservicefleet.models.FleetMember;
import com.azure.resourcemanager.containerservicefleet.models.FleetMemberProvisioningState;
import com.azure.resourcemanager.containerservicefleet.models.FleetMemberUpdate;

public final class FleetMemberImpl implements FleetMember, FleetMember.Definition, FleetMember.Update {
    private FleetMemberInner innerObject;

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String fleetName;

    private String fleetMemberName;

    private String createIfMatch;

    private String createIfNoneMatch;

    private String updateIfMatch;

    private FleetMemberUpdate updateProperties;

    public FleetMemberImpl withExistingFleet(String resourceGroupName, String fleetName) {
        this.resourceGroupName = resourceGroupName;
        this.fleetName = fleetName;
        return this;
    }

    public FleetMember create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFleetMembers()
                .create(
                    resourceGroupName,
                    fleetName,
                    fleetMemberName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    Context.NONE);
        return this;
    }

    public FleetMember create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFleetMembers()
                .create(
                    resourceGroupName,
                    fleetName,
                    fleetMemberName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    context);
        return this;
    }

    FleetMemberImpl(
        String name, com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager serviceManager) {
        this.innerObject = new FleetMemberInner();
        this.serviceManager = serviceManager;
        this.fleetMemberName = name;
        this.createIfMatch = null;
        this.createIfNoneMatch = null;
    }

    public FleetMemberImpl update() {
        this.updateIfMatch = null;
        this.updateProperties = new FleetMemberUpdate();
        return this;
    }

    public FleetMember apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFleetMembers()
                .updateWithResponse(
                    resourceGroupName, fleetName, fleetMemberName, updateProperties, updateIfMatch, Context.NONE)
                .getValue();
        return this;
    }

    public FleetMember apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFleetMembers()
                .updateWithResponse(
                    resourceGroupName, fleetName, fleetMemberName, updateProperties, updateIfMatch, context)
                .getValue();
        return this;
    }

    FleetMemberImpl(
        FleetMemberInner innerObject,
        com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.fleetName = Utils.getValueFromIdByName(innerObject.id(), "fleets");
        this.fleetMemberName = Utils.getValueFromIdByName(innerObject.id(), "members");
    }

    public FleetMember refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFleetMembers()
                .getWithResponse(resourceGroupName, fleetName, fleetMemberName, Context.NONE)
                .getValue();
        return this;
    }

    public FleetMember refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFleetMembers()
                .getWithResponse(resourceGroupName, fleetName, fleetMemberName, context)
                .getValue();
        return this;
    }

    public FleetMemberImpl withClusterResourceId(String clusterResourceId) {
        this.innerModel().withClusterResourceId(clusterResourceId);
        return this;
    }

    public FleetMemberImpl withGroup(String group) {
        if (isInCreateMode()) {
            this.innerModel().withGroup(group);
            return this;
        } else {
            this.updateProperties.withGroup(group);
            return this;
        }
    }

    public FleetMemberImpl withIfMatch(String ifMatch) {
        if (isInCreateMode()) {
            this.createIfMatch = ifMatch;
            return this;
        } else {
            this.updateIfMatch = ifMatch;
            return this;
        }
    }

    public FleetMemberImpl withIfNoneMatch(String ifNoneMatch) {
        this.createIfNoneMatch = ifNoneMatch;
        return this;
    }

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