local PT = AhUI
if not PT then return end
local L = PT.L
local Welcome = {}
PT.WelcomeUI = Welcome

local ADDON_NAME = "AhUI"
local FONT = AhUI.FONT

function Welcome:Create()
    if self.frame then return self.frame end
    PT.config = PT.config or {}

    local defaults = {
        showConcentration     = true,
        skinningCheckEnabled  = false,
        showProfessionsWindow = true,
        confirmOK             = true,
        confirmReload         = false,
        stackAssistEnabled    = false,
    }
    for k, v in pairs(defaults) do
        if PT.config[k] == nil then
            PT.config[k] = v
        end
    end

    AhUI_DB = AhUI_DB or {}
    AhUI_CharConfig = AhUI_CharConfig or {}

    local f = CreateFrame("Frame", "AhUIWelcomeFrame", UIParent, "BackdropTemplate")
    f:SetSize(480, 372)
    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(0.694, 0.612, 0.851, 1)
    f:Hide()
    tinsert(UISpecialFrames, f:GetName())

    local logo = f:CreateTexture(nil, "ARTWORK")
    logo:SetSize(165, 128)
    logo:SetPoint("TOP", 0, -24)
    logo:SetTexture("Interface\\AddOns\\AhUI\\media\\logo_big.tga")

    local text = f:CreateFontString(nil, "OVERLAY")
    text:SetFont(FONT, 14, "")
    text:SetWidth(420)
    text:SetJustifyH("CENTER")
    text:SetJustifyV("TOP")
    text:SetPoint("TOP", logo, "BOTTOM", 0, -16)
    text:SetText(L["WELCOME_TEXT"])

    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -4, -4)
    close:SetScript("OnClick", function()
        AhUI_DB.welcomeShown = true
        f:Hide()
    end)

    local openOptions = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
    openOptions:SetSize(280, 36)
    openOptions:SetPoint("BOTTOM", 0, 28)
    openOptions:SetText(L["WELCOME_OPEN_SETTINGS"])
    do
        local fs = openOptions:GetFontString()
        if fs then fs:SetFont(FONT, 15, "") end
    end
    openOptions:SetScript("OnClick", function()
        AhUI_DB.welcomeShown = true
        AhUI_CharConfig.__welcomeApplied = true
        if PT.ConfirmButtons then
            PT.ConfirmButtons:SetOKEnabled(PT.config.confirmOK == true)
            PT.ConfirmButtons:SetReloadEnabled(PT.config.confirmReload == true)
        end
        f:Hide()
        if PT.Settings and PT.Settings.category then
            Settings.OpenToCategory(PT.Settings.category:GetID())
        end
    end)

    self.frame = f
    return f
end

function Welcome:Refresh()
end

function Welcome:Show()
    self:Create():Show()
end

SLASH_AHUIWELCOME1 = "/phwelcome"
SLASH_AHUIWELCOME2 = "/phwel"
SLASH_AHUIWELCOME3 = "/ahuiwelcome"
SLASH_AHUIWELCOME4 = "/ahwelcome"

SlashCmdList["AHUIWELCOME"] = function()
    if PT.WelcomeUI then
        PT.WelcomeUI:Show()
    end
end

local ev = CreateFrame("Frame")
ev:RegisterEvent("PLAYER_LOGIN")
ev:SetScript("OnEvent", function()
    AhUI_DB = AhUI_DB or {}

    if not AhUI_DB.welcomeShown then
        AhUI_DB.welcomeShown = true
        C_Timer.After(0.5, function()
            Welcome:Create():Show()
        end)
    end
end)
