local Button                = require("ui/widget/button")
local Event                 = require("ui/event")
local IconWidget            = require("ui/widget/iconwidget")
local ReaderFooter          = require("apps/reader/modules/readerfooter")
local Setting               = require("lib/setting")
local UIManager             = require("ui/uimanager")
local common                = require("lib/common")
local nf_icons              = require("lib/nf_icons")

local TransparentIcons      = Setting("ui_transparent_icons", false)     -- Whether icons should be fully transparent (default: false)
local TransparentButtons    = Setting("ui_transparent_buttons", false)   -- Whether buttons should be fully transparent (default: false)
local TransparentFooter     = Setting("ui_transparent_footer", false)     -- Whether the ReaderFooter should be fully transparent (default: false)
local TransparentBottomBar  = Setting("ui_transparent_bottombar", false) -- Whether the SimpleUI bottom bar should be made transparent

-- Background color setting
local FooterBackgroundColor = Setting("ui_background_color_reader_footer", false)

--------------------------------------------
-- Lazy Loading
--------------------------------------------

local ui_bgcolor

local function reloadIcons()
    ui_bgcolor = ui_bgcolor or require("ui/background_color")
    return ui_bgcolor.reloadIcons()
end

--------------------------------------------
-- Transparency
--------------------------------------------

-- Cache
local cached = {
    transparent_icons = TransparentIcons.get(),
    transparent_buttons = TransparentButtons.get(),
    transparent_footer = TransparentFooter.get(),
    transparent_bottombar = TransparentBottomBar.get(),
}

-- Menu
local _ = require("gettext")

local function transparency_menu()
    return {
        text_func = function() return nf_icons.label(nf_icons.OPACITY, _("Transparency")) end,
        sub_item_table = {
            {
                text = _("Make icons transparent"),
                checked_func = TransparentIcons.get,
                callback = function()
                    TransparentIcons.toggle()
                    cached.transparent_icons = TransparentIcons.get()

                    reloadIcons()
                end,
            },
            {
                text = _("Make buttons transparent"),
                checked_func = TransparentButtons.get,
                callback = function()
                    TransparentButtons.toggle()
                    cached.transparent_buttons = TransparentButtons.get()
                end,
            },
            {
                text = _("Make the reader footer transparent"),
                enabled_func = function() return not FooterBackgroundColor.get() end,
                checked_func = TransparentFooter.get,
                callback = function()
                    TransparentFooter.toggle()
                    cached.transparent_footer = TransparentFooter.get()

                    if common.has_document_open() then
                        UIManager:broadcastEvent(Event:new("RefreshFooterBackground"))
                    end
                end,
            },
            {
                text = _("Make the bottom bar transparent"),
                checked_func = TransparentBottomBar.get,
                callback = function()
                    TransparentBottomBar.toggle()
                    cached.transparent_bottombar = TransparentBottomBar.get()
                end,
            },
        },
    }
end

-- Set icon widgets to be transparent after initialization
local original_IconWidget_init = IconWidget.init
function IconWidget:init()
    original_IconWidget_init(self)

    if cached.transparent_icons then
        self.alpha = true
        self.original_in_nightmode = false
    end
end

-- Set buttons to be transparent before painting
local original_Button_paintTo = Button.paintTo
function Button:paintTo(bb, x, y)
    local original_background = self[1].background

    if cached.transparent_buttons and not self.exclude_from_transparency then
        self[1].background = nil
    end

    original_Button_paintTo(self, bb, x, y)

    self[1].background = original_background
end

-- Exclude footer background color changes if option is not set
local original_ReaderFooter_updateFooterContainer = ReaderFooter.updateFooterContainer
function ReaderFooter:updateFooterContainer()
    original_ReaderFooter_updateFooterContainer(self)

    if common.is_excluded(self.footer_content.background) then
        if cached.transparent_footer then
            self.footer_content.background = nil
        end
    end
end

return transparency_menu
