local addonName, addon = ...


addon.EquippedItemsMixin = {}
local EquippedItemsMixin = addon.EquippedItemsMixin

local DB = addon.DB


function EquippedItemsMixin:init(slot, slotInfoFrame)
    
    self.slot = slot
    self.quality = 0
    self.location = ItemLocation:CreateFromEquipmentSlot(slot)
    self.upgradeable = nil
    self.info = nil
    self.slotInfoFrame = slotInfoFrame
    self.sumEquippedItemsCost = {}
    self.maxSlotCosts = {}
    self.link = nil
    self.itemID = nil
    self.itemIcon = nil
    self.itemLevel = nil
    self.dirty = true
    self.characterHighWatermark = nil
    self.accountHighWatermark = nil

    if self.location and self.location:IsValid() then
        self:FetchItemData()
    else
        print("Invalid location for slot: " .. tostring(slot))
    end
end

function EquippedItemsMixin:FetchItemData()
    local item = Item:CreateFromItemLocation(self.location)

    item:ContinueOnItemLoad(function()
        self.link = C_Item.GetItemLink(self.location)
        self.upgradeable = C_ItemUpgrade.CanUpgradeItem(self.location)
        self.itemID = self:GetItemID(true)
        self.itemIcon = self:GetItemIcon(true)
        self.itemLevel = self:GetItemLevel(true)
        self.characterHighWatermark, self.accountHighWatermark = C_ItemUpgrade.GetHighWatermarkForItem(self.link)
    end)
end

function EquippedItemsMixin:GetItemID(force)
    if not self.itemID or force then
        if self.location and self.location:IsValid() then
           
            self.itemID = C_Item.GetItemIDForItemInfo(self.link)
        end
    end
    return self.itemID
end

function EquippedItemsMixin:GetItemIcon(force)
    if not self.itemIcon or force then
        local itemID = self:GetItemID()
        if itemID then
            local icon = select(10, C_Item.GetItemInfo(self.link))
            local quality = select(3, C_Item.GetItemInfo(self.link))
            self.itemIcon = icon
            self.quality = quality
        end
    end
    return self.itemIcon
end

function EquippedItemsMixin:GetItemLevel(force)
    if not self.itemLevel or force then
        local link = self.link
        if link then
            local effectiveILvl = C_Item.GetDetailedItemLevelInfo(link)
            self.itemLevel = effectiveILvl
        end
    end
    return self.itemLevel
end

function EquippedItemsMixin:SaveData()
    return {
        slot = self.slot,
        quality = self.quality,
        upgradeable = self.upgradeable,
        info = self.info,
        sumEquippedItemsCost = self.sumEquippedItemsCost,
        maxSlotCosts = self.maxSlotCosts,
        dirty = self.dirty
    }
end

function EquippedItemsMixin:LoadData(data)
    --self.slot = data.slot
    self.quality = data.quality
    self.upgradeable = data.upgradeable
    self.info = data.info
    self.sumEquippedItemsCost = data.sumEquippedItemsCost
    self.maxSlotCosts = data.maxSlotCosts
    self.dirty = data.dirty
end

function EquippedItemsMixin:CalculateMaxSlotCost(force)
    local maxSlotCosts = {}
    local slot = self.slot
    local itemlevel = self.itemLevel or 554
    local info = self.info

    -- Corrected the condition to check if maxItemCost is empty or nil
    if force or not (self.maxSlotCosts and next(self.maxSlotCosts)) then
        HexLog("Updating MaxItemCost " .. slot, 4)

        if info then
            local maxItemLevel = self.info.maxItemLevel
            if maxItemLevel then
                for iLevel, costs in pairs(HexOhmUpgradeTracker.upgradeCostsBySlot[slot].iLevel) do
                    if iLevel > maxItemLevel then
                        if costs.CrestID and DB.CREST_IDS[costs.CrestID] then
                            maxSlotCosts[costs.CrestID] = (maxSlotCosts[costs.CrestID] or 0) + costs.CrestCost
                        end
                        if costs.ValorStoneCost > 0 then
                            maxSlotCosts[DB.VALORSTONE_ID] = (maxSlotCosts[DB.VALORSTONE_ID] or 0) + costs.ValorStoneCost
                        end
                    end
                end
            end
        else
            for iLevel, costs in pairs(HexOhmUpgradeTracker.upgradeCostsBySlot[slot].iLevel) do
                if iLevel > itemlevel then
                    if costs.CrestID and DB.CREST_IDS[costs.CrestID] then
                        maxSlotCosts[costs.CrestID] = (maxSlotCosts[costs.CrestID] or 0) + costs.CrestCost
                    end
                    if costs.ValorStoneCost > 0 then
                        maxSlotCosts[DB.VALORSTONE_ID] = (maxSlotCosts[DB.VALORSTONE_ID] or 0) + costs.ValorStoneCost
                    end
                end
            end
        end
        self.maxSlotCosts = maxSlotCosts
    end

    
    return maxSlotCosts
