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

package com.azure.resourcemanager.security.implementation;

import com.azure.core.util.Context;
import com.azure.resourcemanager.security.fluent.models.DeviceSecurityGroupInner;
import com.azure.resourcemanager.security.models.AllowlistCustomAlertRule;
import com.azure.resourcemanager.security.models.DenylistCustomAlertRule;
import com.azure.resourcemanager.security.models.DeviceSecurityGroup;
import com.azure.resourcemanager.security.models.ThresholdCustomAlertRule;
import com.azure.resourcemanager.security.models.TimeWindowCustomAlertRule;
import java.util.Collections;
import java.util.List;

public final class DeviceSecurityGroupImpl
    implements DeviceSecurityGroup, DeviceSecurityGroup.Definition, DeviceSecurityGroup.Update {
    private DeviceSecurityGroupInner innerObject;

    private final com.azure.resourcemanager.security.SecurityManager serviceManager;

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

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

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

    public List<ThresholdCustomAlertRule> thresholdRules() {
        List<ThresholdCustomAlertRule> inner = this.innerModel().thresholdRules();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public List<TimeWindowCustomAlertRule> timeWindowRules() {
        List<TimeWindowCustomAlertRule> inner = this.innerModel().timeWindowRules();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public List<AllowlistCustomAlertRule> allowlistRules() {
        List<AllowlistCustomAlertRule> inner = this.innerModel().allowlistRules();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public List<DenylistCustomAlertRule> denylistRules() {
        List<DenylistCustomAlertRule> inner = this.innerModel().denylistRules();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

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

    private com.azure.resourcemanager.security.SecurityManager manager() {
        return this.serviceManager;
    }

    private String resourceId;

    private String deviceSecurityGroupName;

    public DeviceSecurityGroupImpl withExistingResourceId(String resourceId) {
        this.resourceId = resourceId;
        return this;
    }

    public DeviceSecurityGroup create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeviceSecurityGroups()
                .createOrUpdateWithResponse(resourceId, deviceSecurityGroupName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public DeviceSecurityGroup create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeviceSecurityGroups()
                .createOrUpdateWithResponse(resourceId, deviceSecurityGroupName, this.innerModel(), context)
                .getValue();
        return this;
    }

    DeviceSecurityGroupImpl(String name, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = new DeviceSecurityGroupInner();
        this.serviceManager = serviceManager;
        this.deviceSecurityGroupName = name;
    }

    public DeviceSecurityGroupImpl update() {
        return this;
    }

    public DeviceSecurityGroup apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeviceSecurityGroups()
                .createOrUpdateWithResponse(resourceId, deviceSecurityGroupName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public DeviceSecurityGroup apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeviceSecurityGroups()
                .createOrUpdateWithResponse(resourceId, deviceSecurityGroupName, this.innerModel(), context)
                .getValue();
        return this;
    }

    DeviceSecurityGroupImpl(
        DeviceSecurityGroupInner innerObject, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceId =
            Utils
                .getValueFromIdByParameterName(
                    innerObject.id(),
                    "/{resourceId}/providers/Microsoft.Security/deviceSecurityGroups/{deviceSecurityGroupName}",
                    "resourceId");
        this.deviceSecurityGroupName =
            Utils
                .getValueFromIdByParameterName(
                    innerObject.id(),
                    "/{resourceId}/providers/Microsoft.Security/deviceSecurityGroups/{deviceSecurityGroupName}",
                    "deviceSecurityGroupName");
    }

    public DeviceSecurityGroup refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeviceSecurityGroups()
                .getWithResponse(resourceId, deviceSecurityGroupName, Context.NONE)
                .getValue();
        return this;
    }

    public DeviceSecurityGroup refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeviceSecurityGroups()
                .getWithResponse(resourceId, deviceSecurityGroupName, context)
                .getValue();
        return this;
    }

    public DeviceSecurityGroupImpl withThresholdRules(List<ThresholdCustomAlertRule> thresholdRules) {
        this.innerModel().withThresholdRules(thresholdRules);
        return this;
    }

    public DeviceSecurityGroupImpl withTimeWindowRules(List<TimeWindowCustomAlertRule> timeWindowRules) {
        this.innerModel().withTimeWindowRules(timeWindowRules);
        return this;
    }

    public DeviceSecurityGroupImpl withAllowlistRules(List<AllowlistCustomAlertRule> allowlistRules) {
        this.innerModel().withAllowlistRules(allowlistRules);
        return this;
    }

    public DeviceSecurityGroupImpl withDenylistRules(List<DenylistCustomAlertRule> denylistRules) {
        this.innerModel().withDenylistRules(denylistRules);
        return this;
    }
}
