local _AddonName, _EchoRaidTools = ...
local _Miscellaneous = _EchoRaidTools:NewModule("Miscellaneous")


function _Miscellaneous:SetupCommunitiesPrivacy()
    --create overlay
    if _Miscellaneous.communitiesOverlay then
        _Miscellaneous.communitiesOverlay:toggleOverlay()
        return
    end
    local f = CreateFrame("Button",nil,UIParent)
    f:SetFrameStrata("HIGH")
    f.tex = f:CreateTexture(nil, "BACKGROUND")
    f.tex:SetAllPoints()
    f.tex:SetColorTexture(0.1,0.1,0.1,1)
    f.text = f:CreateFontString()
    f.text:SetFontObject("GameFontNormalMed3")
    f.text:SetText("Chat Hidden. Click to show")
    f.text:SetTextColor(1, 1, 1, 1)
    f.text:SetJustifyH("CENTER")
    f.text:SetJustifyV("MIDDLE")
    f.text:SetHeight(20)
    f.text:SetPoint("CENTER",f,"CENTER",0,0)
    f:EnableMouse(true)
    f:RegisterForClicks("AnyUp")
    if CommunitiesFrame:IsShown() then
        f:Show()
    else
        f:Hide()
    end
    f:SetScript("OnClick",function(...)
            f:Hide()
    end)
    --toggle
    function f:toggleOverlay()
        if CommunitiesFrame:GetDisplayMode() == COMMUNITIES_FRAME_DISPLAY_MODES.CHAT
        and EchoRaidToolsDB.Miscellaneous.communityCover
        and CommunitiesFrame.Chat.InsetFrame:IsVisible() then
            f:SetAllPoints(CommunitiesFrame.Chat.InsetFrame)
            f:Show()
        else
            f:Hide()
        end
    end
    function f:hideOverlay()
        f:Hide()
    end
    f:toggleOverlay() --run once
    
    --hook
    hooksecurefunc(CommunitiesFrame,"SetDisplayMode", function() f:toggleOverlay() end)
    hooksecurefunc(CommunitiesFrame,"Show", function() f:toggleOverlay() end)
    hooksecurefunc(CommunitiesFrame,"Hide",function() f:hideOverlay() end)
    hooksecurefunc(CommunitiesFrame,"OnClubSelected", function() f:toggleOverlay() end)
    _Miscellaneous.communitiesOverlay = f
end

local wmSpells = {
    [84996] = 6, --1
    [84997] = 4, --2 
    [84998] = 3, --3
    [84999] = 7, --4
    [85000] = 1, --5
    [171555] = 2, --6
    [171556] = 5, --7
    [171557] = 8, --8
}

