// 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.InformationProtectionPolicyInner;
import com.azure.resourcemanager.security.models.InformationProtectionPolicy;
import com.azure.resourcemanager.security.models.InformationProtectionPolicyName;
import com.azure.resourcemanager.security.models.InformationType;
import com.azure.resourcemanager.security.models.SensitivityLabel;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

public final class InformationProtectionPolicyImpl
    implements InformationProtectionPolicy, InformationProtectionPolicy.Definition, InformationProtectionPolicy.Update {
    private InformationProtectionPolicyInner 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 OffsetDateTime lastModifiedUtc() {
        return this.innerModel().lastModifiedUtc();
    }

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

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

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

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

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

    private String scope;

    private InformationProtectionPolicyName informationProtectionPolicyName;

    public InformationProtectionPolicyImpl withExistingScope(String scope) {
        this.scope = scope;
        return this;
    }

    public InformationProtectionPolicy create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInformationProtectionPolicies()
                .createOrUpdateWithResponse(scope, informationProtectionPolicyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public InformationProtectionPolicy create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInformationProtectionPolicies()
                .createOrUpdateWithResponse(scope, informationProtectionPolicyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    InformationProtectionPolicyImpl(
        InformationProtectionPolicyName name, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = new InformationProtectionPolicyInner();
        this.serviceManager = serviceManager;
        this.informationProtectionPolicyName = name;
    }

    public InformationProtectionPolicyImpl update() {
        return this;
    }

    public InformationProtectionPolicy apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInformationProtectionPolicies()
                .createOrUpdateWithResponse(scope, informationProtectionPolicyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public InformationProtectionPolicy apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInformationProtectionPolicies()
                .createOrUpdateWithResponse(scope, informationProtectionPolicyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    InformationProtectionPolicyImpl(
        InformationProtectionPolicyInner innerObject,
        com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.scope =
            Utils
                .getValueFromIdByParameterName(
                    innerObject.id(),
                    "/{scope}/providers/Microsoft.Security/informationProtectionPolicies/{informationProtectionPolicyName}",
                    "scope");
        this.informationProtectionPolicyName =
            InformationProtectionPolicyName
                .fromString(
                    Utils
                        .getValueFromIdByParameterName(
                            innerObject.id(),
                            "/{scope}/providers/Microsoft.Security/informationProtectionPolicies/{informationProtectionPolicyName}",
                            "informationProtectionPolicyName"));
    }

    public InformationProtectionPolicy refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInformationProtectionPolicies()
                .getWithResponse(scope, informationProtectionPolicyName, Context.NONE)
                .getValue();
        return this;
    }

    public InformationProtectionPolicy refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInformationProtectionPolicies()
                .getWithResponse(scope, informationProtectionPolicyName, context)
                .getValue();
        return this;
    }

    public InformationProtectionPolicyImpl withLabels(Map<String, SensitivityLabel> labels) {
        this.innerModel().withLabels(labels);
        return this;
    }

    public InformationProtectionPolicyImpl withInformationTypes(Map<String, InformationType> informationTypes) {
        this.innerModel().withInformationTypes(informationTypes);
        return this;
    }
}
