local _, ns = ...

local TransientPanels = {}
ns.UITransientPanels = TransientPanels

function TransientPanels.Install(UI, helpers)
    if type(UI) ~= "table" then
        return
    end

    helpers = helpers or {}
    local setMiniActionButtonActive = helpers.setMiniActionButtonActive

    function UI:GetTransientPanelMode()
        if self.frame and self.frame.settingsPanel and self.frame.settingsPanel:IsShown() then
            return "settings"
        end
        if self.featuresPanelOpen == true then
            return "features"
        end
        if self.wishlistPanelOpen == true then
            return "wishlist"
        end

        return nil
    end

    function UI:IsTransientPanelOpen()
        return self:GetTransientPanelMode() ~= nil
    end

    function UI:RefreshFeaturesButton()
        if not self.frame or not self.frame.featuresButton then
            return
        end

        local settingsOpen = self.frame.settingsPanel and self.frame.settingsPanel:IsShown()
        local featuresOpen = self.featuresPanelOpen == true

        if self.frame.featuresButton.SetActive then
            self.frame.featuresButton:SetActive(featuresOpen)
            self.frame.featuresButton:Enable()
        elseif setMiniActionButtonActive then
            setMiniActionButtonActive(self.frame.featuresButton, featuresOpen)
        end
        self.frame.featuresButton:SetAlpha(1)

        if self.frame.settingsButton then
            if self.frame.settingsButton.SetActive then
                self.frame.settingsButton:SetActive(settingsOpen == true)
                self.frame.settingsButton:Enable()
            elseif setMiniActionButtonActive then
                setMiniActionButtonActive(self.frame.settingsButton, settingsOpen == true)
            end
            self.frame.settingsButton:SetAlpha(1)
        end
    end

    function UI:CloseTransientPanels(except)
        if not self.frame then
            return
        end

        if except ~= "settings" and self.frame.settingsPanel then
            self.frame.settingsPanel:Hide()
        end
        if except ~= "wishlist" then
            self.wishlistPanelOpen = false
        end
        if except ~= "features" then
            self.featuresPanelOpen = false
        end
        if self.frame.featuresPanel then
            self.frame.featuresPanel:SetShown(self.featuresPanelOpen == true)
        end
    end

    function UI:ApplyWorkbenchLayout(panelKey)
        if not self.frame then
            return
        end

        local frame = self.frame
        frame.sidebar:Hide()
        frame.content:Hide()
        frame.utility:Hide()

        if frame.settingsPanel then
            frame.settingsPanel:SetShown(panelKey == "settings")
        end
        if frame.featuresPanel then
            frame.featuresPanel:SetShown(panelKey == "features")
        end
        if frame.wishlistCard then
            frame.wishlistCard:SetShown(panelKey == "wishlist")
        end
    end

    function UI:OpenSettingsPanel()
        if not self.frame then
            self:Initialize()
        end
        if not self.frame then
            return false
        end

        if not self.frame:IsShown() then
            if ns and ns.RefreshCurrentCharacterLiveData then
                ns:RefreshCurrentCharacterLiveData()
            end
            self.frame:Show()
            self.frame:Raise()
        end

        if self.CloseTransientPanels then
            self:CloseTransientPanels("settings")
        end
        if self.frame.settingsPanel then
            self.frame.settingsPanel:Show()
        end
        self:Refresh()
        return true
    end
end
