local addonName, TOAST = ...

local GOLD = { 0.88, 0.75, 0.36 }
local BORDER = { 1, 1, 1, 0.12 }
local COMMUNITY_INVITE = "https://www.worldofwarcraft.com/invite/pPmMXpyCPoX?region=EU&faction=Horde"
local DISCORD_INVITE = "https://discord.gg/tide-crew"

-- Layout constants: the popup height is derived from these (see ApplyMode)
-- instead of a fixed frame size, so short modes (community-only) don't leave
-- a dead zone between the button and the checkbox.
local W = 440
local PAD = 16
local HEADER_H = 58
local DIVIDER_Y = -(HEADER_H + 1)  -- same "+1" convention as dashboard/leaderboard
local DIVIDER_MEDALLION = 18
local DIVIDER_GAP = 4
local GAP_DIVIDER_BODY = 14
local GAP_BODY_SECTION = 16
local GAP_LABEL_INSTR = 4
local GAP_INSTR_BOX = 8
local GAP_SECTION = 20
local BOX_W = W - PAD * 2
local BOX_H = 30
local BUTTON_W, BUTTON_H = 240, 44
local GAP_BUTTON_CHECK = 18
local CHECK_SIZE = 18
local BOTTOM_PAD = 16

local BUTTON_STYLES = {
    gold = {
        bg      = { 0.88, 0.75, 0.36, 0.15 },
        bgHover = { 0.88, 0.75, 0.36, 0.28 },
        border  = { 0.88, 0.75, 0.36, 1 },
        text    = { 0.88, 0.75, 0.36 },
    },
    grey = {
        bg      = { 1, 1, 1, 0.06 },
        bgHover = { 1, 1, 1, 0.12 },
        border  = { 1, 1, 1, 0.18 },
        text    = { 1, 1, 1 },
    },
}

local MODES = {
    community = {
        title       = "Join TIDE and Crew",
        body        = "Join the in-game community to unlock full TOAST functionality.\nKeys, scores and crew features work best when you are part of TIDE and Crew.",
        showCommunity = true,
        showDiscord   = false,
    },
    discord = {
        title       = "Join The SEAMEN",
        body        = "You're in TIDE and Crew but not yet linked in Discord.\nJoin our Discord to show up on the leaderboard.",
        showCommunity = false,
        showDiscord   = true,
    },
    both = {
        title       = "Join The SEAMEN",
        body        = "This character is not in the SEAMEN roster yet.\nJoin the community and Discord to get started.",
        showCommunity = true,
        showDiscord   = true,
    },
}

local frame
local header
local title, body
local communityLabel, communityInstruction, communityBox, communityEditBox
local discordLabel, discordBox, discordEditBox
local hopBtn
local checkbox, checkboxLabel

local function LockEditBox(eb, text)
    eb:SetAutoFocus(false)
    eb:SetText(text)
    eb:SetScript("OnEditFocusGained", function(self) self:HighlightText() end)
    eb:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
    eb:SetScript("OnChar", function(self)
        self:SetText(text)
        self:HighlightText()
    end)
end

local function CreateActionButton(parent, label, style, width, height)
    local s = BUTTON_STYLES[style]

    local btn = CreateFrame("Button", nil, parent, "BackdropTemplate")
    Mixin(btn, BackdropTemplateMixin)
    btn:SetSize(width, height)

    local bg = btn:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(unpack(s.bg))

    local top = btn:CreateTexture(nil, "BORDER")
    top:SetColorTexture(unpack(s.border))
    top:SetPoint("TOPLEFT", btn, "TOPLEFT", 0, 0)
    top:SetPoint("TOPRIGHT", btn, "TOPRIGHT", 0, 0)
    top:SetHeight(1)

    local bottom = btn:CreateTexture(nil, "BORDER")
    bottom:SetColorTexture(unpack(s.border))
    bottom:SetPoint("BOTTOMLEFT", btn, "BOTTOMLEFT", 0, 0)
    bottom:SetPoint("BOTTOMRIGHT", btn, "BOTTOMRIGHT", 0, 0)
    bottom:SetHeight(1)

    local left = btn:CreateTexture(nil, "BORDER")
    left:SetColorTexture(unpack(s.border))
    left:SetPoint("TOPLEFT", btn, "TOPLEFT", 0, 0)
    left:SetPoint("BOTTOMLEFT", btn, "BOTTOMLEFT", 0, 0)
    left:SetWidth(1)

    local right = btn:CreateTexture(nil, "BORDER")
    right:SetColorTexture(unpack(s.border))
    right:SetPoint("TOPRIGHT", btn, "TOPRIGHT", 0, 0)
    right:SetPoint("BOTTOMRIGHT", btn, "BOTTOMRIGHT", 0, 0)
    right:SetWidth(1)

    local text = btn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    text:SetPoint("CENTER")
    text:SetText(label)
    text:SetTextColor(unpack(s.text))

    btn:SetScript("OnEnter", function() bg:SetColorTexture(unpack(s.bgHover)) end)
    btn:SetScript("OnLeave", function() bg:SetColorTexture(unpack(s.bg)) end)

    return btn, text
