---@class LibAT
local LibAT = LibAT

-- Initialize ProfileManager namespace
---@class LibAT.ProfileManager
LibAT.ProfileManager = LibAT:NewModule('Handler.ProfileManager')
local ProfileManager = LibAT.ProfileManager

-- This file contains all UI-related code for the ProfileManager system
-- Handles window creation, navigation tree, and mode switching

-- Import shared state (will be set by ProfileManager.lua)
local ProfileManagerState

---Initialize the UI module with shared state from ProfileManager
---@param state table The shared ProfileManager state
function ProfileManager.InitUI(state)
	ProfileManagerState = state
end

----------------------------------------------------------------------------------------------------
-- Navigation Tree Functions
----------------------------------------------------------------------------------------------------

---Rebuild the entire navigation tree with current registered addons
function ProfileManager.BuildNavigationTree()
	if not ProfileManagerState.window or not ProfileManagerState.window.NavTree then
		return
	end

	-- Build addon categories
	local addonCategories = ProfileManagerState.BuildAddonCategories()

	-- Combine with settings category
	local allCategories = addonCategories

	-- Add settings at the end
	allCategories['Settings'] = {
		name = 'Settings',
		key = 'Settings',
		expanded = false,
		subCategories = {
			['Options'] = {
				name = 'Options',
				key = 'Settings.Options',
				onSelect = function()
					LibAT:Print('Profile options coming in Phase 2')
				end,
			},
			['Namespaces'] = {
				name = 'Namespace Filter',
				key = 'Settings.Namespaces',
				onSelect = function()
					LibAT:Print('Namespace filtering coming in Phase 2')
				end,
			},
		},
		sortedKeys = { 'Options', 'Namespaces' },
	}

	-- Update navigation tree
	ProfileManagerState.window.NavTree.config.categories = allCategories
	LibAT.UI.BuildNavigationTree(ProfileManagerState.window.NavTree)
end

----------------------------------------------------------------------------------------------------
-- Window Management Functions
----------------------------------------------------------------------------------------------------

