local addonName, NS = ...
local GR = _G["GRS_MainFrame"] or NS.GR

if not GR then return end

-- =========================================================================
-- COMPACT MODE: DATA & STATE
-- =========================================================================

GR.CompactData = {
    isCompact = false,
    foundCount = 0,
    sentCount = 0,
    savedGeometry = nil,
    originalStartSearch = nil,
    originalParent = nil,
    originalPoints = {},
    headerFS = nil -- Store reference to the header font string
}

-- Counter Container
GR.CompactCounterFrame = CreateFrame("Frame", "GRS_CompactCounter", GR)
GR.CompactCounterFrame:SetSize(200, 20)
GR.CompactCounterFrame:Hide()
GR.CompactCounterLabel = GR.CompactCounterFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
GR.CompactCounterLabel:SetPoint("CENTER")
GR.CompactCounterLabel:SetText("sending messages 0 out of 0")

-- Create Compact Specific Buttons
local function CreateCompactBtn(name, text, func)
    local btn = CreateFrame("Button", name, GR, "UIPanelButtonTemplate")
    btn:SetSize(140, 30)
    btn:SetText(text)
    btn:SetScript("OnClick", func)
    btn:Hide()
    if NS.SkinButton then NS.SkinButton(btn) end
    return btn
end

-- [[ NEW COMPACT SEARCH BUTTON ]]
-- Independent button for Compact Mode only. Replicates StartSearch functionality.
GR.CompactSearchBtn = CreateCompactBtn("GRS_CompactSearchBtn", "Search Players", function()
    GR:StartSearch()
end)

GR.CompactBatchBtn = CreateCompactBtn("GRS_CompactBatchBtn", "Send Batch", function()
    -- [[ AUTOMATIC SELECTION LOGIC ]]
    -- Select all found players immediately
    for _, p in ipairs(GR.foundPlayers) do
        p.selected = true
    end
    -- Trigger original send sequence
    GR:SendMessageSequence()
end)

-- =========================================================================
-- LOGIC OVERRIDES
-- =========================================================================

-- [[ 1. OVERRIDE RESIZE BOUNDS ]]
-- We replace the main function to respect the Compact Mode state dynamically.
local OriginalUpdateBounds = GR.UpdateResizeBounds
GR.UpdateResizeBounds = function(self)
    if self.db.compactMode then
        -- In Compact Mode, allow minimum width of 420 (as requested)
        -- Max height increased to 1000 for vertical flexibility
        self:SetResizeBounds(420, 665, 420, 1000)
    else
        -- Use original logic for standard mode
        OriginalUpdateBounds(self)
    end
end

-- Override StartSearch to handle "Bypass Filter"
GR.OriginalStartSearch = GR.StartSearch
GR.StartSearch = function(self)
    if self.db.compactMode and self.db.bypassFilter then
        -- [[ BYPASS LOGIC ]]
        local now = GetTime()
        if (now - self.lastScanTime) < 5 then return end
        
        self:UpdateRaiderIOStatus()
        self.lastScanTime = now
        self.isScanning = true
        
        -- Cooldown UI Logic (Handled in Engine for visual updates, but triggered here)
        -- We manually trigger the visual lock here for the specific bypass bypass logic
        local buttons = {self.SearchBtn, self.CompactSearchBtn}
        for _, btn in pairs(buttons) do
            if btn then
                btn:Disable()
                local cd = 5
                btn:SetText("|cffff0000CD ("..cd..")|r")
                C_Timer.NewTicker(1, function(timer)
                    cd = cd - 1
                    if cd > 0 then
                        if btn then btn:SetText("|cffff0000CD ("..cd..")|r") end
                    else
                        timer:Cancel()
                        if btn then 
                            btn:Enable()
                            btn:SetText("Search Players")
                        end
                    end
                end, 5)
            end
        end
        
        C_Timer.After(60, function() if GR.isScanning then GR.isScanning = false end end)
        
        -- SIMPLE QUERY: No class/race loop
        -- Just min-max level
        local query = string.format("%d-%d", self.db.minLevel, self.db.maxLevel)
        
        if self.StatusText then self.StatusText:SetText("Bypass Scan: " .. query .. "...") end
        
        -- Force friend frame prep
        if FriendsFrame then FriendsFrame:UnregisterEvent("WHO_LIST_UPDATE") end
        C_FriendList.SetWhoToUi(1)
        C_FriendList.SendWho(query)
    else
        -- Use Original
        GR.OriginalStartSearch(self)
    end
end

-- Hook ProcessWhoResults to increment 'Found' (Y)
local OriginalProcessWho = GR.ProcessWhoResults
GR.ProcessWhoResults = function(self)
    local oldCount = #self.foundPlayers
    OriginalProcessWho(self)
    local newCount = #self.foundPlayers
    
    if self.db.compactMode then
        local diff = newCount - oldCount
        if diff > 0 then
            GR.CompactData.foundCount = GR.CompactData.foundCount + diff
            GR:UpdateCompactCounter()
        end
    end
