local addonName, NS = ...

-- =========================================================================
-- Blacklist Module (Fixed: Ghost Blacklist Logic + Timer System)
-- =========================================================================

NS.Blacklist = NS.Blacklist or {}
local M = NS.Blacklist

-- =========================================================================
-- Local Helpers
-- =========================================================================

local function CreateBg(f, r, g, b, a)
    local t = f:CreateTexture(nil, "BACKGROUND")
    t:SetAllPoints()
    t:SetColorTexture(r, g, b, a)
    f.bg = t
end

local function CreateBorder(f, r, g, b, a)
    if f.border then f.border:Hide() end

    local border = f:CreateTexture(nil, "BORDER")
    border:SetPoint("TOPLEFT", -1, 1)
    border:SetPoint("BOTTOMRIGHT", 1, -1)
    border:SetColorTexture(r, g, b, a)
    f.border = border
end

local function SkinButton(btn)
    if not btn then return end

    btn:SetNormalTexture("")
    btn:SetHighlightTexture("")
    btn:SetPushedTexture("")
    btn:SetDisabledTexture("")

    if btn.Left then btn.Left:SetAlpha(0) end
    if btn.Middle then btn.Middle:SetAlpha(0) end
    if btn.Right then btn.Right:SetAlpha(0) end
    if btn.Top then btn.Top:SetAlpha(0) end
    if btn.Bottom then btn.Bottom:SetAlpha(0) end

    CreateBg(btn, 0.2, 0.2, 0.2, 1)
    CreateBorder(btn, 0, 0, 0, 1)

    btn:HookScript("OnEnter", function(s)
        if s.bg then s.bg:SetColorTexture(0.3, 0.3, 0.3, 1) end
    end)
    btn:HookScript("OnLeave", function(s)
        if s.bg then s.bg:SetColorTexture(0.2, 0.2, 0.2, 1) end
    end)
    btn:HookScript("OnMouseDown", function(s)
        if s.bg then s.bg:SetColorTexture(0.1, 0.1, 0.1, 1) end
    end)
    btn:HookScript("OnMouseUp", function(s)
        if s.bg then s.bg:SetColorTexture(0.3, 0.3, 0.3, 1) end
    end)

    local fs = btn:GetFontString()
    if fs then fs:SetTextColor(1, 1, 1) end
end

local function GetTableSize(t)
    local count = 0
    if t then
        for _ in pairs(t) do count = count + 1 end
    end
    return count
end

-- =========================================================================
-- Attach API
-- =========================================================================

