local addonName, ns = ...
local L = ns.L

local function GetVersion()
    if C_AddOns and C_AddOns.GetAddOnMetadata then
        return C_AddOns.GetAddOnMetadata(addonName, "Version") or "1.0.3"
    end
    if GetAddOnMetadata then
        return GetAddOnMetadata(addonName, "Version") or "1.0.3"
    end
    return "1.0.3"
end

local EN = {
    "Initial public release of ChatFloater.",
    "Compact chat channel and utility bar with horizontal and vertical layouts.",
    "Dedicated keybind for cycling through enabled chat channels.",
    "Custom border textures, LibSharedMedia support and built-in color picker.",
    "BigChatBox integration with automatic Blizzard chat fallback.",
}

local RU = {
    "Первый публичный релиз ChatFloater.",
    "Компактная панель каналов чата и служебных кнопок с горизонтальным и вертикальным режимами.",
    "Отдельная клавиша для переключения включённых каналов чата.",
    "Настраиваемые рамки, поддержка LibSharedMedia и встроенный выбор цвета.",
    "Интеграция с BigChatBox с автоматическим возвратом к чату Blizzard.",
}

local function ClassColor()
    local classFile = select(2, UnitClass("player"))
    if C_ClassColor and C_ClassColor.GetClassColor and classFile then
        local c = C_ClassColor.GetClassColor(classFile)
        if c then return c.r, c.g, c.b end
    end
    if RAID_CLASS_COLORS and classFile and RAID_CLASS_COLORS[classFile] then
        local c = RAID_CLASS_COLORS[classFile]
        return c.r, c.g, c.b
    end
    return 1, 1, 1
end

local function SoftButton(parent, text, width, height)
    local b = CreateFrame("Button", nil, parent, "BackdropTemplate")
    b:SetSize(width, height)
    b:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Buttons\\WHITE8X8",
        edgeSize = 1,
    })
    b:SetBackdropColor(1, 1, 1, 0.025)
    b:SetBackdropBorderColor(1, 1, 1, 0.10)

    local fs = b:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    fs:SetPoint("CENTER")
    fs:SetText(text)
    fs:SetTextColor(0.90, 0.93, 0.98, 0.92)

    b:SetScript("OnEnter", function(self)
        local r, g, bl = ClassColor()
        self:SetBackdropColor(1, 1, 1, 0.055)
        self:SetBackdropBorderColor(r, g, bl, 0.70)
    end)
    b:SetScript("OnLeave", function(self)
        self:SetBackdropColor(1, 1, 1, 0.025)
        self:SetBackdropBorderColor(1, 1, 1, 0.10)
    end)
    return b
end

local function CloseButton(parent)
    local b = SoftButton(parent, "", 22, 22)
    b:SetPoint("TOPRIGHT", -10, -10)

    local a = b:CreateTexture(nil, "OVERLAY")
    local c = b:CreateTexture(nil, "OVERLAY")
    a:SetTexture("Interface\\Buttons\\WHITE8X8")
    c:SetTexture("Interface\\Buttons\\WHITE8X8")
    a:SetSize(11, 1)
    c:SetSize(11, 1)
    a:SetPoint("CENTER")
    c:SetPoint("CENTER")
    a:SetRotation(math.rad(45))
    c:SetRotation(math.rad(-45))
    a:SetVertexColor(1, 1, 1, 0.82)
    c:SetVertexColor(1, 1, 1, 0.82)

    b:HookScript("OnEnter", function()
        local r, g, bl = ClassColor()
        a:SetVertexColor(r, g, bl, 1)
        c:SetVertexColor(r, g, bl, 1)
    end)
    b:HookScript("OnLeave", function()
        a:SetVertexColor(1, 1, 1, 0.82)
        c:SetVertexColor(1, 1, 1, 0.82)
    end)
    b:SetScript("OnClick", function() parent:Hide() end)
    return b
end

local function Show()
    if not ChatFloaterDB then return end
    local version = GetVersion()
    if ChatFloaterDB.lastSeenVersion == version then return end
    ChatFloaterDB.lastSeenVersion = version

    local lines = GetLocale() == "ruRU" and RU or EN
    local f = CreateFrame("Frame", "ChatFloaterChangelogFrame", UIParent, "BackdropTemplate")
    f:SetSize(520, 260)
    f:SetPoint("CENTER")
    f:SetFrameStrata("DIALOG")
    f:SetToplevel(true)
    f:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Buttons\\WHITE8X8",
        edgeSize = 1,
    })
    f:SetBackdropColor(0.018, 0.020, 0.026, 0.985)
    f:SetBackdropBorderColor(1, 1, 1, 0.10)

    local title = f:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", 16, -14)
    title:SetText((L["CF_WHATS_NEW"] or "What's new in ChatFloater") .. " — v" .. version)
    title:SetTextColor(0.36, 0.78, 1.0)

    local line = f:CreateTexture(nil, "ARTWORK")
    line:SetPoint("TOPLEFT", 16, -40)
    line:SetPoint("TOPRIGHT", -16, -40)
    line:SetHeight(1)
    line:SetColorTexture(0.36, 0.78, 1.0, 0.18)

    CloseButton(f)

    local y = -58
    for _, entry in ipairs(lines) do
        local fs = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        fs:SetPoint("TOPLEFT", 20, y)
        fs:SetWidth(478)
        fs:SetJustifyH("LEFT")
        fs:SetText("• " .. entry)
        fs:SetTextColor(1, 1, 1, 0.72)
        y = y - 31
    end

    local ok = SoftButton(f, L["CF_CLOSE"] or "Close", 94, 20)
    ok:SetPoint("BOTTOMRIGHT", -16, 14)
    ok:SetScript("OnClick", function() f:Hide() end)
end

EventUtil.ContinueOnPlayerLogin(function()
    C_Timer.After(1, Show)
end)
