--[[
============================================================
Decor Vendor Addon
© 2026 MidniteDestiny. All Rights Reserved.
============================================================

This file is part of the Decor Vendor addon.

All code, structure, and design are the intellectual
property of MidniteDestiny unless otherwise stated.

You may NOT:
• Copy, reproduce, or redistribute this code
• Modify and redistribute this code
• Use this code in other addons or projects

without explicit permission from the author.

This addon is distributed for personal use only.

============================================================
]]

local addonName, dv = ...

-------------------------------------------------
-- Gallery Loader
-- Loads DecorVendor_Gallery from the main DecorVendor addon.
-------------------------------------------------

local GALLERY_ADDON_NAME = "DecorVendor_Gallery"

local function IsGalleryLoaded()
    if C_AddOns and C_AddOns.IsAddOnLoaded then
        return C_AddOns.IsAddOnLoaded(GALLERY_ADDON_NAME)
    elseif IsAddOnLoaded then
        return IsAddOnLoaded(GALLERY_ADDON_NAME)
    end

    return false
end

local function LoadGallery()
    if IsGalleryLoaded() then
        return true
    end

    local loaded, reason

    if C_AddOns and C_AddOns.LoadAddOn then
        loaded, reason = C_AddOns.LoadAddOn(GALLERY_ADDON_NAME)
    elseif LoadAddOn then
        loaded, reason = LoadAddOn(GALLERY_ADDON_NAME)
    end

    return loaded, reason
end

local function GetGalleryGlobal()
    return _G.DecorVendorGallery
        or _G.DecorVendor_Gallery
        or _G.Gallery
end

-------------------------------------------------
-- Quiet Gallery Preload
-------------------------------------------------

function dv.PreloadGalleryAddon()
    if dv._galleryPreloadStarted then
        return
    end

    dv._galleryPreloadStarted = true

    if IsGalleryLoaded() then
        dv._galleryPreloaded = true
        return
    end

    C_Timer.After(4, function()
        local loaded = LoadGallery()

        if loaded then
            dv._galleryPreloaded = true

            local Gallery = GetGalleryGlobal()

            if Gallery then
                if Gallery.BuildItemList and not Gallery.items then
                    Gallery.BuildItemList()
                end

                if Gallery.BuildCategoryList and not Gallery.categoryList then
                    Gallery.BuildCategoryList()
                end
            end
        else
            dv._galleryPreloaded = false
        end
    end)
end

-------------------------------------------------
-- Open Gallery
-------------------------------------------------

function dv.OpenGalleryAddon()
    local loaded, reason = LoadGallery()

    if not loaded then
        print("|cffff5555DecorVendor:|r Could not load " .. GALLERY_ADDON_NAME .. ". Reason:", tostring(reason))
        return
    end

    C_Timer.After(0, function()
        local Gallery = GetGalleryGlobal()

        if DecorVendorGallery_Open then
            DecorVendorGallery_Open()
            return
        end

        if Gallery and Gallery.Open then
            Gallery.Open()
            return
        end

        if Gallery and Gallery.Toggle then
            Gallery.Toggle()
            return
        end

        if Gallery and Gallery.frame then
            Gallery.frame:Show()
            return
        end

        print("|cffffcc00DecorVendor:|r " .. GALLERY_ADDON_NAME .. " loaded, but no open function was found.")
    end)
end