end

-- Hook SendMessageSequence to increment 'Sent' (X)
local OriginalSendMessage = GR.SendMessageSequence
GR.SendMessageSequence = function(self)
    -- Global hook of SendChatMessage to count sent whispers during batch
    local oldSend = SendChatMessage
    SendChatMessage = function(msg, chatType, lang, target)
        oldSend(msg, chatType, lang, target)
        if GR.db.compactMode and chatType == "WHISPER" then
             GR.CompactData.sentCount = GR.CompactData.sentCount + 1
             GR:UpdateCompactCounter()
        end
    end
    
    OriginalSendMessage(self)
    -- Hook remains active to catch delayed sends
end


function GR:UpdateCompactCounter()
    local x = self.CompactData.sentCount
    local y = self.CompactData.foundCount
    
    -- X = Green, Y = Gold. If X==Y, X becomes Gold.
    local xColor = "|cff00ff00" -- Green
    if x >= y and y > 0 then xColor = "|cffffd100" end -- Gold
    
    local txt = string.format("sending messages %s%d|r out of |cffffd100%d|r", xColor, x, y)
    self.CompactCounterLabel:SetText(txt)
end


-- =========================================================================
-- MAIN TOGGLE FUNCTION
-- =========================================================================

function GR:ToggleCompactMode(enable)
    if enable then
        GR.CompactData.isCompact = true
        
        -- 1. SAVE STATE (Geometry ignored on restore, but good practice to keep structure)
        if not GR.CompactData.savedGeometry then
             GR.CompactData.savedGeometry = { w = GR:GetWidth(), h = GR:GetHeight() }
        end
        
        -- Resize to Compact (Width 420, Height 665) -- UPDATED to 420
        GR:SetSize(420, 665)
        GR:UpdateResizeBounds() -- Calls our overridden function
        
        -- 2. HIDE TABS (Except 1 & 7)
        for i, tab in ipairs(GR.Tabs) do
            if i ~= 1 and i ~= 7 then tab:Hide() end
        end
        
        -- 3. HIDE PANES (Keep Left/Home visible)
        if GR.views[1] then
             local scrollChild = _G["GRS_ScrollChild"]
             if scrollChild then
                 local right = _G["GRS_RightPane"]
                 local center = _G["GRS_CenterPane"]
                 if right then right:Hide() end
                 if center then center:Hide() end
                 
                 local outer = _G["GRS_OuterScroll"]
                 if outer then outer:SetHorizontalScroll(0) end
             end
        end

        -- 4. VISUAL ADJUSTMENTS (Header & Status)
        
        -- Correctly find and change the Top Blue Header
        local headerFrame = _G["GRS_AxiomHeader"]
        if headerFrame then
            local regions = {headerFrame:GetRegions()}
            for _, r in ipairs(regions) do
                if r:IsObjectType("FontString") then
                    local txt = r:GetText()
                    if txt and string.find(txt, "Automated Xenial") then
                        GR.CompactData.headerFS = r -- Save reference
                        r:SetText("A.X.I.O.M.")
                        break
                    end
                end
            end
        end
        
        -- Hide Status Texts ("Greetings...", "Recruiter mode...")
        if GR.ioStatus then GR.ioStatus:Hide() end
        if GR.officerStatus then GR.officerStatus:Hide() end
        
        -- 5. BUTTON & UI RELOCATION
        -- In Compact Mode:
        -- - HIDE the Original Search Button
        -- - SHOW the New Compact Search Button
        
        if GR.SearchBtn then GR.SearchBtn:Hide() end
        
        -- Position the COMPACT Search Button
        GR.CompactSearchBtn:SetParent(GR) 
        GR.CompactSearchBtn:SetFrameStrata("FULLSCREEN_DIALOG") 
        GR.CompactSearchBtn:ClearAllPoints()
        
        -- Find 'TEST ME' button inside the ScrollChild for positioning reference
        -- Note: We now have GR.TestBtn exposed in UI.lua, we can use that if we want,
        -- but for the "Compact Layout", we are defining new positions relative to the frame bottom.
        
        -- We position Compact Search relative to the Test Button (if visible) or generic.
        if GR.TestBtn then
             GR.CompactSearchBtn:SetPoint("TOPLEFT", GR.TestBtn, "BOTTOMLEFT", 0, -10)
        else
             GR.CompactSearchBtn:SetPoint("TOPLEFT", 15, -530)
        end
        GR.CompactSearchBtn:SetSize(165, 30) 
        
        -- Position Batch Button
        GR.CompactBatchBtn:SetParent(GR)
        GR.CompactBatchBtn:SetFrameStrata("FULLSCREEN_DIALOG") 
        GR.CompactBatchBtn:ClearAllPoints()
        GR.CompactBatchBtn:SetPoint("TOPLEFT", GR.CompactSearchBtn, "BOTTOMLEFT", 0, -10)
        GR.CompactBatchBtn:SetSize(165, 30)
        
        -- Counter
        GR.CompactCounterFrame:SetParent(GR)
        GR.CompactCounterFrame:SetFrameStrata("FULLSCREEN_DIALOG") 
        GR.CompactCounterFrame:ClearAllPoints()
        GR.CompactCounterFrame:SetPoint("TOP", GR.CompactBatchBtn, "BOTTOM", 0, -10)

        -- [[ REPARENT & MOVE WELCOME BUTTON ]]
        if GR.WelcomeBtn then
            GR.WelcomeBtn:ClearAllPoints()
            GR.WelcomeBtn:SetParent(GR)
            GR.WelcomeBtn:SetFrameStrata("FULLSCREEN_DIALOG")
            GR.WelcomeBtn:SetPoint("TOP", GR.CompactCounterFrame, "BOTTOM", 0, -10)
            -- Width is already 165 from UI.lua, matching compact buttons
        end
        
        -- Hide Standard Status Text
        if GR.StatusText then GR.StatusText:Hide() end
        
        -- [[ VISIBILITY CHECK ]]
        -- Only show compact buttons if we are currently on the Home Tab (1).
        local isHome = (GR.activeTab == 1)
        GR.CompactSearchBtn:SetShown(isHome)
        GR.CompactBatchBtn:SetShown(isHome)
        GR.CompactCounterFrame:SetShown(isHome)
        if GR.WelcomeBtn then GR.WelcomeBtn:SetShown(isHome) end
        
    else
        GR.CompactData.isCompact = false
        
        -- 1. RESTORE WINDOW (Force 1280x750)
        GR:SetSize(1280, 750)
        GR:UpdateResizeBounds() -- Calls our overridden function which now sees compact=false
        
        -- [[ CRITICAL FIX: FORCE LAYOUT UPDATE ]]
        if GR.views[1] and GR.views[1]:GetScript("OnSizeChanged") then
             GR.views[1]:GetScript("OnSizeChanged")(GR.views[1], 1280, 750)
        end
        
        -- 2. RESTORE TITLES & STATUS
        if GR.CompactData.headerFS then
            GR.CompactData.headerFS:SetText("A.X.I.O.M. | Automated Xenial Index & Operations Matrix")
        end
        
        if GR.ioStatus then GR.ioStatus:Show() end
        if GR.officerStatus then GR.officerStatus:Show() end
        
        -- [[ RESTORE TABS ]]
        for _, tab in ipairs(GR.Tabs) do
            tab:Show()
        end
        GR:UpdateOfficerStatus() -- Refresh tab visibility & layout
        
        -- 3. RESTORE PANES
        local right = _G["GRS_RightPane"]
        local center = _G["GRS_CenterPane"]
        if right then right:Show() end
        if center then center:Show() end
        
        -- 4. RESTORE BUTTONS
        -- Hide Compact Buttons
        GR.CompactSearchBtn:Hide()
        GR.CompactBatchBtn:Hide()
        GR.CompactCounterFrame:Hide()

        -- Restore Welcome Button Position
        if GR.WelcomeBtn and GR.TestBtn then
             GR.WelcomeBtn:ClearAllPoints()
             -- Note: In UI.lua, the parent was `parent` (which is `GRS_HomeView` i.e. GR.views[1])
             GR.WelcomeBtn:SetParent(GR.views[1]) 
             GR.WelcomeBtn:SetFrameStrata("MEDIUM")
             GR.WelcomeBtn:SetPoint("TOPLEFT", GR.TestBtn, "BOTTOMLEFT", 0, -20)
             GR.WelcomeBtn:SetShown(true)
        end
        
        -- Restore Original Search Button
        if GR.SearchBtn then
             GR.SearchBtn:Show()
             GR.SearchBtn:Enable()
        end
        
        if GR.StatusText then GR.StatusText:Show() end
    end
end

-- =========================================================================
-- TAB SWITCH HOOK
-- =========================================================================

-- Secure hook to handle button visibility when switching tabs in Compact Mode
hooksecurefunc(GR, "SwitchView", function(self, id)
    if not self.db.compactMode then return end
    
    -- In Compact Mode, buttons are parented to Main Frame (GR).
    -- They do not auto-hide when changing views. We must force it.
    
    local showCompactBtns = (id == 1) -- Only show on Home Tab
    
    if self.CompactSearchBtn then self.CompactSearchBtn:SetShown(showCompactBtns) end
    if self.CompactBatchBtn then self.CompactBatchBtn:SetShown(showCompactBtns) end
    if self.CompactCounterFrame then self.CompactCounterFrame:SetShown(showCompactBtns) end
    if self.WelcomeBtn then self.WelcomeBtn:SetShown(showCompactBtns) end
end)