local addonName, SinsToolkit = ...

local frame = CreateFrame("Frame", addonName .. "AutoUnshiftFrame")
frame:RegisterEvent("PLAYER_LOGIN")

local function IsFeralOrGuardianDruid()
    local _, class = UnitClass("player")
    if class ~= "DRUID" then return false end
    local specIndex = GetSpecialization and GetSpecialization()
    local specID = specIndex and GetSpecializationInfo and GetSpecializationInfo(specIndex)
    return specID == 103 or specID == 104
end

local function DisableAutoUnshift()
    if IsFeralOrGuardianDruid() then
        if C_CVar and C_CVar.SetCVar then
            C_CVar.SetCVar("autoUnshift", 0)
        else
            SetCVar("autoUnshift", 0)
        end
    end
end

local function EnableAutoUnshift()
    if IsFeralOrGuardianDruid() then
        if C_CVar and C_CVar.SetCVar then
            C_CVar.SetCVar("autoUnshift", 1)
        else
            SetCVar("autoUnshift", 1)
        end
    end
end

function SinsToolkit.ApplyGSEAutoUnshift()
    if SinsToolkit.IsGSEAvailable and not SinsToolkit.IsGSEAvailable() then
        frame:UnregisterEvent("PLAYER_REGEN_DISABLED")
        frame:UnregisterEvent("PLAYER_REGEN_ENABLED")
        EnableAutoUnshift()
        return
    end

    if SinsToolkit.IsFeatureActive and SinsToolkit.IsFeatureActive("gseAutoUnshift") and IsFeralOrGuardianDruid() then
        frame:RegisterEvent("PLAYER_REGEN_DISABLED")
        frame:RegisterEvent("PLAYER_REGEN_ENABLED")
        if InCombatLockdown and InCombatLockdown() then
            DisableAutoUnshift()
        else
            EnableAutoUnshift()
        end
    else
        frame:UnregisterEvent("PLAYER_REGEN_DISABLED")
        frame:UnregisterEvent("PLAYER_REGEN_ENABLED")
        EnableAutoUnshift()
    end
end

frame:SetScript("OnEvent", function(_, event)
    if event == "PLAYER_LOGIN" then
        SinsToolkit.ApplyGSEAutoUnshift()
    elseif event == "PLAYER_REGEN_DISABLED" then
        DisableAutoUnshift()
    elseif event == "PLAYER_REGEN_ENABLED" then
        EnableAutoUnshift()
    end
end)

SLASH_SinsToolkitGSEAUTOUNSHIFT1 = "/sau"
SlashCmdList.SinsToolkitGSEAUTOUNSHIFT = function(msg)
    msg = (msg or ""):lower():match("^%s*(.-)%s*$")
    if msg == "toggle" then
        SinsToolkitDB.gseAutoUnshift = not SinsToolkitDB.gseAutoUnshift
        if SinsToolkit.ApplyDependencyGuards then SinsToolkit.ApplyDependencyGuards(false) end
        SinsToolkit.ApplyGSEAutoUnshift()
        if SinsToolkitDB.gseAutoUnshift then
            print("|cff9b59b6[Sin's Toolkit]|r Auto Unshift for Druids |cff00ff00enabled|r.")
        else
            print("|cff9b59b6[Sin's Toolkit]|r Auto Unshift for Druids |cffff4040disabled|r.")
        end
    elseif msg == "" or msg == "status" then
        if SinsToolkit.IsFeatureActive and SinsToolkit.IsFeatureActive("gseAutoUnshift") then
            print("|cff9b59b6[Sin's Toolkit]|r Auto Unshift for Druids is |cff00ff00enabled|r.")
        else
            print("|cff9b59b6[Sin's Toolkit]|r Auto Unshift for Druids is |cffff4040disabled|r.")
        end
    else
        print("|cff9b59b6[Sin's Toolkit]|r Usage: /sau status")
        print("|cff9b59b6[Sin's Toolkit]|r Usage: /sau toggle")
    end
end
