local ADDON_NAME, ns = ...
ns.preview = ns.preview or {}
local P = ns.preview

local U = ns.util

function P:Create(parent, skins, onRefresh)
    if self.frame then return self.frame end

    local wrap = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    wrap:SetSize(360, 190)
    wrap:SetBackdrop({ bgFile="Interface\\Buttons\\WHITE8x8", edgeFile="Interface\\Buttons\\WHITE8x8", edgeSize=1 })
    wrap:SetBackdropColor(0,0,0,0.35)
    wrap:SetBackdropBorderColor(0.4,0.4,0.4,1)

    local title = wrap:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    title:SetPoint("TOPLEFT", wrap, "TOPLEFT", 10, -8)
    title:SetText("Preview")

    local uf = CreateFrame("Frame", nil, wrap)
    uf:SetPoint("TOPLEFT", wrap, "TOPLEFT", 14, -30)
    uf:SetSize(330, 120)

    local hb = CreateFrame("StatusBar", nil, uf)
    hb:SetPoint("TOPLEFT", uf, "TOPLEFT", 0, 0)
    hb:SetSize(330, 18)
    hb:SetMinMaxValues(0, 100)
    hb:SetValue(87)
    uf.healthBar = hb

    local cb = CreateFrame("StatusBar", nil, uf)
    cb:SetPoint("TOPLEFT", hb, "BOTTOMLEFT", 0, -6)
    cb:SetSize(330, 12)
    cb:SetMinMaxValues(0, 100)
    cb:SetValue(55)
    uf.castBar = cb

    cb.Text = cb:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    cb.Text:SetText("Shadow Bolt")
    cb.Text:Show()

    local prevTarget = CreateFrame("CheckButton", nil, wrap, "UICheckButtonTemplate")
    prevTarget:SetPoint("BOTTOMLEFT", wrap, "BOTTOMLEFT", 8, 10)
    prevTarget.text:SetText("Preview as Target")
    prevTarget:SetChecked(true)

    wrap.uf = uf
    wrap.hb = hb
    wrap.cb = cb
    wrap.prevTarget = prevTarget
    wrap._skins = skins

    wrap.Refresh = function()
        if onRefresh then onRefresh(wrap) end
    end

    prevTarget:SetScript("OnClick", function()
        wrap:Refresh()
    end)

    self.frame = wrap
    return wrap
end
