--[[
	Addon: Taos Group Ultimate
	Author: TProg Taonnor
	Created by @Taonnor
]]--

--[[
	Local variables
]]--
local LOG_ACTIVE = false
local _logger = nil

local SETTINGS_VERSION = 5

--[[
	Table TGT_SettingsHandler
]]--
TGT_SettingsHandler = {}
TGT_SettingsHandler.__index = TGT_SettingsHandler

--[[
	Table Members
]]--
TGT_SettingsHandler.Name = "TGT-SettingsHandler"
TGT_SettingsHandler.SettingsName = "TaosGroupToolSettings"
TGT_SettingsHandler.SavedVariables = nil
TGT_SettingsHandler.Default =
{
    ["PosX"] = 0,
    ["PosY"] = 0,
    ["SelectorPosX"] = 0,
    ["SelectorPosY"] = 0,
    ["OnlyAva"] = false,
    ["IsSortingActive"] = false,
    ["Movable"] = true,
    ["Style"] = 3,
    ["StaticUltimateID"] = 29861,
    ["SwimlaneUltimateGroupIds"] =
    {
        [1] = 29861,
        [2] = 27413,
        [3] = 86536,
        [4] = 86112,
        [5] = 46537,
        [6] = 46622,
    },
}

--[[
	Sets SetStyleSettings and fires TGT-StyleChanged callbacks
]]--
function TGT_SettingsHandler.SetStyleSettings(style)
    if (LOG_ACTIVE) then
        _logger:logTrace("TGT_SettingsHandler.SetStyleSettings")
        _logger:logDebug("style", style)
    end

    local numberStyle = tonumber(style)

    if (numberStyle == 1 or numberStyle == 2 or numberStyle == 3) then
        TGT_SettingsHandler.SavedVariables.Style = numberStyle

        CALLBACK_MANAGER:FireCallbacks(TGT_STYLE_CHANGED)
    else
        _logger:logError("TGT_SettingsHandler.SetStyleSettings, invalid style " .. tostring(style))
    end
end

--[[
	Sets MovableSettings and fires TGT-MovableChanged callbacks
]]--
function TGT_SettingsHandler.SetStaticUltimateIDSettings(staticUltimateID)
    if (LOG_ACTIVE) then
        _logger:logTrace("TGT_SettingsHandler.StaticUltimateIDSettings")
        _logger:logDebug("staticUltimateID", staticUltimateID)
    end

    TGT_SettingsHandler.SavedVariables.StaticUltimateID = staticUltimateID

    CALLBACK_MANAGER:FireCallbacks(TGT_STATIC_ULTIMATE_ID_CHANGED, staticUltimateID)
end

--[[
	Sets MovableSettings and fires TGT-MovableChanged callbacks
]]--
function TGT_SettingsHandler.SetSwimlaneUltimateGroupIdSettings(swimlane, ultimateGroup)
    if (LOG_ACTIVE) then
        _logger:logTrace("TGT_SettingsHandler.StaticUltimateIDSettings")
        _logger:logDebug("swimlane", swimlane)
        _logger:logDebug("ultimateGroup", ultimateGroup)
    end

    TGT_SettingsHandler.SavedVariables.SwimlaneUltimateGroupIds[swimlane] = ultimateGroup.GroupAbilityId

    CALLBACK_MANAGER:FireCallbacks(TGT_SWIMLANE_ULTIMATE_GROUP_ID_CHANGED, swimlane, ultimateGroup)
end

--[[
	Sets MovableSettings and fires TGT-MovableChanged callbacks
]]--
function TGT_SettingsHandler.SetMovableSettings(movable)
    if (LOG_ACTIVE) then
        _logger:logTrace("TGT_SettingsHandler.SetMovableSettings")
        _logger:logDebug("movable", movable)
    end

    TGT_SettingsHandler.SavedVariables.Movable = movable

    CALLBACK_MANAGER:FireCallbacks(TGT_MOVABLE_CHANGED, movable)
end

--[[
	Sets MovableSettings and fires TGT-MovableChanged callbacks
]]--
function TGT_SettingsHandler.SetOnlyAvaSettings(onlyAva)
    if (LOG_ACTIVE) then
        _logger:logTrace("TGT_SettingsHandler.SetOnlyAvaSettings")
        _logger:logDebug("onlyAva", onlyAva)
    end

    TGT_SettingsHandler.SavedVariables.OnlyAva = onlyAva

    CALLBACK_MANAGER:FireCallbacks(TGT_IS_ZONE_CHANGED)