function _Miscellaneous:ADDON_LOADED(addonName)
	if addonName ~= _AddonName then
		return
	end

    EchoRaidToolsDB.Miscellaneous = EchoRaidToolsDB.Miscellaneous or {}

    if type(EchoRaidToolsDB.Miscellaneous.communityCover) ~= "boolean" then
        EchoRaidToolsDB.Miscellaneous.communityCover = true
    end
    if type(EchoRaidToolsDB.Miscellaneous.wmCursor) ~= "boolean" then
        EchoRaidToolsDB.Miscellaneous.wmCursor = true
    end
    if type(EchoRaidToolsDB.Miscellaneous.wmCursorSize) ~= "number" then
        EchoRaidToolsDB.Miscellaneous.wmCursorSize = 30
    end

    local wmCursorFrame = CreateFrame("Frame", nil, UIParent)
    wmCursorFrame:SetFrameStrata("HIGH")
    wmCursorFrame:SetSize(EchoRaidToolsDB.Miscellaneous.wmCursorSize, EchoRaidToolsDB.Miscellaneous.wmCursorSize)
    local wmTexture = wmCursorFrame:CreateTexture(nil, "OVERLAY")
    wmTexture:SetAllPoints()
    wmCursorFrame.texture = wmTexture
    wmCursorFrame.moveWithMouse = function()
        local scale = 1 / UIParent:GetEffectiveScale()
        local x, y =  GetCursorPosition()
        wmCursorFrame:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x * scale, y * scale)
    end

    if C_AddOns.IsAddOnLoaded("Blizzard_Communities") then
        _Miscellaneous:SetupCommunitiesPrivacy()
    end
    local eventFrame = CreateFrame("Frame")
    eventFrame:RegisterEvent("ADDON_LOADED")
    eventFrame:SetScript("OnEvent", function(self, event, ...)
        if event == "ADDON_LOADED" then
            local loadedAddonName = ...
            if loadedAddonName == "Blizzard_Communities" then
                _Miscellaneous:SetupCommunitiesPrivacy()
            end
        elseif event == "UNIT_SPELLCAST_SENT" or event == "UNIT_SPELLCAST_RETICLE_CLEAR" then
            local unit = ...
            if unit == "player" and wmCursorFrame:IsShown() then
                wmCursorFrame:Hide()
                wmCursorFrame:SetScript("OnUpdate", nil)
            end
        elseif event == "UNIT_SPELLCAST_RETICLE_TARGET" then
            local unit, _, spellID = ...
            if unit == "player" and wmSpells[spellID] then
                local markIndex = wmSpells[spellID]
                local tex = [[Interface\TargetingFrame\UI-RaidTargetingIcon_]]..markIndex
                wmCursorFrame.texture:SetTexture(tex)
                wmCursorFrame:Show()

                wmCursorFrame:ClearAllPoints()
                wmCursorFrame:SetScript("OnUpdate", wmCursorFrame.moveWithMouse)
            end
        end
    end)
    
    local function updateWmCursorEnable()
        if EchoRaidToolsDB.Miscellaneous.wmCursor then
            eventFrame:RegisterEvent("UNIT_SPELLCAST_SENT")
            eventFrame:RegisterEvent("UNIT_SPELLCAST_RETICLE_CLEAR")
            eventFrame:RegisterEvent("UNIT_SPELLCAST_RETICLE_TARGET")
        else
            eventFrame:UnregisterEvent("UNIT_SPELLCAST_SENT")
            eventFrame:UnregisterEvent("UNIT_SPELLCAST_RETICLE_CLEAR")
            eventFrame:UnregisterEvent("UNIT_SPELLCAST_RETICLE_TARGET")
            wmCursorFrame:SetScript("OnUpdate", nil)
            wmCursorFrame:Hide()
        end
    end
    updateWmCursorEnable()

    function _Miscellaneous:GetNavigationButtons() end

    function _Miscellaneous:GetConfig()
        if _EchoRaidTools.panels.MiscellaneousModule then
            return _EchoRaidTools.panels.MiscellaneousModule
        end

        --main panel creation
		local window = CreateFrame("FRAME", nil, _EchoRaidTools.panels.main, "EchoRaidToolsMainConfigWindow")
		window.Header.EnableModule:Hide()
		window.Header.HeaderText:ClearAllPoints()
		window.Header.HeaderText:SetPoint("LEFT", 10, 0)
		window.Header.HeaderText:SetText("Miscellaneous")

		local panel = CreateFrame("FRAME", nil, window, "EchoRaidToolsMainConfigPanel")

		local function onShowHandler()
		end
		panel:SetScript("OnShow", onShowHandler)
		panel:SetScript("OnHide", function()
		end)
		-- Call the show handler immediately since OnShow won't fire on initial creation
		onShowHandler()

		panel:SetPoint("TOPLEFT", window.Header, "BOTTOMLEFT")
		panel:SetPoint("BOTTOMRIGHT")
		local mainContainer = panel.ScrollFrame.ScrollingPanel
		_Miscellaneous.controlParent = mainContainer
		_Miscellaneous.mainWindow = window
		_Miscellaneous.mainPanel = panel
		_Miscellaneous.mainWindow.currentPanel = panel

		panel.ScrollFrame:SetVerticalScroll(0)
		local Resize = function() end
		panel:SetScript("OnSizeChanged", Resize)

		_EchoRaidTools.panels.MiscellaneousModule = window

		local ScrollingPanel = CreateFrame("Frame", nil, mainContainer, "EchoRaidToolsMainConfigPanel")
		ScrollingPanel:SetAllPoints()
		ScrollingPanel:Show()

        local function newCheckbutton(label, description, onClick, parent)
			local checkbutton = CreateFrame("CheckButton", nil, parent, "EchoRaidToolsConfig_CheckButton")
			checkbutton:SetSize(24, 24)
			checkbutton.CheckButton:SetScript("OnClick", function(self)
				local tick = self:GetChecked()
				onClick(self, not not tick)
				if tick then
					PlaySound(856)
				else
					PlaySound(857)
				end
			end)
			checkbutton.TopText:SetText(label)
			checkbutton.TopText:Show()
			checkbutton.tooltip = description
            checkbutton.CheckButton.tooltip = description
			return checkbutton
		end

        local communityCoverLabel = mainContainer:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		communityCoverLabel:SetPoint("TOPLEFT", mainContainer, "TOPLEFT", 20, -20)
		communityCoverLabel:SetText("Community Cover")
		local communityCoverCheckbox = newCheckbutton(
			"",
			"Add a cover to the community panel to keep the chat private until you click on it",
			function(_, checked)
				EchoRaidToolsDB.Miscellaneous.communityCover = checked
                _Miscellaneous:SetupCommunitiesPrivacy()
			end,
			mainContainer
		)
		communityCoverCheckbox:SetPoint("TOPLEFT", communityCoverLabel, "BOTTOMLEFT", 0, -8)
		communityCoverCheckbox.CheckButton:SetChecked(EchoRaidToolsDB.Miscellaneous.communityCover)

        local wmCursorLabel = mainContainer:CreateFontString(nil, "ARTWORK", "GameFontNormal")
        wmCursorLabel:SetPoint("TOPLEFT", communityCoverCheckbox, "BOTTOMLEFT", 0, -20)
        wmCursorLabel:SetText("World Marker Cursor")
        local wmCursorCheckbox = newCheckbutton(
            "",
            "Show the world marker you're about to place on your cursor",
            function(_, checked)
                EchoRaidToolsDB.Miscellaneous.wmCursor = checked
                updateWmCursorEnable()
            end,
            mainContainer
        )
        wmCursorCheckbox:SetPoint("TOPLEFT", wmCursorLabel, "BOTTOMLEFT", 0, -8)
        wmCursorCheckbox.CheckButton:SetChecked(EchoRaidToolsDB.Miscellaneous.wmCursor)

        -- Cursor size slider
        --local wmCursorSizeLabel = mainContainer:CreateFontString(nil, "ARTWORK", "GameFontNormal")
        --wmCursorSizeLabel:SetPoint("LEFT", wmCursorCheckbox, "RIGHT", 20, 0)
        --wmCursorSizeLabel:SetText("World Marker Cursor Size")
        local wmCursorSizeSlider = CreateFrame("Frame", nil, mainContainer, "EchoRaidTools_BetterSlider")
        wmCursorSizeSlider:SetPoint("LEFT", wmCursorCheckbox, "RIGHT", 110, 0)
        wmCursorSizeSlider:Setup(10, 100, 1, function() return EchoRaidToolsDB.Miscellaneous.wmCursorSize end, function(value)
            EchoRaidToolsDB.Miscellaneous.wmCursorSize = value
            wmCursorFrame:SetSize(value, value)
        end, 10, 100)
        wmCursorSizeSlider.TopText:SetText("Cursor Marker Size")
        wmCursorSizeSlider.TopText:Show()

        return window
    end

    function _Miscellaneous:GetButtonInfo()
		return "Miscellaneous",
			20,
			function()
				return _Miscellaneous:GetConfig()
			end,
			_Miscellaneous:GetNavigationButtons()
	end
end