end

function EquippedItemsMixin:CalculateEquippedActualCost()
    local maxItemCosts = {}
    local upgradeInfo = self.info
    if upgradeInfo then
        HexLog("Found UpgradeInfo for slot " .. self.slot,1)
        for _, upgradeLevelInfo in ipairs(upgradeInfo.upgradeLevelInfos) do
            if upgradeLevelInfo.itemLevelIncrement > 0 then
                for _, currencyCost in ipairs(upgradeLevelInfo.currencyCostsToUpgrade) do
                    local currencyID = currencyCost.currencyID
                    if DB.CREST_IDS[currencyID] or currencyID == DB.VALORSTONE_ID then
                        maxItemCosts[currencyID] = (maxItemCosts[currencyID] or 0) + currencyCost.cost
                    end
                end
            end
        end
    end

    self.sumEquippedItemsCost = maxItemCosts
    return maxItemCosts
end

function EquippedItemsMixin:CheckAndMarkDirtyItems()
    local location = self.location
    if location and location:IsValid() then
        local newLink = C_Item.GetItemLink(self.location)

        if newLink ~= self.link then
            HexLog("Slot " .. self.slot .. " Has a new link")
            -- Item link has changed or item is removed
            self.link = newLink
            self.upgradeable = C_ItemUpgrade.CanUpgradeItem(self.location)
            self.itemID = self:GetItemID(true)
            self.itemIcon = self:GetItemIcon(true)
            self.itemLevel = self:GetItemLevel(true)
            self.dirty = true
            self.info = nil
            
            self.maxSlotCosts = {}
            
            if self.slot == 16 then
                local offhandItem = HexOhmUpgradeTracker.equippedItems[17]
                if offhandItem then
                    offhandItem.dirty = true
                    --offhandItem.maxSlotCosts = {}
                end
            elseif self.slot == 17 then
                local mainhandItem = HexOhmUpgradeTracker.equippedItems[16]
                if mainhandItem then
                    mainhandItem.dirty = true
                    --mainhandItem.maxSlotCosts = {}
                end
            end

        else
            -- No changes detected
            --print("slot: ", self.slot, " No Changes Detected")
        end
    else
       
        self.link = nil
        self.upgradeable = false
        self.itemID = nil
        self.itemIcon = nil
        self.itemLevel = nil
        self.dirty = true
        self.maxSlotCosts = {}
        --print("slot: ", self.slot, " Item Removed")
        
        -- Check if the slot is 16 (main hand) or 17 (off-hand) and mark both as dirty
        if self.slot == 16 then
            local offhandItem = HexOhmUpgradeTracker.equippedItems[17]
            if offhandItem then
                offhandItem.dirty = true
                --print("slot: 17 (off-hand) also Marked as Dirty")
            end
        elseif self.slot == 17 then
            local mainhandItem = HexOhmUpgradeTracker.equippedItems[16]
            if mainhandItem then
                mainhandItem.dirty = true
                --print("slot: 16 (main hand) also Marked as Dirty")
            end
        end
        
    end
    self:CalculateMaxSlotCost()
    
end

function EquippedItemsMixin:UpdateUpgradeInfo()
    local location = self.location
    if location and location:IsValid() then
        local itemLink = self.link
        local upgradable = self.upgradeable
        local dirty = self.dirty
        
        if itemLink and upgradable then
            if dirty then
                C_ItemUpgrade.SetItemUpgradeFromLocation(location)
                self.info = C_ItemUpgrade.GetItemUpgradeItemInfo()
            end
        else
            self.info = nil
            --maybe self:init to set all to default
        end
    end
    self.dirty = false
    
end