end

TOAST.LockEditBox = LockEditBox
TOAST.CreateActionButton = CreateActionButton

-- Dark, bordered link box with a gold-on-focus border, replacing the default
-- Blizzard InputBoxTemplate look. Border texture pattern mirrors the
-- leaderboard search box so both speak the same "custom input" language.
local function CreateInviteBox(parent, width, height)
    local box = CreateFrame("Frame", nil, parent)
    box:SetSize(width, height)

    local bg = box:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.04, 0.05, 0.07, 0.9)

    local topB = box:CreateTexture(nil, "BORDER")
    topB:SetPoint("TOPLEFT", box, "TOPLEFT", 0, 0)
    topB:SetPoint("TOPRIGHT", box, "TOPRIGHT", 0, 0)
    topB:SetHeight(1)

    local bottomB = box:CreateTexture(nil, "BORDER")
    bottomB:SetPoint("BOTTOMLEFT", box, "BOTTOMLEFT", 0, 0)
    bottomB:SetPoint("BOTTOMRIGHT", box, "BOTTOMRIGHT", 0, 0)
    bottomB:SetHeight(1)

    local leftB = box:CreateTexture(nil, "BORDER")
    leftB:SetPoint("TOPLEFT", box, "TOPLEFT", 0, 0)
    leftB:SetPoint("BOTTOMLEFT", box, "BOTTOMLEFT", 0, 0)
    leftB:SetWidth(1)

    local rightB = box:CreateTexture(nil, "BORDER")
    rightB:SetPoint("TOPRIGHT", box, "TOPRIGHT", 0, 0)
    rightB:SetPoint("BOTTOMRIGHT", box, "BOTTOMRIGHT", 0, 0)
    rightB:SetWidth(1)

    local function SetBoxBorder(r, g, b, a)
        topB:SetColorTexture(r, g, b, a)
        bottomB:SetColorTexture(r, g, b, a)
        leftB:SetColorTexture(r, g, b, a)
        rightB:SetColorTexture(r, g, b, a)
    end
    SetBoxBorder(1, 1, 1, 0.14)

    local eb = CreateFrame("EditBox", nil, box)
    eb:SetAllPoints()
    eb:SetFont("Fonts\\FRIZQT__.TTF", 12, "")
    eb:SetTextColor(0.92, 0.92, 0.90, 1)
    eb:SetTextInsets(10, 10, 0, 0)
    eb:SetAutoFocus(false)

    eb:HookScript("OnEditFocusGained", function() SetBoxBorder(GOLD[1], GOLD[2], GOLD[3], 0.9) end)
    eb:HookScript("OnEditFocusLost", function() SetBoxBorder(1, 1, 1, 0.14) end)

    return box, eb
end

-- Small checkbox: dark fill, muted grey border, gold fill when checked —
-- replaces the oversized default UICheckButtonTemplate art.
local function CreateStyledCheckbox(parent, size)
    local cb = CreateFrame("CheckButton", nil, parent)
    cb:SetSize(size, size)

    local bg = cb:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.04, 0.05, 0.07, 1)

    local topB = cb:CreateTexture(nil, "BORDER")
    topB:SetColorTexture(1, 1, 1, 0.25)
    topB:SetPoint("TOPLEFT", cb, "TOPLEFT", 0, 0)
    topB:SetPoint("TOPRIGHT", cb, "TOPRIGHT", 0, 0)
    topB:SetHeight(1)

    local bottomB = cb:CreateTexture(nil, "BORDER")
    bottomB:SetColorTexture(1, 1, 1, 0.25)
    bottomB:SetPoint("BOTTOMLEFT", cb, "BOTTOMLEFT", 0, 0)
    bottomB:SetPoint("BOTTOMRIGHT", cb, "BOTTOMRIGHT", 0, 0)
    bottomB:SetHeight(1)

    local leftB = cb:CreateTexture(nil, "BORDER")
    leftB:SetColorTexture(1, 1, 1, 0.25)
    leftB:SetPoint("TOPLEFT", cb, "TOPLEFT", 0, 0)
    leftB:SetPoint("BOTTOMLEFT", cb, "BOTTOMLEFT", 0, 0)
    leftB:SetWidth(1)

    local rightB = cb:CreateTexture(nil, "BORDER")
    rightB:SetColorTexture(1, 1, 1, 0.25)
    rightB:SetPoint("TOPRIGHT", cb, "TOPRIGHT", 0, 0)
    rightB:SetPoint("BOTTOMRIGHT", cb, "BOTTOMRIGHT", 0, 0)
    rightB:SetWidth(1)

    local checked = cb:CreateTexture(nil, "ARTWORK")
    checked:SetPoint("TOPLEFT", cb, "TOPLEFT", 3, -3)
    checked:SetPoint("BOTTOMRIGHT", cb, "BOTTOMRIGHT", -3, 3)
    checked:SetColorTexture(GOLD[1], GOLD[2], GOLD[3], 1)
    cb:SetCheckedTexture(checked)

    return cb
