# encoding: ascii
# api: powershell
# title: Buying groceries
# description: Buying groceries with PowerShell, because why not? ;-)
# version: 0.1
# type: function
# author: DollarUnderscore
# license: CC0
# function: Connect-Mathem
# x-poshcode-id: 6556
# x-archived: 2016-11-29T02:27:56
# x-published: 2016-10-06T12:27:00
#
# This service is only available for people living in certain cities in Sweden. The rest of you can just look at it as sample code for doing something similar.
# It contains a couple of advanced functions for buying groceries through a internet based service here. More information available at my blog (new post coming as well soon!):
# https://p0wershell.com/?p=2031
# Even though this is mostly made to generate a few smiles, it actually works… :-)
# Happy automating anything, as always :-)
# Edit: Updated to fix Get-FoodBasket (they changed the site) and added “Get-FoodItemMostBought”.
# Edit 2: Updated to fix Remove-FoodItem and enabled https usage (was not available when this “module” was first created)
# Edit 3: New function, Add-MatHemBonusCode
#
#========================================================================
# Created By: Anders Wahlqvist
# Website: DollarUnderscore (https://p0wershell.com)
#========================================================================


function Connect-Mathem
{
    [cmdletbinding()]
    param(
          [Parameter(Mandatory=$true)]
          [System.Management.Automation.PSCredential] $Credential)

    $PostURL = "https://www.mathem.se/Account/Login"

    $LoginRequest = @{'UserName'= $Credential.UserName ;'Password' = $Credential.GetNetworkCredential().Password }

    $LoginData = Invoke-WebRequest -Uri $PostURL -Body $LoginRequest -Method POST -SessionVariable Global:MathemSession -UseBasicParsing
}

function Get-FoodItem
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
        [Alias('ProductName')]
        $SearchString)

    BEGIN {
        if ($MathemSession -eq $null) {
            Write-Error "You must first connect using the Connect-Mathem cmdlet"
            return
        }
    }

    PROCESS {
        $SearchURL = "https://www.mathem.se/WebServices/ProductService.asmx/SearchAndAddResult?searchText="
        $SearchQuery = $SearchString -replace " ","+"

        $SearchResults = Invoke-RestMethod -Uri ($SearchURL + $SearchQuery) -WebSession $MathemSession

        foreach ($Product in $SearchResults) {

            $returnObject = New-Object System.Object
            $returnObject | Add-Member -Type NoteProperty -Name ProductID -Value $Product.ProductID
            $returnObject | Add-Member -Type NoteProperty -Name ProductName -Value $([text.encoding]::utf8.getstring([text.encoding]::default.GetBytes($Product.ProductName)))
            $returnObject | Add-Member -Type NoteProperty -Name Price -Value $Product.Price
            $returnObject | Add-Member -Type NoteProperty -Name PricePerUnit -Value $Product.PricePerUnit

            Write-Output $returnObject
        }
    }
}

function Add-FoodItem
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
        [string] $ProductID,
        [int] $NumberOfItems = 1)

    BEGIN {
        if ($MathemSession -eq $null) {
            Write-Error "You must first connect using the Connect-Mathem cmdlet"
            return
        }
    }

    PROCESS {
        $AddURL = "https://www.mathem.se/Pages/products/AddToCart.aspx?AddProduct=true&ProductID=$ProductID&noOfFooditem=$NumberOfItems"

        Write-Verbose "Adding item $ProductID to the cart..."

        $AddItem = Invoke-WebRequest -Uri $AddURL -WebSession $MathemSession -Method Get

    }

    END { }
}


function Get-FoodBasket
{

    if ($MathemSession -eq $null) {
        Write-Error "You must first connect using the Connect-Mathem cmdlet"
        return
    }

    $FoodCartURL = "https://www.mathem.se/ShoppingCart/RenderMiniCart"

    $FoodBasket = Invoke-RestMethod -Uri $FoodCartURL -WebSession $MathemSession
    
    $ItemsFoundInBasket = $FoodBasket.div.div.form.ul.li

    $ProductsInBasket = @()

    if ($ItemsFoundInBasket -ne $null) {
        $ProductsInBasket += $ItemsFoundInBasket
    }

    if ($ProductsInBasket.count -gt 0) {
        foreach ($Product in $ProductsInBasket) {
            $returnObject = New-Object System.Object
            $returnObject | Add-Member -Type NoteProperty -Name ProductID -Value $Product."data-highlight-id"
            $returnObject | Add-Member -Type NoteProperty -Name ProductName -Value $($Product.table.tbody.tr.td | select -Skip 1 -First 1 | % { $_.a."#text" })
            $returnObject | Add-Member -Type NoteProperty -Name ProductLink -Value $($Product.table.tbody.tr.td | select -Skip 1 -First 1 | % { $_.a.href })
            $returnObject | Add-Member -Type NoteProperty -Name TotalPrice -Value $($Product.table.tbody.tr.td | select -Skip 2 -First 1 | % { $_.strong })
            $returnObject | Add-Member -Type NoteProperty -Name NumberOfItems -Value $Product.table.tbody.tr.td.div.input.value

            Write-Output $returnObject
        }
    }
}

function Remove-FoodItem
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
        [string] $ProductID)

    begin {
        if ($MathemSession -eq $null) {
            Write-Error "You must first connect using the Connect-Mathem cmdlet"
            return
        }
    }

    PROCESS {
        $DeleteURL = "https://www.mathem.se/ShoppingCart/Delete/$ProductID"

        Write-Verbose "Removing item $ProductID from cart..."

        $DeleteItem = Invoke-WebRequest -Uri $DeleteURL -WebSession $MathemSession -Method Get
    }

    END { }
}

# Gets the list of the most bought items
function Get-FoodItemMostBought
{
    if ($MathemSession -eq $null) {
        Write-Error "You must first connect using the Connect-Mathem cmdlet"
        return
    }

    $MostBoughtURL = "https://www.mathem.se/min-sida/varor#mostBought"

    $MostBoughtFoodItemsSiteFile = Invoke-WebRequest -Uri $MostBoughtURL -WebSession $MathemSession -UseBasicParsing -OutFile .\tempfile.site
    $MostBoughtFoodItemsSite = Get-Content .\tempfile.site -Encoding UTF8
    Remove-Item .\tempfile.site -Force
    $MostBoughtFoodItems = $MostBoughtFoodItemsSite.split("`n") | Select-String -Pattern "class=`"prodTitle`"" | % { (($_ -split ">")[1] -split "</a")[0] } | select -Skip 1 | Sort-Object -Unique
    
    
    foreach ($Item in $MostBoughtFoodItems) {

        $returnObject = New-Object System.Object
        $returnObject | Add-Member -Type NoteProperty -Name ProductName -Value $Item

        Write-Output $returnObject
    }
}


function Add-MatHemBonusCode
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
        [string] $BonusCode)

    begin {
        if ($MathemSession -eq $null) {
            Write-Error "You must first connect using the Connect-Mathem cmdlet"
            return
        }
    }

    PROCESS {
        $AddCodeURL = "https://www.mathem.se/checkout/addbonuscode/$BonusCode"

        Write-Verbose "Adding bonus code $BonusCode"

        $BonusCodeResponse = Invoke-RestMethod -Uri $AddCodeURL -WebSession $MathemSession -Method Get

        if ($BonusCodeResponse.Error) {
            throw "The request failed with message: $($BonusCodeResponse.Message)"
        }
        else {
            $BonusCodeResponse.Message
        }
    }

    END { }
}
