--[[ UI/Widgets.lua - shared building blocks: panel, button, score chip,
     tag chip, copy box (EditBox auto-highlight - no OS clipboard writes). ]]

local _, ns = ...
local S = ns.Style
local W = {}
ns.Widgets = W

function W.Panel(parent, light)
  local f = CreateFrame("Frame", nil, parent, "BackdropTemplate")
  f:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8", edgeSize = 1 })
  local c = light and S.panelLight or S.panel
  f:SetBackdropColor(c[1], c[2], c[3], c[4])
  f:SetBackdropBorderColor(unpack(S.border))
  return f
end

function W.Text(parent, size, color)
  local fs = parent:CreateFontString(nil, "OVERLAY")
  fs:SetFont(S.font, size or S.size.body, "")
  local c = color or S.text
  fs:SetTextColor(c[1], c[2], c[3])
  fs:SetJustifyH("LEFT")
  return fs
end

function W.Button(parent, label, onClick)
  local b = CreateFrame("Button", nil, parent, "BackdropTemplate")
  b:SetHeight(22)
  b:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8", edgeSize = 1 })
  b:SetBackdropColor(0.16, 0.15, 0.13, 0.95)
  b:SetBackdropBorderColor(unpack(S.border))
  local t = W.Text(b, S.size.small)
  t:SetPoint("CENTER")
  t:SetText(label)
  b.text = t
  b:SetWidth(t:GetStringWidth() + 20)
  b:SetScript("OnEnter", function(self) self:SetBackdropBorderColor(S.gold[1], S.gold[2], S.gold[3], 1) end)
  b:SetScript("OnLeave", function(self) self:SetBackdropBorderColor(unpack(S.border)) end)
  if onClick then b:SetScript("OnClick", onClick) end
  return b
end

function W.ScoreChip(parent)
  local f = CreateFrame("Frame", nil, parent, "BackdropTemplate")
  f:SetSize(44, 20)
  f:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8", edgeSize = 1 })
  f:SetBackdropColor(0, 0, 0, 0.55)
  local t = W.Text(f, S.size.small)
  t:SetPoint("CENTER")
  f.text = t
  function f:SetScore(pct)
    local r, g, b, tag = ns.Score.RingColor(pct)
    self:SetBackdropBorderColor(r, g, b, 1)
    self.text:SetTextColor(r, g, b)
    if tag == "unknown" then self.text:SetText("?")
    elseif tag == "star" then self.text:SetText("100%")
    else self.text:SetText(math.floor(pct * 100 + 0.5) .. "%") end
  end
  return f
end

function W.CopyBox(parent, width)
  local box = CreateFrame("EditBox", nil, parent, "BackdropTemplate")
  box:SetAutoFocus(false)
  box:SetHeight(22)
  box:SetWidth(width or 200)
  box:SetFont(S.font, S.size.small, "")
  box:SetTextColor(unpack(S.text))
  box:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8", edgeSize = 1 })
  box:SetBackdropColor(0, 0, 0, 0.6)
  box:SetBackdropBorderColor(unpack(S.border))
  box:SetTextInsets(6, 6, 0, 0)
  box.value = ""
  local function reselect(self)
    self:SetText(self.value)
    self:HighlightText()
  end
  box:SetScript("OnEditFocusGained", reselect)
  box:SetScript("OnCursorChanged", function(self) self:HighlightText() end)
  box:SetScript("OnTextChanged", function(self, user) if user then reselect(self) end end)
  box:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
  box:SetScript("OnEnterPressed", function(self) self:ClearFocus() end)
  function box:SetValue(v)
    self.value = v or ""
    self:SetText(self.value)
  end
  return box
end

-- "Full Layout", not "House": a house-looking exterior is still just an
-- exterior. The full-layout type means inside AND outside in one code, and
-- Blizzard's own Blueprints tab uses the same words.
local TYPE_LABEL = { house = "Full Layout", interior = "Interior", exterior = "Exterior", room = "Room" }
function W.TypeLabel(t) return TYPE_LABEL[t] or t or "?" end
