local PT = AhUI
if not PT or not PT.SkinningCheck then return end

local SC = PT.SkinningCheck
local UI = {}
local L = PT.L
PT.UI_SkinningTargets = UI

local FONT = AhUI.FONT

local function GetSkinningDisabledDB()
    PT.config.skinningDisabledTargets =
        PT.config.skinningDisabledTargets or {}
    return PT.config.skinningDisabledTargets
end

function UI:Create()
    if self.frame then return self.frame end

    local f = CreateFrame("Frame", "AhUISkinningTargetsFrame", UIParent, "BackdropTemplate")
    f:SetSize(360, 300)
    f:SetPoint("CENTER")
    f:SetFrameStrata("DIALOG")
    f:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 3,
    })
    f:SetBackdropColor(0, 0, 0, 0.95)
    f:SetBackdropBorderColor(1, 1, 1, 1)
    f:Hide()

    tinsert(UISpecialFrames, f:GetName())

    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)

    local title = f:CreateFontString(nil, "OVERLAY")
    title:SetFont(FONT, 14, "OUTLINE")
    title:SetPoint("TOP", 0, -10)
    title:SetText(L["SKINNING_TARGETS_TITLE"])
    f.title = title

    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -4, -4)

    local scroll = CreateFrame("ScrollFrame", nil, f, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 12, -36)
    scroll:SetPoint("BOTTOMRIGHT", -30, 12)

    local content = CreateFrame("Frame", nil, scroll)
    content:SetSize(1, 1)
    scroll:SetScrollChild(content)

    f.content = content
    self.frame = f

    self:Build()

    return f
end

function UI:Build()
    local content = self.frame.content
    local disabledDB = GetSkinningDisabledDB()

    for _, c in ipairs({ content:GetChildren() }) do
        c:Hide()
    end

    local y = 0

    for _, target in ipairs(SC.TARGETS) do
        local row = CreateFrame("CheckButton", nil, content, "UICheckButtonTemplate")
        row:SetPoint("TOPLEFT", 0, -y)

        local enabled = not disabledDB[target.id]
        row:SetChecked(enabled)

        row.text = row:CreateFontString(nil, "OVERLAY")
        row.text:SetFont(FONT, 12, "")
        row.text:SetPoint("LEFT", row, "RIGHT", 6, 0)
        row.text:SetText(SC:GetTargetName(target))

        row:SetScript("OnClick", function(self)
            disabledDB[target.id] = not self:GetChecked()

            if PT.UI_SkinningCheck then
                PT.UI_SkinningCheck:Update()
            end

            if PT.AccountOverview_Data
               and PT.AccountOverview_Data.SaveCurrentCharacterSkinningSnapshot
            then
                PT.AccountOverview_Data:SaveCurrentCharacterSkinningSnapshot()
            end

            if PT.AccountOverview and PT.AccountOverview.Refresh then
                PT.AccountOverview:Refresh()
            end
        end)

        y = y + 24
    end

    content:SetHeight(y)
end

function UI:Toggle()
    local f = self:Create()
    if f:IsShown() then
        f:Hide()
    else
        f:Show()
    end
end