local _, addon = ...
local HLY = addon.HLY

local C_Timer = C_Timer
local _G = _G

local DIFF_LFR       = 17
local DIFF_NORMAL    = 14
local DIFF_HEROIC    = 15
local DIFF_MYTHIC    = 16
local DIFF_MPLUS     = 8
local DIFF_DELVE     = 33
local DIFF_OPENWORLD = 0

local function TryBonusRollPass()
    local f = _G.BonusRollFrame
    if not f or not f:IsShown() then return end

    local db = HLY.db.bonusRoll
    if not db or not db.enabled then return end

    local difficultyID = f.difficultyID
    local shouldPass = false

    if     difficultyID == DIFF_LFR    and db.passLFR    then shouldPass = true
    elseif difficultyID == DIFF_NORMAL and db.passNormal then shouldPass = true
    elseif difficultyID == DIFF_HEROIC and db.passHeroic then shouldPass = true
    elseif difficultyID == DIFF_MYTHIC and db.passMythic then shouldPass = true
    elseif difficultyID == DIFF_MPLUS  and db.passMplus  then shouldPass = true
    elseif difficultyID == DIFF_DELVE     and db.passDelve     then shouldPass = true
    elseif difficultyID == DIFF_OPENWORLD and db.passOpenWorld then shouldPass = true
    end

    if not shouldPass then return end

    local passButton = f.PromptFrame and f.PromptFrame.PassButton
    if passButton and passButton:IsEnabled() then
        passButton:Click()
        print(addon.brand .. " Bonus Roll: passed.")
    end
end

function HLY:SetupBonusRollHook()
    if self.bonusRollHooked then return end

    local f = _G.BonusRollFrame
    if not f or not f.PromptFrame then return end

    f.PromptFrame:HookScript("OnShow", TryBonusRollPass)
    self.bonusRollHooked = true
end

function HLY:HandleBonusRollStarted()
    self:SetupBonusRollHook()
    C_Timer.After(0.5, TryBonusRollPass)
end

function HLY:SetupBonusRoll()
    if self.bonusRollSetup then
        return
    end

    self:SetupBonusRollHook()
    self:RegisterEvent("BONUS_ROLL_STARTED", "HandleBonusRollStarted")

    self.bonusRollSetup = true
end

-- LibHLYUI options pane (v2) ------------------------------------------------
function HLY:BuildBonusRollOptions(tab)
    tab:Note("Passt den Bonus-Würfelwurf automatisch für die ausgewählten Schwierigkeitsgrade.")
    tab:Checkbox({
        label = "Enable",
        get = function() return self.db.bonusRoll.enabled end,
        set = function(v) self.db.bonusRoll.enabled = v end,
    })
    tab:Section("Raids")
    tab:Row({
        function(c) c:Checkbox({ label = "LFR", get = function() return self.db.bonusRoll.passLFR end, set = function(v) self.db.bonusRoll.passLFR = v end }) end,
        function(c) c:Checkbox({ label = "Normal", get = function() return self.db.bonusRoll.passNormal end, set = function(v) self.db.bonusRoll.passNormal = v end }) end,
        function(c) c:Checkbox({ label = "Heroic", get = function() return self.db.bonusRoll.passHeroic end, set = function(v) self.db.bonusRoll.passHeroic = v end }) end,
        function(c) c:Checkbox({ label = "Mythic", get = function() return self.db.bonusRoll.passMythic end, set = function(v) self.db.bonusRoll.passMythic = v end }) end,
    })
    tab:Section("Other")
    tab:Row({
        function(c) c:Checkbox({ label = "M+", get = function() return self.db.bonusRoll.passMplus end, set = function(v) self.db.bonusRoll.passMplus = v end }) end,
        function(c) c:Checkbox({ label = "Delve", get = function() return self.db.bonusRoll.passDelve end, set = function(v) self.db.bonusRoll.passDelve = v end }) end,
        function(c) c:Checkbox({ label = "Open World", get = function() return self.db.bonusRoll.passOpenWorld end, set = function(v) self.db.bonusRoll.passOpenWorld = v end }) end,
    }, 4)
end

addon.options = addon.options or {}
addon.options[#addon.options + 1] = {
    group = "Other", groupOrder = 800, name = "Bonus Roll", order = 3, groupAsEntry = true,
    build = "BuildBonusRollOptions",
}
