-- TOG Tools — I'd Hit That tab (UI)
-- Settings panel for the IdHitThat module, rendered inside the main window.

local _, addon = ...
local AceGUI = LibStub("AceGUI-3.0")
local gv = addon.gameVersion

local BRAND = "|cffFF8000"

-- ---------------------------------------------------------------------------
-- Tab module table — merges UI fields into the engine table registered by
-- Modules/IdHitThat/IdHitThat.lua (which loads before this file).
-- ---------------------------------------------------------------------------
local Tab = addon.modules["idhitthat"]
Tab.tabKey      = "idhitthat"
Tab.configKey   = "idHitThat"
Tab.label       = "I'd Hit That"
Tab.WINDOW_SIZE = { width = 560, height = 480, locked = true }
Tab.help = {
    title = "I'd Hit That",
    lines = {
        "Shows your chance to hit whatever you have targeted, on screen, at a glance.",
        "Separate numbers for special attacks, white swings and off-hand swings — because dual wielding penalises white swings only.",
        "Green means you're at the hit cap for that target. Amber is close. Red means you're losing attacks.",
        "It builds itself into your target frame rather than floating over your screen — matching ZPerl's box styling, ElvUI's aura sizing, or Blizzard's debuff grid. Stack it as a column or a row, on whichever side you like.",
        "Costs nothing while idle: the numbers are only recalculated when your target, gear, talents or buffs actually change.",
    },
}

local function cfg()
    return addon.DB.global.idHitThat
end

-- ---------------------------------------------------------------------------
-- Draw
-- ---------------------------------------------------------------------------
function Tab:Draw(container)
    local c = cfg()

    local scroll = AceGUI:Create("ScrollFrame")
    scroll:SetLayout("Flow")
    scroll:SetFullWidth(true)
    scroll:SetFullHeight(true)
    container:AddChild(scroll)

    -- Retail removed hit as a stat entirely, so there is nothing to tune.
    -- Say so plainly rather than showing controls that do nothing.
    if gv.isRetail then
        local note = AceGUI:Create("Label")
        note:SetFullWidth(true)
        note:SetText("\n" .. BRAND .. "Not applicable on this version.|r\n\n"
            .. "Hit chance was removed as a stat in Warlords of Draenor. Your attacks always land, "
            .. "so there is nothing to display.\n")
        scroll:AddChild(note)
        return
    end

    -- -----------------------------------------------------------------------
    -- Enable
    -- -----------------------------------------------------------------------
    local enable = AceGUI:Create("CheckBox")
    enable:SetLabel("Show the hit chance display")
    -- `~= false`: a missing flag means enabled. See GUI/NamePrefixTab.lua for why.
    enable:SetValue(c.enabled ~= false)
    enable:SetFullWidth(true)
    enable:SetCallback("OnValueChanged", function(_, _, val)
        c.enabled = val
        Tab:UpdateHUD()
    end)
    scroll:AddChild(enable)

    -- -----------------------------------------------------------------------
    -- Live readout — the current numbers, so the settings can be verified
    -- against a real target without alt-tabbing out of the window.
    -- -----------------------------------------------------------------------
    local readout = AceGUI:Create("Label")
    readout:SetFullWidth(true)
    local r = Tab:GetResult()
    if r.valid then
        readout:SetText(string.format(
            "\n" .. BRAND .. "Current target:|r %s\n"
            .. "Specials %.1f%%   White %.1f%%%s\n",
            r.targetName or "?", r.special, r.whiteMain,
            r.whiteOff > 0 and string.format("   Off-hand %.1f%%", r.whiteOff) or ""))
    else
        readout:SetText("\n|cffaaaaaaNo attackable target selected.|r\n")
    end
    scroll:AddChild(readout)

    -- -----------------------------------------------------------------------
    -- Placement
    -- -----------------------------------------------------------------------
    local heading = AceGUI:Create("Heading")
    heading:SetText("Placement")
    heading:SetFullWidth(true)
    scroll:AddChild(heading)

    local anchorNote = AceGUI:Create("Label")
    anchorNote:SetFullWidth(true)
    anchorNote:SetText("|cffaaaaaaThe readout is always part of your target frame — it is never a "
        .. "window, can't be dragged, and moves, scales and disappears along with the frame it "
        .. "belongs to. With ZPerl it borrows the same bubble styling, height and row spacing as the "
        .. "level and combo-point boxes; with ElvUI it matches your configured aura size and spacing; "
        .. "with the default frames it matches Blizzard's own debuff grid. Other unit-frame addons "
        .. "get sensible defaults. If your target frame hasn't appeared yet it shows nothing for a "
        .. "moment rather than floating somewhere else.|r\n")
    scroll:AddChild(anchorNote)

    local side = AceGUI:Create("Dropdown")
    side:SetLabel("Which side")
    side:SetList({
        right = "Right of the frame",
        left  = "Left of the frame",
        below = "Below the frame",
    }, { "right", "left", "below" })
    side:SetValue(c.side or "right")
    side:SetWidth(260)
    side:SetCallback("OnValueChanged", function(_, _, val)
        c.side = val
        Tab:UpdateHUD()
    end)
    scroll:AddChild(side)

    local layout = AceGUI:Create("Dropdown")
    layout:SetLabel("Arrangement")
    layout:SetList({
        vertical   = "Stacked in a column",
        horizontal = "Side by side in a row",
    }, { "vertical", "horizontal" })
    layout:SetValue(c.layout or "vertical")
    layout:SetWidth(260)
    layout:SetCallback("OnValueChanged", function(_, _, val)
        c.layout = val
        Tab:UpdateHUD()
    end)
    scroll:AddChild(layout)

    -- -----------------------------------------------------------------------
    -- Rows
    -- -----------------------------------------------------------------------
    local rowsHeading = AceGUI:Create("Heading")
    rowsHeading:SetText("What to show")
    rowsHeading:SetFullWidth(true)
    scroll:AddChild(rowsHeading)

    local white = AceGUI:Create("CheckBox")
    white:SetLabel("White swings (and off-hand when dual wielding)")
    white:SetValue(c.showWhite)
    white:SetFullWidth(true)
    white:SetCallback("OnValueChanged", function(_, _, val)
        c.showWhite = val
        Tab:UpdateHUD()
    end)
    scroll:AddChild(white)

    local ranged = AceGUI:Create("CheckBox")
    ranged:SetLabel("Ranged shots (bows, guns, crossbows, thrown, wands)")
    ranged:SetValue(c.showRanged)
    ranged:SetFullWidth(true)
    ranged:SetCallback("OnValueChanged", function(_, _, val)
        c.showRanged = val
        Tab:UpdateHUD()
    end)
    scroll:AddChild(ranged)

    local rangedNote = AceGUI:Create("Label")
    rangedNote:SetFullWidth(true)
    rangedNote:SetText("|cffaaaaaaOnly appears when you actually have a ranged weapon equipped. "
        .. "Librams, totems and idols use the same slot but aren't weapons, so they don't count.|r\n")
    scroll:AddChild(rangedNote)

    local spell = AceGUI:Create("CheckBox")
    spell:SetLabel("Spell hit chance")
    spell:SetValue(c.showSpell)
    spell:SetFullWidth(true)
    spell:SetCallback("OnValueChanged", function(_, _, val)
        c.showSpell = val
        Tab:UpdateHUD()
    end)
    scroll:AddChild(spell)
end