end

-- Every element below the header divider is re-anchored here using a running
-- cursor (in frame-local Y), and the frame height is derived from where the
-- cursor ends up. This is what keeps community-only mode short instead of
-- leaving the community/discord/both variants sharing one fixed tall frame.
local function ApplyMode(mode)
    local cfg = MODES[mode]

    title:SetText(cfg.title)
    body:SetText(cfg.body)

    local y = DIVIDER_Y - GAP_DIVIDER_BODY
    body:ClearAllPoints()
    body:SetPoint("TOP", frame, "TOPLEFT", W / 2, y)
    y = y - body:GetStringHeight()

    if cfg.showCommunity then
        y = y - GAP_BODY_SECTION
        communityLabel:ClearAllPoints()
        communityLabel:SetPoint("TOP", frame, "TOPLEFT", W / 2, y)
        communityLabel:Show()
        y = y - communityLabel:GetStringHeight()

        y = y - GAP_LABEL_INSTR
        communityInstruction:ClearAllPoints()
        communityInstruction:SetPoint("TOP", frame, "TOPLEFT", W / 2, y)
        communityInstruction:Show()
        y = y - communityInstruction:GetStringHeight()

        y = y - GAP_INSTR_BOX
        communityBox:ClearAllPoints()
        communityBox:SetPoint("TOP", frame, "TOPLEFT", W / 2, y)
        communityBox:Show()
        y = y - BOX_H
    else
        communityLabel:Hide()
        communityInstruction:Hide()
        communityBox:Hide()
    end

    if cfg.showDiscord then
        y = y - GAP_SECTION
        discordLabel:ClearAllPoints()
        discordLabel:SetPoint("TOP", frame, "TOPLEFT", W / 2, y)
        discordLabel:Show()
        y = y - discordLabel:GetStringHeight()

        y = y - GAP_LABEL_INSTR
        discordBox:ClearAllPoints()
        discordBox:SetPoint("TOP", frame, "TOPLEFT", W / 2, y)
        discordBox:Show()
        y = y - BOX_H
    else
        discordLabel:Hide()
        discordBox:Hide()
    end

    y = y - GAP_SECTION
    hopBtn:ClearAllPoints()
    hopBtn:SetPoint("TOP", frame, "TOPLEFT", W / 2, y)
    y = y - BUTTON_H

    y = y - GAP_BUTTON_CHECK
    checkbox:ClearAllPoints()
    checkbox:SetPoint("TOPLEFT", frame, "TOPLEFT", PAD, y)

    frame:SetHeight(-y + CHECK_SIZE + BOTTOM_PAD)
end

