param (
    $examplePC
)

$GetPCNotInstalledApps = @(
    "Citrix",
    "Microsoft Visual C\+\+",
    "RightFax",
    "Windows Driver Package"
)

function Get-AppDiff {
    param(
        [Parameter(Mandatory)]
        $pc,
        $TableView = $false
    )

    # Compares a pc's apps with a list of expected apps to see if the pc has any 
    # extra apps that we need to consider downloading

    $apps = Get-Apps $pc
    Write-Progress -Activity "Filtering out standard apps from $pc" -Status 'Removing version numbers and loading reference' -PercentComplete 10 -ParentId 1
    $apps = Remove-Version $apps
    # Open example file
    $lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\ExpectedApps.json'
    $example = get-content $lookupTablePath | ConvertFrom-Json
    # Open file with manually added apps to ignore
    $lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\AppsToIgnore.json'
    $ignore = get-content $lookupTablePath | ConvertFrom-Json
    $exclude = $example + $ignore
    $output = @()
    # Finds each app that isn't in the example
    $appi = 0
    foreach ( $app in $apps ) {
        $appi++
        Write-Progress -Activity "Filtering out standard apps from $pc" -Status "Checking $($app.DisplayName)" -PercentComplete (($appi * 90 / $apps.Length) + 10) -ParentId 1
        $skip = $false
        foreach ($notapp in $GetPCNotInstalledApps) {
            if ($app.DisplayName -match $notapp) {
                $skip = $true 
                break
            }
        }
        if ($skip -or $exclude.DisplayName -contains $app.DisplayName) { continue }

        $output += $app
    }
    Write-Progress -Activity "Filtering out standard apps from $pc" -Status "Checking $app" -PercentComplete (($appi * 90 / $apps.Length) + 10) -ParentId 1
    
    if($TableView){
        $output | Out-GridView -Title "Get-PC Apps - $pc"
    }
    else{
        Write-Output $output
    }
}

# Function parses get-pc -apps output to extract just the name, not the 
# version number or other unnecessary info

function Remove-Version {
	param ( $apps )
    $output = @()
    foreach ($app in $apps ) {
        if ($null -eq $app.DisplayName) { continue }
        # Strip out the version details
        # " - " might filter out too much, not sure
        $separator = "`t","   ", " - "
        $option = [System.StringSplitOptions]::RemoveEmptyEntries
        $app.DisplayName = $app.DisplayName.Split($separator, $option)[0]
        $output += $app
    }
    Write-Output $output
}

function Copy-FileNoDestruct ($srcpath, $dstpath) {
    while ($true) {
        if (Test-Path $dstpath) {
            $dstpath += ".bak"
        } else {
            Copy-Item -Path $srcpath -Destination $dstpath
            return
        }
    }
}

function Get-ExampleApps {
    param( $examplePC )
    $appsPath = (Join-Path (get-item $PSScriptRoot).Parent.FullName 'Private\Apps.ps1')
    $sccmPath = (Join-Path (get-item $PSScriptRoot).Parent.FullName 'Private\SCCMQuery.ps1')
    Import-Module -Force $appsPath
    Import-Module -Force $sccmPath
    $apps = Get-Apps $examplePC | Select-Object -Property "DisplayName"
    $trimmedApps = @()
    foreach ($app in $apps) {
        $skip = $false
        foreach ($notapp in $GetPCNotInstalledApps) {
            if (!$app.DisplayName -or $app.DisplayName -match $notapp) {
                $skip = $true 
                break
            }
        }
        if ($skip) { continue }
        $trimmedApps += $app
    }
    $apps = Remove-Version $trimmedApps
    # Open example file
    $lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\ExpectedApps.json'
    Copy-FileNoDestruct $lookupTablePath $($lookupTablePath + ".bak")
    Write-Host "Writing expected apps to" $lookupTablePath
    $apps | ConvertTo-Json | Out-File $lookupTablePath
}

if ($examplePC) {
    Get-ExampleApps $examplePC
}