function ProfileManager.UpdateWindowForMode()
	if not ProfileManagerState.window then
		return
	end

	-- Clear text box (EditBox may not exist if window hasn't been fully created)
	if ProfileManagerState.window.EditBox then
		ProfileManagerState.window.EditBox:SetText('')
	end

	-- Get active addon info for display
	local addonInfo = ''
	local sectionName = ''
	if ProfileManagerState.window.activeAddonId and ProfileManagerState.registeredAddons[ProfileManagerState.window.activeAddonId] then
		local addon = ProfileManagerState.registeredAddons[ProfileManagerState.window.activeAddonId]
		local activeNS = ProfileManagerState.window.activeNamespace
		addonInfo = ' - ' .. addon.displayName
		if activeNS == '__COREDB__' then
			addonInfo = addonInfo .. ' (Core DB)'
			sectionName = addon.displayName .. ' Core DB'
		elseif activeNS then
			addonInfo = addonInfo .. ' (' .. activeNS .. ')'
			sectionName = addon.displayName .. ' ' .. activeNS
		else
			addonInfo = addonInfo .. ' (All)'
			sectionName = addon.displayName
		end
	end

	-- Update mode display
	if ProfileManagerState.window.mode == 'export' then
		ProfileManagerState.window.ModeLabel:SetText('|cff00ff00Export Mode|r' .. addonInfo)

		-- In export mode: show action button, hide text area initially
		if ProfileManagerState.window.activeAddonId then
			ProfileManagerState.window.Description:SetText('')
			ProfileManagerState.window.Description:Hide()

			if ProfileManagerState.window.ExportActionButton then
				ProfileManagerState.window.ExportActionButton:SetText('Export ' .. sectionName)
				ProfileManagerState.window.ExportActionButton:Show()
			end

			-- Hide the text panel until export is generated
			if ProfileManagerState.window.TextPanel then
				ProfileManagerState.window.TextPanel:Hide()
			end
		else
			ProfileManagerState.window.Description:SetText('Select a section from the left panel to export.')
			ProfileManagerState.window.Description:Show()
			if ProfileManagerState.window.ExportActionButton then
				ProfileManagerState.window.ExportActionButton:Hide()
			end
		end

		-- Hide bottom action bar import/export buttons (action is on the centered button now)
		ProfileManagerState.window.ExportButton:Hide()
		ProfileManagerState.window.ImportButton:Hide()
	else
		ProfileManagerState.window.ModeLabel:SetText('|cff00aaffImport Mode|r' .. addonInfo)
		ProfileManagerState.window.Description:SetText('Paste profile data below, then click Import to apply changes.')
		ProfileManagerState.window.Description:Show()

		-- In import mode: hide export action button, show text area
		if ProfileManagerState.window.ExportActionButton then
			ProfileManagerState.window.ExportActionButton:Hide()
		end
		if ProfileManagerState.window.TextPanel then
			ProfileManagerState.window.TextPanel:Show()
		end

		ProfileManagerState.window.ExportButton:Hide()
		ProfileManagerState.window.ImportButton:Show()
	end

	-- Update navigation tree to highlight current selection
	if ProfileManagerState.window.NavTree and ProfileManagerState.window.activeAddonId then
		local navKey = 'Addons.' .. ProfileManagerState.window.activeAddonId .. '.' .. (ProfileManagerState.window.activeNamespace or 'ALL')
		ProfileManagerState.window.NavTree.config.activeKey = navKey
		LibAT.UI.BuildNavigationTree(ProfileManagerState.window.NavTree)
	end

	ProfileManagerState.window:Show()
end

function ProfileManager.CreateWindow()
	-- Create base window using LibAT.UI
	ProfileManagerState.window = LibAT.UI.CreateWindow({
		name = 'LibAT_ProfileWindow',
		title = '|cffffffffLib|cffe21f1fAT|r Profile Manager',
		width = 800,
		height = 538,
		portrait = 'Interface\\AddOns\\Libs-AddonTools\\Logo-Icon',
	})

	ProfileManagerState.window.mode = 'import'
	ProfileManagerState.window.activeAddonId = nil -- Currently selected addon ID
	ProfileManagerState.window.activeNamespace = nil -- Currently selected namespace (nil = all)

	-- Create control frame (top bar)
	ProfileManagerState.window.ControlFrame = LibAT.UI.CreateControlFrame(ProfileManagerState.window)

	-- Add mode label (shows current mode)
	ProfileManagerState.window.ModeLabel = LibAT.UI.CreateHeader(ProfileManagerState.window.ControlFrame, 'Import Mode')
	ProfileManagerState.window.ModeLabel:SetPoint('LEFT', ProfileManagerState.window.ControlFrame, 'LEFT', 10, 0)

	-- Add switch mode button
	ProfileManagerState.window.SwitchModeButton = LibAT.UI.CreateButton(ProfileManagerState.window.ControlFrame, 100, 22, 'Switch Mode')
	ProfileManagerState.window.SwitchModeButton:SetPoint('RIGHT', ProfileManagerState.window.ControlFrame, 'RIGHT', -10, 0)
	ProfileManagerState.window.SwitchModeButton:SetScript('OnClick', function()
		ProfileManagerState.window.mode = ProfileManagerState.window.mode == 'import' and 'export' or 'import'
		ProfileManager.UpdateWindowForMode()
	end)

	-- Create main content area
	ProfileManagerState.window.MainContent = LibAT.UI.CreateContentFrame(ProfileManagerState.window, ProfileManagerState.window.ControlFrame)

	-- Create left panel for navigation
	ProfileManagerState.window.LeftPanel = LibAT.UI.CreateLeftPanel(ProfileManagerState.window.MainContent)

	-- Initialize navigation tree with registered addons
	ProfileManagerState.window.NavTree = LibAT.UI.CreateNavigationTree({
		parent = ProfileManagerState.window.LeftPanel,
		categories = {},
		activeKey = nil,
	})

	-- Build initial navigation tree
	ProfileManager.BuildNavigationTree()

	-- Create right panel for content
	ProfileManagerState.window.RightPanel = LibAT.UI.CreateRightPanel(ProfileManagerState.window.MainContent, ProfileManagerState.window.LeftPanel)

	-- Add description header
	ProfileManagerState.window.Description = LibAT.UI.CreateLabel(ProfileManagerState.window.RightPanel, '')
	ProfileManagerState.window.Description:SetPoint('TOP', ProfileManagerState.window.RightPanel, 'TOP', 0, -10)
	ProfileManagerState.window.Description:SetPoint('LEFT', ProfileManagerState.window.RightPanel, 'LEFT', 20, 0)
	ProfileManagerState.window.Description:SetPoint('RIGHT', ProfileManagerState.window.RightPanel, 'RIGHT', -20, 0)
	ProfileManagerState.window.Description:SetJustifyH('CENTER')
	ProfileManagerState.window.Description:SetWordWrap(true)

	-- Create export action button (centered, shown in export mode)
	ProfileManagerState.window.ExportActionButton = LibAT.UI.CreateButton(ProfileManagerState.window.RightPanel, 250, 40, 'Export')
	ProfileManagerState.window.ExportActionButton:SetPoint('CENTER', ProfileManagerState.window.RightPanel, 'CENTER', 0, 0)
	ProfileManagerState.window.ExportActionButton:SetScript('OnClick', function()
		ProfileManager:DoExport()
	end)
	ProfileManagerState.window.ExportActionButton:Hide()

	-- Create scrollable text display for profile data
	ProfileManagerState.window.TextPanel, ProfileManagerState.window.EditBox = LibAT.UI.CreateScrollableTextDisplay(ProfileManagerState.window.RightPanel)
	ProfileManagerState.window.TextPanel:SetPoint('TOPLEFT', ProfileManagerState.window.Description, 'BOTTOMLEFT', 6, -10)
	ProfileManagerState.window.TextPanel:SetPoint('BOTTOMRIGHT', ProfileManagerState.window.RightPanel, 'BOTTOMRIGHT', -6, 50)
	ProfileManagerState.window.EditBox:SetWidth(ProfileManagerState.window.TextPanel:GetWidth() - 20)

	-- Create action buttons
	local actionButtons = LibAT.UI.CreateActionButtons(ProfileManagerState.window, {
		{
			text = 'Clear',
			width = 70,
			onClick = function()
				ProfileManagerState.window.EditBox:SetText('')
			end,
		},
		{
			text = 'Close',
			width = 70,
			onClick = function()
				ProfileManagerState.window:Hide()
			end,
		},
	})

	-- Import button (shown in import mode)
	ProfileManagerState.window.ImportButton = LibAT.UI.CreateButton(ProfileManagerState.window, 100, 22, 'Import')
	ProfileManagerState.window.ImportButton:SetPoint('RIGHT', actionButtons[1], 'LEFT', -5, 0)
	ProfileManagerState.window.ImportButton:SetScript('OnClick', function()
		ProfileManager:DoImport()
	end)

	-- Export button (shown in export mode)
	ProfileManagerState.window.ExportButton = LibAT.UI.CreateButton(ProfileManagerState.window, 100, 22, 'Export')
	ProfileManagerState.window.ExportButton:SetPoint('RIGHT', actionButtons[1], 'LEFT', -5, 0)
	ProfileManagerState.window.ExportButton:SetScript('OnClick', function()
		ProfileManager:DoExport()
	end)

	-- Hide window initially
	ProfileManagerState.window:Hide()
end