local function BuildWelcome()
    frame = CreateFrame("Frame", "TOASTWelcome", UIParent, "BackdropTemplate")
    Mixin(frame, BackdropTemplateMixin)
    frame:SetSize(W, 400)
    frame:SetPoint("CENTER")
    frame:SetMovable(true)
    frame:EnableMouse(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
    frame:SetFrameStrata("DIALOG")
    frame:Hide()
    tinsert(UISpecialFrames, "TOASTWelcome")

    local bg = frame:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.06, 0.08, 0.10, 1.0)

    local topBorder = frame:CreateTexture(nil, "BORDER")
    topBorder:SetColorTexture(BORDER[1], BORDER[2], BORDER[3], BORDER[4])
    topBorder:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
    topBorder:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
    topBorder:SetHeight(1)

    local bottomBorder = frame:CreateTexture(nil, "BORDER")
    bottomBorder:SetColorTexture(BORDER[1], BORDER[2], BORDER[3], BORDER[4])
    bottomBorder:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
    bottomBorder:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
    bottomBorder:SetHeight(1)

    local leftBorder = frame:CreateTexture(nil, "BORDER")
    leftBorder:SetColorTexture(BORDER[1], BORDER[2], BORDER[3], BORDER[4])
    leftBorder:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
    leftBorder:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
    leftBorder:SetWidth(1)

    local rightBorder = frame:CreateTexture(nil, "BORDER")
    rightBorder:SetColorTexture(BORDER[1], BORDER[2], BORDER[3], BORDER[4])
    rightBorder:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
    rightBorder:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
    rightBorder:SetWidth(1)

    -- Subtle top gold line — shouldn't compete with the header divider below it.
    local accent = frame:CreateTexture(nil, "ARTWORK")
    accent:SetColorTexture(GOLD[1], GOLD[2], GOLD[3], 0.85)
    accent:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
    accent:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
    accent:SetHeight(1)

    TOAST.CreateCloseButton(frame, function() frame:Hide() end)

    -- ── Header: compact banner + logo + title, same language as
    -- dashboard/leaderboard headers (left logo, title/subtitle to its right).
    header = CreateFrame("Frame", nil, frame)
    header:SetSize(W, HEADER_H)
    header:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)

    local banner = header:CreateTexture(nil, "BACKGROUND")
    banner:SetTexture("Interface\\AddOns\\TOAST\\media\\banners\\tentakel_2.png")
    banner:SetTexCoord(0, 1, 0, 1)
    banner:SetAllPoints()
    banner:SetVertexColor(1, 1, 1, 0.35)

    local logo = header:CreateTexture(nil, "ARTWORK")
    logo:SetTexture("Interface\\AddOns\\TOAST\\media\\discord\\toast_128.tga")
    logo:SetSize(40, 40)
    logo:SetPoint("TOPLEFT", header, "TOPLEFT", 12, -9)

    title = header:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", logo, "TOPRIGHT", 8, -4)
    title:SetTextColor(GOLD[1], GOLD[2], GOLD[3])

    local subtitle = header:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    subtitle:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -3)
    subtitle:SetTextColor(1, 1, 1, 0.45)
    subtitle:SetText("An open Mythic+ community")

    -- Divider levels above the header so the medallion draws over the banner.
    TOAST.CreateAnchorDivider(frame, PAD, W - PAD, DIVIDER_Y, {
        medallionSize = DIVIDER_MEDALLION,
        gap = DIVIDER_GAP,
        lineAlpha = 0.85,
        aboveFrame = header,
    })

    -- ── Body copy ───────────────────────────────────────────────────
    body = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    body:SetWidth(W - PAD * 2)
    body:SetJustifyH("CENTER")
    body:SetTextColor(1, 1, 1, 0.6)

    -- ── Community invite section ────────────────────────────────────
    communityLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    communityLabel:SetTextColor(GOLD[1], GOLD[2], GOLD[3])
    communityLabel:SetText("Community Invite")

    communityInstruction = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    communityInstruction:SetTextColor(1, 1, 1, 0.45)
    communityInstruction:SetText("Copy this link into your browser to get the in-game join code.")

    communityBox, communityEditBox = CreateInviteBox(frame, BOX_W, BOX_H)
    LockEditBox(communityEditBox, COMMUNITY_INVITE)

    -- ── Discord section ──────────────────────────────────────────────
    discordLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    discordLabel:SetTextColor(GOLD[1], GOLD[2], GOLD[3])
    discordLabel:SetText("Discord")

    discordBox, discordEditBox = CreateInviteBox(frame, BOX_W, BOX_H)
    LockEditBox(discordEditBox, DISCORD_INVITE)

    -- ── Main action button ───────────────────────────────────────────
    hopBtn = CreateActionButton(frame, "Set Sail", "gold", BUTTON_W, BUTTON_H)
    hopBtn:SetScript("OnClick", function()
        frame:Hide()
        TOAST.commands["dashboard"]()
    end)

    -- ── Dismiss checkbox ─────────────────────────────────────────────
    checkbox = CreateStyledCheckbox(frame, CHECK_SIZE)
    checkbox:SetScript("OnClick", function()
        TOAST_DB.dismissedWelcome = TOAST_DB.dismissedWelcome or {}
        TOAST_DB.dismissedWelcome[UnitName("player")] = true
    end)

    checkboxLabel = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    checkboxLabel:SetPoint("LEFT", checkbox, "RIGHT", 6, 0)
    checkboxLabel:SetTextColor(1, 1, 1, 0.5)
    checkboxLabel:SetText("Don't show this again for this character")
end

function TOAST.ShowWelcome(mode)
    mode = mode or "both"
    if not frame then
        BuildWelcome()
    end
    ApplyMode(mode)
    frame:Show()
end

_G.TOAST_TEST_WELCOME = TOAST.ShowWelcome
