#Requires -Modules Az.Accounts,Az.Websites,Az.FrontDoor,Az.Storage,Az.Cdn,Az.Network,Az.TrafficManager,Az.ContainerInstance
function Get-EffectoryDomainResources {
    <#
   .SYNOPSIS
    Find resources in Azure that have DNS records
    .DESCRIPTION
    Gets all resources that have hostnames.
    .PARAMETER subscriptionId
    Optional Subscription Id to set the context to. Otherwise uses the current context.
    #>
    param(
        [Parameter(Mandatory)]
        [string] $effectoryDomainPattern,
        [Parameter()]
        [string] $subscriptionId 
    )

    # Initialize
    [EffectoryDomainNameCheck[]]$result = @()
    [string]$effectoryDomainPattern = "*.effectory.com"

    # Get subscription info
    [Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext]$currentContext = $null
    if (![string]::IsNullOrWhitespace($subscriptionId)) {
        $currentContext = Set-AzContext -SubscriptionId $subscriptionId
    }
    else {
        $currentContext = Get-AzContext
        $subscriptionId = $currentContext.Subscription
    }

    Write-Verbose "Processing subscription $($currentContext.Name)"

    # ------------------------------------------------------------------------------------------------------------------
    Write-Verbose "Checking WebApps and WebApp Slots for subscription $($currentContext.Name)"
    $webApps = Get-AzWebApp 
    [int]$webAppCounter = 0
    [int]$webAppSlotCounter = 0

    if ($null -ne $webApps) {
        # check webapps
        $itemsWebApps = CheckWebApps -subscription $currentContext -webApps $webApps -effectoryDomainPattern $effectoryDomainPattern
        $webAppCounter += $itemsWebApps.Count
        $result +=  $itemsWebApps

        # check webapp slots
        foreach ($webApp in $webApps) {
            $slot = Get-AzWebAppSlot -WebApp $webApp 
            if ($null -ne $slot) {
                $itemsWebAppSlots = CheckWebApps -subscription $currentContext -webApps $slot -effectoryDomainPattern $effectoryDomainPattern
                $webAppSlotCounter += $itemsWebAppSlots.Count
                $result +=  $itemsWebAppSlots
            }
        }
    }
    Write-Verbose "Found $($webAppCounter) WebApps and $($webAppSlotsCounter) WebApp Slots for subscription $($currentContext.Name)"
    
    # ------------------------------------------------------------------------------------------------------------------
    Write-Verbose "Checking FrontDoor Endpoints for subscription $($currentContext.Name)"
    $frontDoors = Get-AzFrontDoor
    [int]$frontDoorEndPointCounter = 0

    if ($null -ne $frontDoors) {
        $itemsFrontDoors = CheckFrontDoorEndPoints -subscription $currentContext -frontDoors $frontDoors -effectoryDomainPattern $effectoryDomainPattern
        $frontDoorEndPointCounter += $itemsFrontDoors.Count
        $result += $itemsFrontDoors
    }
    Write-Verbose "Found $($frontDoorEndPointCounter) FrontDoor Endpoints for subscription $($currentContext.Name)"

    # ------------------------------------------------------------------------------------------------------------------
    Write-Verbose "Checking Storage Accounts for subscription $($currentContext.Name)"
    $storageAccounts = Get-AzStorageAccount | Where-Object { $_.CustomDomain.Name -like $effectoryDomainPattern } # storage accounts
    [int]$storageCounter = 0

    if ($null -ne $storageAccounts) {
        $itemsStorage = CheckStorageAccounts -subscription $currentContext -storageAccounts $storageAccounts -effectoryDomainPattern $effectoryDomainPattern
        $storageCounter += $itemsStorage.Count
        $result += $itemsStorage
    }
    Write-Verbose "Found $($storageCounter) Storage Accounts for subscription $($currentContext.Name)"
 
    # ------------------------------------------------------------------------------------------------------------------
    Write-Verbose "Checking Cdn Endpoints for subscription $($currentContext.Name)"
    $cdnProfiles = Get-AzCdnProfile
    [int]$cdnCounter = 0
    
    if ($null -ne $cdnProfiles) {
        $itemsCdn = CheckCdnEndpoints -subscription $currentContext -cdnProfiles $cdnProfiles -effectoryDomainPattern $effectoryDomainPattern
        $cdnCounter += $itemsCdn.Count
        $result += $itemsCdn
    }
    Write-Verbose "Found $($cdnCounter) Cdn Endpoints for subscription $($currentContext.Name)"
 
    # ------------------------------------------------------------------------------------------------------------------
    Write-Verbose "Checking public IP addresses for subscription $($currentContext.Name)"
    $ipAddresses = Get-AzPublicIpAddress | Where-Object DnsSettings -ne $null  | Where-Object { $_.DnsSettings.DomainNameLabel -ne "" }
    [int]$ipCounter = 0
    
    if ($null -ne $ipAddresses) {
        $itemsIpAddresses = CheckIpAddresses -subscription $currentContext -ipAddresses $ipAddresses -effectoryDomainPattern $effectoryDomainPattern
        $ipCounter += $itemsIpAddresses.Count
        $result += $itemsIpAddresses
    }
    Write-Verbose "Found $($ipCounter) public IP addresses for subscription $($currentContext.Name)"

    # ------------------------------------------------------------------------------------------------------------------
    Write-Verbose "Checking Traffic Managers for subscription $($currentContext.Name)"
    $trafficManagers = Get-AzTrafficManagerProfile
    [int]$trafficManagerCounter = 0
    
    if ($null -ne $trafficManagers) {
        $itemsTrafficManagers = CheckTrafficManagers -subscription $currentContext -trafficManagers $trafficManagers -effectoryDomainPattern $effectoryDomainPattern
        $trafficManagerCounter += $itemsTrafficManagers.Count
        $result += $itemsTrafficManagers
    }
    Write-Verbose "Found $($trafficManagerCounter) Traffic Managers for subscription $($currentContext.Name)"

    # ------------------------------------------------------------------------------------------------------------------
    Write-Verbose "Checking Container groups for subscription $($currentContext.Name)"
    $containerInstances =  Get-AzContainerGroup
    
    if ($null -ne $containerInstances) {
        throw "Container groups are not implemented yet."
    }

    # ------------------------------------------------------------------------------------------------------------------
    Write-Verbose "Checking API Management for subscription $($currentContext.Name)"
    $apiManagementServices =  Get-AzApiManagement
    
    if ($null -ne $apiManagementServices) {
        throw "API Management services are not implemented yet."
    }

    # ------------------------------------------------------------------------------------------------------------------
    $result
}