end

--[[
	Sets IsSortingActive settings
]]--
function TGT_SettingsHandler.SetIsSortingActiveSettings(isSortingActive)
    if (LOG_ACTIVE) then
        _logger:logTrace("TGT_SettingsHandler.SetIsLgsActiveSettings")
        _logger:logDebug("isSortingActive", isSortingActive)
    end

    TGT_SettingsHandler.SavedVariables.IsSortingActive = isSortingActive
end

--[[
	Gets SimpleList visible in connection with selected style
]]--
function TGT_SettingsHandler.IsSimpleListVisible()
    if (LOG_ACTIVE) then _logger:logTrace("TGT_SettingsHandler.IsSimpleListVisible") end
    if (TGT_SettingsHandler.SavedVariables ~= nil) then
        if (LOG_ACTIVE) then _logger:logDebug("style", TGT_SettingsHandler.SavedVariables.Style) end
        return tonumber(TGT_SettingsHandler.SavedVariables.Style) == 1 and TGT_SettingsHandler.IsControlsVisible()
    else
        _logger:logError("TGT_SettingsHandler.SavedVariables is nil")
        return false
    end
end

--[[
	Gets SwimlaneList visible in connection with selected style
]]--
function TGT_SettingsHandler.IsSwimlaneListVisible()
    if (LOG_ACTIVE) then _logger:logTrace("TGT_SettingsHandler.IsSwimlaneListVisible") end
    if (TGT_SettingsHandler.SavedVariables ~= nil) then
        if (LOG_ACTIVE) then _logger:logDebug("style", TGT_SettingsHandler.SavedVariables.Style) end
        return tonumber(TGT_SettingsHandler.SavedVariables.Style) == 2 and TGT_SettingsHandler.IsControlsVisible()
    else
        _logger:logError("TGT_SettingsHandler.SavedVariables is nil")
        return false
    end
end

--[[
	Gets CompactSwimlaneList visible in connection with selected style
]]--
function TGT_SettingsHandler.IsCompactSwimlaneListVisible()
    if (LOG_ACTIVE) then _logger:logTrace("TGT_SettingsHandler.IsCompactSwimlaneListVisible") end
    if (TGT_SettingsHandler.SavedVariables ~= nil) then
        if (LOG_ACTIVE) then _logger:logDebug("style", TGT_SettingsHandler.SavedVariables.Style) end
        return tonumber(TGT_SettingsHandler.SavedVariables.Style) == 3 and TGT_SettingsHandler.IsControlsVisible()
    else
        _logger:logError("TGT_SettingsHandler.SavedVariables is nil")
        return false
    end
end

--[[
	Gets CompactSwimlaneList visible in connection with selected style
]]--
function TGT_SettingsHandler.IsControlsVisible()
    if (LOG_ACTIVE) then _logger:logTrace("TGT_SettingsHandler.IsControlsVisible") end
    if (TGT_SettingsHandler.SavedVariables ~= nil) then
        if (LOG_ACTIVE) then _logger:logDebug("onlyAvA", TGT_SettingsHandler.SavedVariables.OnlyAva) end
        if (TGT_SettingsHandler.SavedVariables.OnlyAva) then
            _logger:logDebug("isPlayerInAvAWorld", IsPlayerInAvAWorld())
            return IsPlayerInAvAWorld()
        else
            return true
        end
    else
        _logger:logError("TGT_SettingsHandler.SavedVariables is nil")
        return false
    end
end

--[[
	OnPlayerActivated sends IsZoneChanged event
]]--
function TGT_SettingsHandler.OnPlayerActivated(eventCode)
    if (LOG_ACTIVE) then
        _logger:logTrace("TGT_SettingsHandler.OnPlayerActivated")
    end

    CALLBACK_MANAGER:FireCallbacks(TGT_IS_ZONE_CHANGED)
end

--[[
	Initialize loads SavedVariables
]]--
function TGT_SettingsHandler.Initialize(logger)
    if (LOG_ACTIVE) then logger:logTrace("TGT_SettingsHandler.Initialize") end

    _logger = logger

    TGT_SettingsHandler.SavedVariables = ZO_SavedVars:NewAccountWide(TGT_SettingsHandler.SettingsName, SETTINGS_VERSION, nil, TGT_SettingsHandler.Default)

    -- Register
    EVENT_MANAGER:RegisterForEvent(TGT_SettingsHandler.Name, EVENT_PLAYER_ACTIVATED, TGT_SettingsHandler.OnPlayerActivated)
end