// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.resourcemanager.compute.models;

import com.azure.resourcemanager.resources.fluentcore.arm.ResourceUtils;

/** The source from which managed disk or snapshot is created. */
public class CreationSource {
    private final CreationData creationData;

    /**
     * Creates DiskSource.
     *
     * @param creationData the creation data of managed disk or snapshot
     */
    public CreationSource(CreationData creationData) {
        this.creationData = creationData;
    }

    /** @return type of the source from which disk or snapshot is created */
    public CreationSourceType type() {
        DiskCreateOption createOption = this.creationData.createOption();
        if (createOption == DiskCreateOption.FROM_IMAGE) {
            ImageDiskReference imageReference = this.creationData.imageReference();
            if (imageReference.lun() == null) {
                return CreationSourceType.FROM_OS_DISK_IMAGE;
            }
            return CreationSourceType.FROM_DATA_DISK_IMAGE;
        }
        if (createOption == DiskCreateOption.IMPORT) {
            return CreationSourceType.IMPORTED_FROM_VHD;
        }
        if (createOption == DiskCreateOption.COPY) {
            String sourceResourceId = this.creationData.sourceResourceId();
            if (sourceResourceId != null) {
                String resourceType = ResourceUtils.resourceTypeFromResourceId(sourceResourceId);
                if (resourceType.equalsIgnoreCase("disks")) {
                    return CreationSourceType.COPIED_FROM_DISK;
                }
                if (resourceType.equalsIgnoreCase("snapshots")) {
                    return CreationSourceType.COPIED_FROM_SNAPSHOT;
                }
            }
            if (this.creationData.sourceUri() != null) {
                sourceResourceId = this.creationData.sourceUri();
                String resourceType = ResourceUtils.resourceTypeFromResourceId(sourceResourceId);
                if (resourceType.equalsIgnoreCase("disks")) {
                    return CreationSourceType.COPIED_FROM_DISK;
                }
                if (resourceType.equalsIgnoreCase("snapshots")) {
                    return CreationSourceType.COPIED_FROM_SNAPSHOT;
                }
            }
        }
        if (createOption == DiskCreateOption.EMPTY) {
            return CreationSourceType.EMPTY;
        }
        return CreationSourceType.UNKNOWN;
    }

    /** @return ID of the source */
    public String sourceId() {
        if (this.type() == CreationSourceType.FROM_OS_DISK_IMAGE
            || this.type() == CreationSourceType.FROM_DATA_DISK_IMAGE) {
            return this.creationData.imageReference().id();
        }
        if (this.type() == CreationSourceType.IMPORTED_FROM_VHD) {
            return this.creationData.sourceUri();
        }
        if (this.type() == CreationSourceType.COPIED_FROM_DISK) {
            String sourceResourceId = this.creationData.sourceResourceId();
            if (sourceResourceId == null) {
                sourceResourceId = this.creationData.sourceUri();
            }
            return sourceResourceId;
        }
        if (this.type() == CreationSourceType.COPIED_FROM_SNAPSHOT) {
            String sourceResourceId = this.creationData.sourceResourceId();
            if (sourceResourceId == null) {
                sourceResourceId = this.creationData.sourceUri();
            }
            return sourceResourceId;
        }
        return null;
    }

    /**
     * @return the LUN value of the data disk image if this disk or snapshot is created from a data disk image, -1
     *     otherwise
     */
    public int sourceDataDiskImageLun() {
        if (this.type() == CreationSourceType.FROM_DATA_DISK_IMAGE) {
            return this.creationData.imageReference().lun();
        }
        return -1;
    }
}

@@ -1399,443 +1399,95 @@
 eId 
-!
+=
 = null
-) %7B%0A                String resourceType = ResourceUtils.resourceTypeFromResourceId(sourceResourceId);%0A                if (resourceType.equalsIgnoreCase(%22disks%22)) %7B%0A                    return CreationSourceType.COPIED_FROM_DISK;%0A                %7D%0A                if (resourceType.equalsIgnoreCase(%22snapshots%22)) %7B%0A                    return CreationSourceType.COPIED_FROM_SNAPSHOT;%0A                %7D%0A            %7D%0A            if (
+ && this.creationData.sourceUri() != null) %7B%0A                sourceResourceId = 
 this
@@ -1507,36 +1507,28 @@
 .sourceUri()
- != null) %7B%0A
+;%0A  
             
@@ -1516,39 +1516,51 @@
 i();%0A           
+ %7D%0A
        
+     if (
 sourceResourceId
@@ -1564,40 +1564,18 @@
 eId 
+!
 = 
-this.creationData.sourceUri();
+null) %7B
 %0A   
@@ -1685,36 +1685,31 @@
         if (
-resourceType
+%22disks%22
 .equalsIgnor
@@ -1714,23 +1714,28 @@
 oreCase(
-%22disks%22
+resourceType
 )) %7B%0A   
@@ -1833,28 +1833,27 @@
     if (
-resourceType
+%22snapshots%22
 .equalsI
@@ -1862,27 +1862,28 @@
 oreCase(
-%22snapshots%22
+resourceType
 )) %7B%0A   

Update CreationSource.java (#17948)