--[[
Author: Jarth
Filename: CBs_List.lua
]] --

-------------------------------------------------------------------------------------------------
-- FUNCTIONS --
-------------------------------------------------------------------------------------------------
CBs_List = ZO_SortFilterList:Subclass()

function CBs_List:New(...)
    self.list = ZO_SortFilterList.New(self, ...)
    return self.list
end

function CBs_List:Initialize(base, _type, ...)
    ZO_SortFilterList.Initialize(self, ...)
    self.base = base
    self._type = _type
    self.masterList = {}

    self:SetAlternateRowBackgrounds(false)
    ZO_ScrollList_AddDataType(
        self.list,
        1,
        "CBs_ListRow",
        40,
        function(control, data)
            control._type = self._type
            control.base = self.base
            control.data = data
            self:SetupListRow(control, data)
        end
    )
    ZO_ScrollList_AddDataType(
        self.list,
        2,
        "CBs_Divider",
        6,
        function(control, data)
            ZO_SortFilterList.SetupRow(self, control, data)
        end
    )
end

function CBs_List:SetupListRow(control, data)
    ZO_SortFilterList.SetupRow(self, control, data)

    control.checkbox = GetControl(control, "Checkbox")
    if control.checkbox then
        control.checkbox.tooltipText = data.tooltipText
        ZO_CheckButton_SetCheckState(control.checkbox, control.data.valueFunc and control.data.valueFunc())
        ZO_CheckButton_SetLabelText(control.checkbox, data.name)
        if control.checkbox.label then
            control.checkbox.label.defaultNormalColor = ZO_DEFAULT_ENABLED_COLOR
            control.checkbox.label:ClearAnchors()
            control.checkbox.label:SetAnchor(TOPLEFT, control.checkbox, TOPLEFT, -425)
            control.checkbox.label:SetAnchor(BOTTOMRIGHT, control.checkbox, BOTTOMLEFT)
        end
        ZO_CheckButton_SetToggleFunction(control.checkbox, control.data.func)
        ZO_CheckButton_SetEnableState(control.checkbox, not (control.data.disabledFunc and control.data.disabledFunc()))
    end
end

function CBs_List:BuildMasterList()
    self.masterList = {}

    local function CBs_List_Show_Disabled_Collectible(checkBoxControl, newValue)
        local control = checkBoxControl:GetParent()
        control._type.Saved.MenuShowDisabled = newValue
        control.base.CreateCollection(control._type)
        control.base.Global.SettingsList:RefreshData()
    end

    local function CBs_List_Select_All_Collectible(checkBoxControl, newValue)
        local control = checkBoxControl:GetParent()
        control.base.SelectAll(control._type, newValue)
        control.base.RestoreFrame(control._type)
        control.base.RestoreCombineLabels()
        control.base.Global.SettingsList:RefreshData()
    end

    local function CBs_List_Select_Collectible(checkBoxControl, newValue)
        local control = checkBoxControl:GetParent()
        if not (control.data.disabledFunc and control.data.disabledFunc()) then
            if newValue == true then
                control._type.Saved.Selected[control.data.cId] = newValue
            else
                control._type.Saved.Selected[control.data.cId] = nil
            end
            control.base.RestoreFrame(control._type)
            control.base.RestoreCombineLabels()
            control.base.Global.SettingsList:RefreshData()
        end
    end

    if self._type then
        table.insert(
            self.masterList,
            {
                name = "Show disabled " .. self._type.Name,
                tooltipText = "When ON disabled elements will be shown for " .. self._type.Name,
                valueFunc = function()
                    return self._type.Saved.MenuShowDisabled
                end,
                func = CBs_List_Show_Disabled_Collectible,
                type = "show_disabled_collectible"
            }
        )
        table.insert(self.masterList, {type = "divider"})
        table.insert(
            self.masterList,
            {
                name = "Select all " .. self._type.Name,
                tooltipText = "When pressed all " .. self._type.Name .. " will either be selected or deselected",
                valueFunc = function()
                    return self.base.IsAllSelected(self._type)
                end,
                func = CBs_List_Select_All_Collectible,
                type = "select_all_collectible"
            }
        )
        table.insert(self.masterList, {type = "divider"})

        if self._type.OrderedCollection then
            for _, collection in ipairs(self._type.OrderedCollection) do
                if self._type.Saved.MenuShowDisabled and collection.Disabled or not collection.Disabled then
                    table.insert(
                        self.masterList,
                        {
                            cId = collection.Id,
                            name = "Show " .. collection.Name,
                            tooltipText = collection.Tooltip,
                            valueFunc = function()
                                return self._type.Saved.Selected[collection.Id]
                            end,
                            disabledFunc = function()
                                return collection.Disabled
                            end,
                            func = CBs_List_Select_Collectible,
                            type = "select_collectible"
                        }
                    )
                end
            end
        end
    end
end

function CBs_List:FilterScrollList()
    local scrollData = ZO_ScrollList_GetDataList(self.list)
    ZO_ClearNumericallyIndexedTable(scrollData)

    for i = 1, #self.masterList do
        if self.masterList[i].type == "divider" then
            scrollData[#scrollData + 1] = ZO_ScrollList_CreateDataEntry(2, self.masterList[i])
        else
            scrollData[#scrollData + 1] = ZO_ScrollList_CreateDataEntry(1, self.masterList[i])
        end
    end
end

function CBs_List:SortScrollList()
end