using module ..\Include.psm1

param(
    [PSCustomObject]$Pools,
    [PSCustomObject]$Stats,
    [PSCustomObject]$Config,
    [PSCustomObject[]]$Devices
)

$Name = "$(Get-Item $MyInvocation.MyCommand.Path | Select-Object -ExpandProperty BaseName)"
$Path = ".\Bin\$($Name)\sgminer.exe"
$HashSHA256 = "A24024BEA8789B62D61CB3F41432EA1A62EE5AD97CD3DEAB1E2308F40B127A4D"
$Uri = "https://github.com/KL0nLutiy/sgminer-kl/releases/download/kl-1.0.9/sgminer-kl-1.0.9-windows.zip"
$ManualUri = "https://github.com/KL0nLutiy/sgminer-kl"

$Miner_Config = Get-MinerConfig -Name $Name -Config $Config

$Commands = [PSCustomObject]@{ 
    "aergo"     = " --kernel aergo -X 256 -g 2" #Aergo
    "geek"      = " --kernel geek -X 256 -g 2" # Geek
    "phi"       = " --kernel phi -X 256 -g 2 -w 256" # Phi
    # "polytimos" = " --kernel polytimos -X 256 -g 2 -w 256" # Polytimos, 	WildRig-v0.19.1 is 50% faster
    "skunk"     = " --kernel skunk -X 256 -g 2 -w 256" # Skunk
    "tribus"    = " --kernel tribus -X 256 -g 2" #Tribus
    # "xevan"     = " --kernel xevan -X 256 -g 2" #Xevan, AMD-SgminerAvermore_v1.4.1 is 20% faster
    "x16s"      = " --kernel x16s -X 256 -g 2" #X16S Pigeoncoin
    "x16r"      = " --kernel x16r -X 256 -g 2" #X16R Ravencoin
    "x17"       = " --kernel x17 -X 256 -g 2"
}
#Commands from config file take precedence
if ($Miner_Config.Commands) { $Miner_Config.Commands | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name | ForEach-Object { $Commands | Add-Member $_ $($Miner_Config.Commands.$_) -Force } }

#CommonCommands from config file take precedence
if ($Miner_Config.CommonCommands) { $CommonCommands = $Miner_Config.CommonCommands }
else { $CommonCommands = " $(if (-not $Config.ShowMinerWindow) { ' --text-only' })" }

$Devices = @($Devices | Where-Object Type -EQ "GPU" | Where-Object Vendor -EQ "AMD" | Where-Object { $_.OpenCL.GlobalMemSize -gt 2GB })
$Devices | Select-Object Model -Unique | ForEach-Object { 
    $Miner_Device = @($Devices | Where-Object Model -EQ $_.Model)
    $Miner_Port = [UInt16]($Config.APIPort + ($Miner_Device | Select-Object -First 1 -ExpandProperty Id) + 1)

    $Commands | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name | ForEach-Object { $Algorithm_Norm = Get-Algorithm $_; $_ } | Where-Object { $Pools.$Algorithm_Norm.Protocol -eq "stratum+tcp" <#temp fix#> } | ForEach-Object { 
        $Miner_Name = (@($Name) + @($Miner_Device.Model | Sort-Object -unique | ForEach-Object { $Model = $_; "$(@($Miner_Device | Where-Object Model -eq $Model).Count)x$Model" }) | Select-Object) -join '-'

        #Get commands for active miner devices
        $Command = Get-CommandPerDevice -Command $Commands.$_ -ExcludeParameters @("algorithm", "k", "kernel") -DeviceIDs $Miner_Device.Type_Vendor_Index

        #Allow time to build binaries
        if (-not (Get-Stat "$($Miner_Name)_$($Algorithm_Norm)_HashRate")) { $WarmupTime = 90 } else { $WarmupTime = 30 }

        [PSCustomObject]@{ 
            Name             = $Miner_Name
            DeviceName       = $Miner_Device.Name
            Path             = $Path
            HashSHA256       = $HashSHA256
            Arguments        = ("$Command$CommonCommands --api-listen --api-port $Miner_Port --url $($Pools.$Algorithm_Norm.Protocol)://$($Pools.$Algorithm_Norm.Host):$($Pools.$Algorithm_Norm.Port) --user $($Pools.$Algorithm_Norm.User) --pass $($Pools.$Algorithm_Norm.Pass) --gpu-platform $($Miner_Device.PlatformId | Sort-Object -Unique) --device $(($Miner_Device | ForEach-Object { '{0:x}' -f $_.Type_Vendor_Index }) -join ',')" -replace "\s+", " ").trim()
            HashRates        = [PSCustomObject]@{ $Algorithm_Norm = $Stats."$($Miner_Name)_$($Algorithm_Norm)_HashRate".Week }
            API              = "Xgminer"
            Port             = $Miner_Port
            URI              = $Uri
            Fees             = [PSCustomObject]@{ $Algorithm_Norm = 1 / 100 }
            Environment      = @("GPU_FORCE_64BIT_PTR=0")
            WarmupTime       = $WarmupTime #seconds
            PrerequisitePath = "$env:SystemRoot\System32\VCRUNTIME140.dll"
            PrerequisiteURI  = "https://aka.ms/vs/16/release/vc_redist.x64.exe"
        }
    }
}
