local addonName, ns = ...

------------------------------------------------------------
-- Paste box for website exports (HH:1:... lines).
------------------------------------------------------------

local popup = CreateFrame("Frame", "HHImportPopup", UIParent, "BackdropTemplate")
popup:SetSize(420, 280)
popup:SetPoint("CENTER")
popup:SetFrameStrata("FULLSCREEN_DIALOG")
popup:EnableMouse(true)
popup:Hide()
popup:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    edgeSize = 14,
    insets = { left = 3, right = 3, top = 3, bottom = 3 },
})
popup:SetBackdropColor(ns.Theme.BG.r, ns.Theme.BG.g, ns.Theme.BG.b, 0.97)
popup:SetBackdropBorderColor(ns.Theme.BORDER:GetRGBA())
ns._importPopup = popup
ns.RegisterPopup(popup)

local title = popup:CreateFontString(nil, "OVERLAY", "GameFontNormal")
title:SetPoint("TOP", 0, -10)
title:SetText("Import Shopping List")
title:SetTextColor(ns.Theme.HEADING:GetRGBA())

local hint = popup:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
hint:SetPoint("TOP", title, "BOTTOM", 0, -4)
hint:SetText("Paste the export from housing-herald.com")
hint:SetTextColor(ns.Theme.TEXT_MUTED:GetRGBA())

ns.CreateThemedCloseButton(popup)

local scroll = CreateFrame("ScrollFrame", nil, popup, "UIPanelScrollFrameTemplate")
scroll:SetPoint("TOPLEFT", 12, -48)
scroll:SetPoint("BOTTOMRIGHT", -30, 44)

local editBox = CreateFrame("EditBox", nil, scroll)
editBox:SetMultiLine(true)
editBox:SetFontObject("ChatFontNormal")
editBox:SetWidth(370)
editBox:SetAutoFocus(false)
editBox:SetScript("OnEscapePressed", function() popup:Hide() end)
scroll:SetScrollChild(editBox)
ns._importEditBox = editBox

local accept = ns.CreateThemedButton(popup, "Import")
accept:SetSize(110, 24)
accept:SetPoint("BOTTOMRIGHT", -12, 12)
accept:SetScript("OnClick", function()
    local imported, skipped = ns.ShoppingList.Import(editBox:GetText())
    ns.Print(("Imported %d items, skipped %d lines."):format(imported, skipped))
    popup:Hide()
end)
ns._importAcceptButton = accept

local cancel = ns.CreateThemedButton(popup, "Cancel")
cancel:SetSize(110, 24)
cancel:SetPoint("RIGHT", accept, "LEFT", -8, 0)
cancel:SetScript("OnClick", function() popup:Hide() end)

function ns:ShowImportPopup()
    editBox:SetText("")
    ns.ShowPopup(popup)
    editBox:SetFocus()
end
