local addonName, addon = ...

addon.LootTracker = addon.LootTracker or {}
addon.LootTracker.PriceSource = addon.LootTracker.PriceSource or {}

local PriceSource = addon.LootTracker.PriceSource

PriceSource.ADDON_NAME = addonName or "WhatsNext"

function PriceSource:GetSourceName()
    if C_AddOns
        and C_AddOns.IsAddOnLoaded
        and C_AddOns.IsAddOnLoaded(
            "Auctionator"
        )
        and Auctionator
        and Auctionator.API
        and Auctionator.API.v1
    then
        return "AUCTIONATOR"
    end

    if C_AddOns
        and C_AddOns.IsAddOnLoaded
        and C_AddOns.IsAddOnLoaded(
            "TradeSkillMaster"
        )
        and TSM_API
    then
        return "TSM"
    end

    return "NONE"
end

function PriceSource:GetAuctionatorPrice(
    itemID,
    itemLink
)
    if not Auctionator
        or not Auctionator.API
        or not Auctionator.API.v1
    then
        return nil
    end

    local api =
        Auctionator.API.v1

    local vendorPrice = nil

    if itemLink
        and api.GetVendorPriceByItemLink
    then
        local ok, value =
            pcall(
                api.GetVendorPriceByItemLink,
                self.ADDON_NAME,
                itemLink
            )

        if ok then
            vendorPrice = value
        end
    elseif api.GetVendorPriceByItemID then
        local ok, value =
            pcall(
                api.GetVendorPriceByItemID,
                self.ADDON_NAME,
                itemID
            )

        if ok then
            vendorPrice = value
        end
    end

    if vendorPrice
        and vendorPrice > 0
    then
        return vendorPrice
    end

    if itemLink
        and api.GetAuctionPriceByItemLink
    then
        local ok, value =
            pcall(
                api.GetAuctionPriceByItemLink,
                self.ADDON_NAME,
                itemLink
            )

        if ok
            and value
            and value > 0
        then
            return value
        end
    end

    if api.GetAuctionPriceByItemID then
        local ok, value =
            pcall(
                api.GetAuctionPriceByItemID,
                self.ADDON_NAME,
                itemID
            )

        if ok
            and value
            and value > 0
        then
            return value
        end
    end

    return nil
end

function PriceSource:GetTSMPrice(
    itemID,
    itemLink
)
    if not TSM_API
        or not TSM_API.GetCustomPriceValue
    then
        return nil
    end

    local itemString =
        "i:"
        .. tostring(
            itemID
        )

    if itemLink
        and TSM_API.ToItemString
    then
        local ok, value =
            pcall(
                TSM_API.ToItemString,
                itemLink
            )

        if ok
            and value
        then
            itemString = value
        end
    end

    local expressions = {
        "DBMinBuyout",
        "DBMarket",
    }

    for _, expression in ipairs(
        expressions
    ) do
        local ok, value =
            pcall(
                TSM_API.GetCustomPriceValue,
                expression,
                itemString
            )

        if ok
            and value
            and value > 0
        then
            return value
        end
    end

    return nil
end

function PriceSource:GetItemPrice(
    itemID,
    itemLink
)
    itemID =
        tonumber(
            itemID
        )

    if not itemID then
        return nil
    end

    local source =
        self:GetSourceName()

    if source == "AUCTIONATOR" then
        return self:GetAuctionatorPrice(
            itemID,
            itemLink
        )
    end

    if source == "TSM" then
        return self:GetTSMPrice(
            itemID,
            itemLink
        )
    end

    return nil
end

function PriceSource:FormatMoney(
    copper
)
    copper =
        tonumber(
            copper
        )

    if not copper
        or copper <= 0
    then
        return "NO PRICE"
    end

    local gold =
        math.floor(
            copper / 10000
        )

    local silver =
        math.floor(
            (
                copper % 10000
            )
            / 100
        )

    local coin =
        copper % 100

    if gold > 0 then
        return string.format(
            "%dg %02ds %02dc",
            gold,
            silver,
            coin
        )
    end

    if silver > 0 then
        return string.format(
            "%ds %02dc",
            silver,
            coin
        )
    end

    return string.format(
        "%dc",
        coin
    )
end
