WarFrameRelics.psm1

if(!(test-path $PSScriptRoot\data\RelicsData.xml)){&"$PSScriptRoot\utilities\Update-RelicData.ps1"}
if(!(test-path $PSScriptRoot\data\ItemData.xml)){&"$PSScriptRoot\utilities\Update-ItemData.ps1"}

$Relics = Import-CliXML $PSScriptRoot\data\RelicsData.xml
$ItemData = Import-CliXML $PSScriptRoot\data\ItemData.xml

Function Get-AveragePrice {
    param(
        [string]$partName
    )

    #TODO add in an if for spaces to decide if the conversion is needed?

    $headers = @{"Accept-Language" = "en"}
    $url = "https://api.warframe.market/v1/items/$partName/statistics"
    $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers

    $liveData = $response.payload.statistics_live
    $closedData = $response.payload.statistics_closed

    $ListedAvg48Hours = [math]::round((($liveData."48hours" | where { $_.order_type -eq "sell" }) | measure avg_price -Average).Average)
    #$live90d = [math]::round((($liveData."90days" | where { $_.order_type -eq "sell" }) | measure avg_price -Average).Average)
    $ClosedAvg48Hours = [math]::round((($closedData."48hours" | measure avg_price -Average).Average))
    #$closed90d = [math]::round((($closedData."90days" | measure avg_price -Average).Average))

    $ListedAvg48Hours, $ClosedAvg48Hours
}

#this function takes in a part and converts the name to the format that the API uses
function Convert-PartName
{
    param(
        [string]$partName
    )

    $partName = $partName -replace " ", "_"
    $partName = $partName.ToLower()

    return $partName
}

function Get-RelicDropPricesHelper {
    param(
        [string]$relicName,
        [hashtable]$Relics,
        [hashtable]$ItemData
    )

    $partInfo = @()

    Foreach($drop in $Relics[$relicName].drops)
    {
        #The item property would be something like "Carrier Prime" and the part property would be like "Chassis Blueprint"
        $partName = "$($drop.item) $($drop.Part)"
        $partPrices = Get-AveragePrice -partName (Convert-PartName $partName)

        $partInfo += [PSCustomObject]@{
            Name = $partName
            Listed48Hours = $partPrices[0]
            Closed48Hours = $partPrices[1]
            Status = if([string]::IsNullOrEmpty($ItemData[$partname].Vaulted)){""}else{"Vaulted"}
            JunkPlat = [math]::round([Double]($ItemData[$partname].DucatValue) / 7.5)
            DucatValue = $ItemData[$partname].DucatValue
        }
    }
    $partInfo | sort-object -Property Listed48Hours -Descending | Format-Table 
}

<#This will be the public function that is called so people only need to pass in the name of the relic
function Get-RelicDropPrices {
    param(
        [string]$relicName
    )
 
    Get-RelicDropPricesHelper -relicName $relicName -Relics $Relics -ItemData $ItemData
}
    #>


function Get-RelicDropPrices {
    param(
        [string[]]$relicName
    )

    foreach ($relic in $relicName) {
        Write-Output "### $relic ###"
        Get-RelicDropPricesHelper -relicName $relic -Relics $Relics -ItemData $ItemData
        Write-Output ""
    }
}

#Public function that checks when the data was last updated, which can use the creation time of the XML files
function Get-WarFrameDataLastUpdateDate {
    $relicTime = (Get-Item $PSScriptRoot\data\RelicsData.xml).CreationTime
    $itemTime = (Get-Item $PSScriptRoot\data\ItemData.xml).CreationTime

    Write-Host "Relic Data Last Updated: $relicTime"                                
    Write-Host "Item Data Last Updated: $itemTime"   
}

#public function that updates the data
function Update-WarFrameData {
    &"$PSScriptRoot\utilities\Update-RelicData.ps1"
    &"$PSScriptRoot\utilities\Update-ItemData.ps1"
}