local WIT, core = ...

local AceGUI = LibStub("AceGUI-3.0")

local function randomFarmModule(name, category)
    local self = core.FarmResultModule(name, {}, category)
    self.ConfigKey = "RandomFarm"

    local data = {}
    local contents = {}

    for k, c in pairs(core.Content) do
        if core.ProductHelper.IsRetail()
            or c == core.Content.Vanilla
            or (c == core.Content.TBC and core.ProductHelper.IsTBCOrHigher())
            or (c == core.Content.WotLK and core.ProductHelper.IsWotlkOrHigher())
            or (c == core.Content.Cata and core.ProductHelper.IsCataOrHigher())
        then
            table.insert(contents, { Value = c, Name = k })
        end
    end

    table.sort(contents, function(a, b)
        return a.Value > b.Value
    end)

    self.GetData = function ()
        return data
    end

    local function randomizeFarm()
        local ignoredIds = core.Config.GetUserIgnoredFarms()
        local allFarms = {}
        local filters = core.Config.GetRandomFarmFilters()

        for _, farm in pairs(core.Data.Results.Farms) do
            if not core.TableHelper.IndexOf(ignoredIds, farm.Id)
                and farm.Activity and core.TableHelper.IndexOf(filters.Activities, farm.Activity)
                and farm.Content and core.TableHelper.IndexOf(filters.Content, farm.Content) then
                table.insert(allFarms, farm)
            end
        end

        if core.TableHelper.IndexOf(filters.Content, -1) then
            for _, farm in pairs(core.Config.GetUserFarms()) do
                if not core.TableHelper.IndexOf(ignoredIds, farm.Id)
                    and (farm.Activity == nil or farm.Activity and core.TableHelper.IndexOf(filters.Activities, farm.Activity)) then
                    table.insert(allFarms, farm)
                end
            end
        end

        local farmsCount = #(allFarms)

        data = farmsCount > 0 and { allFarms[random(1, farmsCount)] } or {}

        local module = core.UI.MainWindow.CurrentModule()
        module.ClearCache()
        module.Refresh()
    end

    local function toggleActivityFilter(activity)
        if core.TableHelper.IndexOf(core.Config.GetRandomFarmFilters().Activities, activity) then
            core.TableHelper.RemoveValue(core.Config.GetRandomFarmFilters().Activities, activity)
        else
            table.insert(core.Config.GetRandomFarmFilters().Activities, activity)
        end

        self.refreshFilters()
    end

    local function getActivityFilter(activity)
        local name = "Activity" .. (activity or 0)
        return {
            Name = name,
            DisplayName = core.GetString(name),
            IsCheckable = true,
            IsChecked = core.TableHelper.IndexOf(core.Config.GetRandomFarmFilters().Activities, activity),
            Action = toggleActivityFilter,
            ActionArg = activity,
        }
    end

    local function getActivityFilterItems()
        local menu = {}

        table.insert(menu, getActivityFilter(core.Activity.Mining))
        table.insert(menu, getActivityFilter(core.Activity.Herbalism))
        table.insert(menu, getActivityFilter(core.Activity.Skinning))
        table.insert(menu, getActivityFilter(core.Activity.Fishing))
        table.insert(menu, getActivityFilter(core.Activity.Cloth))
        table.insert(menu, getActivityFilter(core.Activity.Transmog))
        table.insert(menu, getActivityFilter(core.Activity.Misc))
        table.insert(menu, getActivityFilter(core.Activity.Toys))
        table.insert(menu, getActivityFilter(core.Activity.RawGold))

        if core.ProductHelper.IsRetail() then
            table.insert(menu, getActivityFilter(core.Activity.BattlePets))
        end
        
        table.insert(menu, getActivityFilter(core.Activity.MultiFarms))
        table.insert(menu, getActivityFilter(core.Activity.GroupFarms))

        return menu
    end

    local function toggleContentFilter(content)
        if core.TableHelper.IndexOf(core.Config.GetRandomFarmFilters().Content, content) then
            core.TableHelper.RemoveValue(core.Config.GetRandomFarmFilters().Content, content)
        else
            table.insert(core.Config.GetRandomFarmFilters().Content, content)
        end

        self.refreshFilters()
    end

    local function getContentFilter(content)
        return {
            Name = content.Name,
            DisplayName = core.GetString(content.Name),
            IsCheckable = true,
            IsChecked = core.TableHelper.IndexOf(core.Config.GetRandomFarmFilters().Content, content.Value),
            Action = toggleContentFilter,
            ActionArg = content.Value,
        }
    end

    local function getContentFilterItems()
        local menu = {}

        table.insert(menu, getContentFilter({ Name = "MyFarms", Value = -1 }))

        for _, content in pairs(contents) do
            table.insert(menu, getContentFilter(content))
        end

        return menu
    end

    local function getFilters()
        return {
            {
                Name = "activities",
                DisplayName = core.GetString("Activities"),
                Children = getActivityFilterItems()
            },
            {
                Name = "content",
                DisplayName = core.GetString("Content"),
                Children = getContentFilterItems()
            }
        }
    end

    function self.refreshFilters()
        self.filterButton.ContextMenu:SetMenuList(getFilters())
    end

    local baseDraw = self.Draw
    function self.Draw(container)
        local frame = AceGUI:Create("SimpleGroup")
        frame:SetLayout("List")
        container:AddChild(frame)

        local buttons = AceGUI:Create("SimpleGroup")
        buttons:SetFullWidth(true)
        buttons:SetLayout("Flow")
        frame:AddChild(buttons)

        local randomButton = AceGUI:Create("Button")
        randomButton:SetText(core.GetString("RandomFarm"))
        randomButton:SetCallback("OnClick", randomizeFarm)
        buttons:AddChild(randomButton)

        self.filterButton = AceGUI:Create("Button")
        self.filterButton:SetText(core.GetString("Filters"))
        buttons:AddChild(self.filterButton)

        randomButton:SetWidth(frame.frame:GetWidth() - self.filterButton.frame:GetWidth() - 10)

        self.filterButton.ContextMenu = AceGUI:Create("WITDropDownMenu")
        self.filterButton.ContextMenu:SetAnchor(self.filterButton.frame, self.filterButton.frame:GetWidth() / 2 + 5, self.filterButton.frame:GetHeight() / 2 + 5)
        self.refreshFilters()

        self.filterButton:SetCallback("OnClick", function()
            self.filterButton.ContextMenu:Show()
        end)

        local gridFrame = AceGUI:Create("SimpleGroup")
        gridFrame:SetFullWidth(true)
        gridFrame:SetHeight(frame.frame:GetHeight() - (buttons and buttons.frame:GetHeight() or 0) - 10)
        gridFrame:SetLayout("Fill")

        baseDraw(gridFrame)

        frame:AddChild(gridFrame)
    end

    function self.GetDetailsRowData(row)
        return row.Data.ItemsOfNote or row.Data.Results or {}
    end

    return self
end

core.RandomFarmModule = randomFarmModule('RandomFarm', 'RandomFarm')

table.insert(core.Modules, core.RandomFarmModule)