function M.Attach(GR)
    if not GR or GR.__BlacklistAttached then return end
    GR.__BlacklistAttached = true

    -- Initialize DB Default for Duration if missing (Default: 1 Week)
    if not GR.db.blacklistDuration then GR.db.blacklistDuration = 604800 end

    -- =========================================================================
    -- View 2: Blacklist UI
    -- =========================================================================

    function GR:BuildBlacklistView()
        local GR = self -- capture for closures

        local parent = CreateFrame("Frame", "GRS_BlacklistView", self.ViewContainer)
        parent:SetPoint("TOPLEFT", 0, -30)
        parent:SetPoint("BOTTOMRIGHT", 0, 0)
        parent:Hide()
        self.views[2] = parent

        -- Split Layout: Left Controls (300px), Right List (Rest)
        local controls = CreateFrame("Frame", nil, parent)
        controls:SetWidth(300)
        controls:SetPoint("TOPLEFT")
        controls:SetPoint("BOTTOMLEFT")
        CreateBg(controls, 0.1, 0.1, 0.1, 0.5)
        CreateBorder(controls, 0.2, 0.2, 0.2, 1)

        local listFrame = CreateFrame("Frame", nil, parent)
        listFrame:SetPoint("TOPLEFT", controls, "TOPRIGHT")
        listFrame:SetPoint("BOTTOMRIGHT")
        CreateBg(listFrame, 0, 0, 0, 0.6)

        -- [[ CONTROLS ]]
        local y = -20
        local function AddLbl(txt)
            local t = controls:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            t:SetPoint("TOPLEFT", 15, y)
            t:SetText(txt)
            y = y - 25
        end

        AddLbl("MANAGE BLACKLIST")

        local btnReset = CreateFrame("Button", nil, controls, "UIPanelButtonTemplate")
        btnReset:SetSize(140, 24)
        btnReset:SetPoint("TOPLEFT", 15, y)
        btnReset:SetText("Reset Entire List")
        SkinButton(btnReset)
        btnReset:SetScript("OnClick", function()
            GR:ResetBlacklist()
        end)

        local btnDelSel = CreateFrame("Button", nil, controls, "UIPanelButtonTemplate")
        btnDelSel:SetSize(120, 24)
        btnDelSel:SetPoint("LEFT", btnReset, "RIGHT", 5, 0)
        btnDelSel:SetText("Reset Selection")
        SkinButton(btnDelSel)
        btnDelSel:SetScript("OnClick", function()
            GR:RemoveSelectedFromBlacklist()
        end)

        y = y - 40
        AddLbl("MANUAL ADD")

        local inputAdd = CreateFrame("EditBox", nil, controls, "InputBoxTemplate")
        inputAdd:SetSize(180, 24)
        inputAdd:SetPoint("TOPLEFT", 15, y)
        inputAdd:SetAutoFocus(false)
        inputAdd:SetScript("OnEscapePressed", function(s) s:ClearFocus() end)

        local btnAdd = CreateFrame("Button", nil, controls, "UIPanelButtonTemplate")
        btnAdd:SetSize(80, 24)
        btnAdd:SetPoint("LEFT", inputAdd, "RIGHT", 5, 0)
        btnAdd:SetText("Add")
        SkinButton(btnAdd)
        btnAdd:SetScript("OnClick", function()
            local n = inputAdd:GetText()
            if n and n ~= "" then
                GR:AddToBlacklist(n)
                inputAdd:SetText("")
                print("Added " .. n .. " to Blacklist.")
            end
        end)

        -- [[ EXPIRY DROPDOWN ]]
        y = y - 40
        AddLbl("Remember for:")
        
        local dropF = CreateFrame("Frame", "GRS_ExpiryDropdown", controls, "UIDropDownMenuTemplate")
        dropF:SetPoint("TOPLEFT", -2, y + 5) -- Adjust specifically for Dropdown Template offset
        
        local function OnSelect(self, arg1)
            GR.db.blacklistDuration = arg1
            UIDropDownMenu_SetSelectedValue(dropF, arg1)
            local text = UIDropDownMenu_GetText(dropF)
            print("GRS: Players will be remembered for " .. (text or "custom duration"))
        end

        UIDropDownMenu_Initialize(dropF, function(self, level, menuList)
            local info = UIDropDownMenu_CreateInfo()
            info.func = OnSelect
            
            local options = {
                { text = "1 Day", value = 86400 },
                { text = "1 Week", value = 604800 },
                { text = "2 Weeks", value = 1209600 },
                { text = "1 Month", value = 2592000 },
                { text = "2 Months", value = 5184000 }
            }

            for _, opt in ipairs(options) do
                info.text = opt.text
                info.arg1 = opt.value
                info.checked = (GR.db.blacklistDuration == opt.value)
                UIDropDownMenu_AddButton(info)
            end
        end)
        
        -- Set Initial Text
        UIDropDownMenu_SetSelectedValue(dropF, GR.db.blacklistDuration)
        local val = GR.db.blacklistDuration
        if val == 86400 then UIDropDownMenu_SetText(dropF, "1 Day")
        elseif val == 604800 then UIDropDownMenu_SetText(dropF, "1 Week")
        elseif val == 1209600 then UIDropDownMenu_SetText(dropF, "2 Weeks")
        elseif val == 2592000 then UIDropDownMenu_SetText(dropF, "1 Month")
        elseif val == 5184000 then UIDropDownMenu_SetText(dropF, "2 Months")
        else UIDropDownMenu_SetText(dropF, "1 Week") end

        y = y - 35
        AddLbl("GUILD SYNC")

        local btnSync = CreateFrame("Button", nil, controls, "UIPanelButtonTemplate")
        btnSync:SetSize(265, 30)
        btnSync:SetPoint("TOPLEFT", 15, y)
        btnSync:SetText("Sync with Guild")
        SkinButton(btnSync)
        btnSync:SetScript("OnClick", function()
            GR:SyncBlacklist()
        end)

        y = y - 35
        local syncInfo = controls:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        syncInfo:SetPoint("TOPLEFT", 15, y)
        syncInfo:SetWidth(270)
        syncInfo:SetJustifyH("LEFT")
        syncInfo:SetText("Sends your blacklist to online guild members using this addon and merges theirs.")

        -- [[ LIST HEADER ]]
        local headF = CreateFrame("Frame", nil, listFrame)
        headF:SetSize(20, 20)
        headF:SetPoint("TOPLEFT", 0, -5)
        headF:SetPoint("TOPRIGHT", 0, -5)

        -- REMOVED: Score, Mythic
        local headers = {
            {text="Name", width=140},
            {text="Race", width=100},
            {text="Class", width=100},
            {text="Heroic", width=80}
        }

        local lh = nil
        for i, h in ipairs(headers) do
            local b = CreateFrame("Button", nil, headF)
            b:SetSize(h.width, 20)
            if i == 1 then
                b:SetPoint("LEFT", 30, 0)
            else
                b:SetPoint("LEFT", lh, "RIGHT", 0, 0)
            end
            local t = b:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            t:SetPoint("LEFT")
            t:SetText(h.text)
            lh = b
        end

        -- [[ SCROLL LIST ]]
        self.blScroll = CreateFrame("ScrollFrame", "GRS_BLScroll", listFrame, "FauxScrollFrameTemplate")
        self.blScroll:SetPoint("TOPLEFT", headF, "BOTTOMLEFT", 0, -5)
        self.blScroll:SetPoint("BOTTOMRIGHT", -25, 5)
        CreateBg(self.blScroll, 0, 0, 0, 0.3)

        self.blScroll:SetScript("OnVerticalScroll", function(s, offset)
            FauxScrollFrame_OnVerticalScroll(s, offset, 20, function()
                self:UpdateBlacklistScroll()
            end)
        end)

        self.blRows = {}
        -- UPDATED: Loop 34 times for ~680px height (34 * 20 = 680)
        for i = 1, 34 do
            local r = CreateFrame("Button", nil, listFrame)
            r:SetHeight(20)
            r:SetPoint("LEFT", 5, 0)
            r:SetPoint("RIGHT", -5, 0)

            if i == 1 then
                r:SetPoint("TOP", self.blScroll, "TOP")
            else
                r:SetPoint("TOP", self.blRows[i-1], "BOTTOM")
            end

            r.c = CreateFrame("CheckButton", nil, r, "UICheckButtonTemplate")
            r.c:SetSize(20, 20)
            r.c:SetPoint("LEFT", 0, 0)
            r.c:SetScript("OnClick", function(b)
                if r.data then r.data.selected = b:GetChecked() end
            end)

            r.n = r:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
            r.n:SetPoint("LEFT", 25, 0); r.n:SetWidth(140); r.n:SetJustifyH("LEFT")

            r.ra = r:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
            r.ra:SetPoint("LEFT", r.n, "RIGHT"); r.ra:SetWidth(100); r.ra:SetJustifyH("LEFT")

            r.cl = r:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
            r.cl:SetPoint("LEFT", r.ra, "RIGHT"); r.cl:SetWidth(100); r.cl:SetJustifyH("LEFT")

            r.h = r:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
            r.h:SetPoint("LEFT", r.cl, "RIGHT"); r.h:SetWidth(80); r.h:SetJustifyH("LEFT")
            -- Removed r.s (Score) and r.m (Mythic)

            self.blRows[i] = r
        end

        parent:SetScript("OnShow", function()
            GR:PruneExpiredBlacklist() -- Check expiry when opening view
            self:UpdateBlacklistScroll()
        end)
    end

    -- =========================================================================
    -- Blacklist Logic & Sync
    -- =========================================================================

    function GR:GetBlacklistAsArray()
        local arr = {}
        for k, v in pairs(self.db.blacklist) do
            if type(v) == "table" then
                table.insert(arr, v)
            else
                table.insert(arr, {name = k, heroic = "-", fullClass = "Unknown"})
            end
        end
        table.sort(arr, function(a, b) return a.name < b.name end)
        return arr
    end

    function GR:UpdateBlacklistScroll()
        local list = self:GetBlacklistAsArray()
        local numRows = #self.blRows

        FauxScrollFrame_Update(self.blScroll, #list, numRows, 20)

        local offset = FauxScrollFrame_GetOffset(self.blScroll)

        for i = 1, numRows do
            local r = self.blRows[i]
            local idx = offset + i
            if idx <= #list then
                local d = list[idx]
                r.data = d
                r:Show()
                r.n:SetText(d.name)
                r.ra:SetText(d.race or "-")
                r.cl:SetText(d.fullClass or "-")
                r.h:SetText(d.heroic or "-")
                -- Removed score/mythic data population
                r.c:SetChecked(d.selected or false)
            else
                r:Hide()
            end
        end
    end

    function GR:PruneExpiredBlacklist()
        local now = time()
        local removedCount = 0
        
        for name, data in pairs(self.db.blacklist) do
            if data.expiryTime then
                if now >= data.expiryTime then
                    self.db.blacklist[name] = nil
                    
                    -- Also try to remove realm-less version if exists as key
                    local shortName = string.match(name, "^(.-)-")
                    if shortName and self.db.blacklist[shortName] then
                        self.db.blacklist[shortName] = nil
                    end
                    removedCount = removedCount + 1
                end
            end
        end
        
        if removedCount > 0 then
            print("GRS: Removed " .. removedCount .. " expired players from Blacklist.")
            if self.views and self.views[2] and self.views[2]:IsShown() then
                self:UpdateBlacklistScroll()
            end
        end
    end

    function GR:AddToBlacklist(name)
        if not name then return end

        if not string.find(name, "-") then
            name = name .. "-" .. GetRealmName()
        end

        local data = nil
        -- Check if we have data on this player in the current scan results
        for _, p in ipairs(self.foundPlayers) do
            local pName = p.name
            if not string.find(pName, "-") then pName = pName .. "-" .. GetRealmName() end
            
            if pName == name then 
                data = p; 
                break 
            end
        end
        
        -- Calculate Expiry
        local duration = self.db.blacklistDuration or 604800 -- Default 1 week if nil
        local expiry = time() + duration

        if data then
            self.db.blacklist[name] = {
                name = data.name, 
                race = data.race,
                fullClass = data.fullClass,
                heroic = data.heroic,
                level = data.level,
                zone = data.zone,
                expiryTime = expiry 
            }
        else
            self.db.blacklist[name] = {
                name = name,
                race = "Unknown",
                fullClass = "Unknown",
                heroic = "-",
                level = 0,
                zone = "Unknown",
                expiryTime = expiry
            }
        end

        if self.RemovePlayer then self:RemovePlayer(name) end
        
        local shortName = string.match(name, "^(.-)-")
        if shortName and self.RemovePlayer then self:RemovePlayer(shortName) end

        if self.AddToRecent then self:AddToRecent(name, "Blacklisted") end
        
        if self.views and self.views[2] and self.views[2].IsShown and self.views[2]:IsShown() then
            self:UpdateBlacklistScroll()
        end
    end

    function GR:RemoveSelectedFromBlacklist()
        local list = self:GetBlacklistAsArray()
        for _, d in ipairs(list) do
            if d.selected then
                self.db.blacklist[d.name] = nil
                if not string.find(d.name, "-") then
                    self.db.blacklist[d.name .. "-" .. GetRealmName()] = nil
                end
            end
        end
        self:UpdateBlacklistScroll()
    end

    function GR:SyncBlacklist()
        if not IsInGuild() then
            print("GRS: You must be in a guild to sync.")
            return
        end

        print("GRS: Sending blacklist to guild members...")

        local str = ""
        local count = 0
        for name, d in pairs(self.db.blacklist) do
            -- Removed Rio/Mythic from sync string to save bytes
            local entry = string.format("%s:%s", name, d.heroic or "-")

            if #str + #entry + 1 > 240 then
                C_ChatInfo.SendAddonMessage("GRS_SYNC", str, "GUILD")
                str = entry
            else
                if str == "" then str = entry else str = str .. "|" .. entry end
            end
            count = count + 1
        end

        if str ~= "" then
            C_ChatInfo.SendAddonMessage("GRS_SYNC", str, "GUILD")
        end

        print("GRS: Sent " .. count .. " entries.")
    end

    function GR:HandleComm(prefix, text, channel, sender)
        if prefix ~= "GRS_SYNC" then return end
        if sender == UnitName("player") then return end

        local entries = { strsplit("|", text) }
        local new = 0

        for _, e in ipairs(entries) do
            local n, h = strsplit(":", e) -- Adapted for simpler sync string
            if n and not self.db.blacklist[n] then
                if not string.find(n, "-") then n = n .. "-" .. GetRealmName() end
                
                self.db.blacklist[n] = {
                    name = n,
                    heroic = h,
                    race = "Unknown",
                    fullClass = "Synced",
                    level = 0,
                    expiryTime = time() + (self.db.blacklistDuration or 604800) -- Apply current user default to synced entries
                }
                new = new + 1
            end
        end

        if new > 0 and self.views and self.views[2] and self.views[2].IsShown and self.views[2]:IsShown() then
            self:UpdateBlacklistScroll()
            print("GRS: Synced " .. new .. " new blacklist entries from " .. sender)
        end
    end

    function GR:ManualBlacklist()
        local toBlacklist = {}
        for _, p in ipairs(self.foundPlayers) do
            if p.selected then table.insert(toBlacklist, p.name) end
        end

        if #toBlacklist == 0 then
            print("No players selected to blacklist.")
            return
        end

        for _, name in ipairs(toBlacklist) do
            self:AddToBlacklist(name)
        end
        print(string.format("Added %d players to blacklist.", #toBlacklist))
    end

    function GR:ResetBlacklist()
        self.db.blacklist = {}
        self.db.recentHistory = {}
        self.foundPlayers = {}
        if self.UpdateRecentList then self:UpdateRecentList() end
        if self.UpdateScrollFrame then self:UpdateScrollFrame() end
        if self.views and self.views[2] and self.views[2].IsShown and self.views[2]:IsShown() then
            self:UpdateBlacklistScroll()
        end
        print("GRS: Blacklist wiped.")
    end

    local blSize = GR.db and GR.db.blacklist and GetTableSize(GR.db.blacklist) or 0
end