local _AddonName, _EchoRaidTools = ...;


local module = _EchoRaidTools:GetModule("EditModeTools")

local editor
local currentlyEditedLayout

local function loadSettingsForProfile(profileName)
    currentlyEditedLayout = profileName
    ---do things based on profileIndex or name?
    ---name probably
    --local settings = module:GetStoredSettingsForProfile(profileName)
    assert(editor)
    editor.SpecConditionCheckButton.CheckButton:UpdateSpecConditionCheckButton()
    editor.SpecConditionDropdown:UpdateSpecDropdown()
    editor.GroupSizeConditionCheckButton.CheckButton:UpdateGroupSizeConditionCheckButton()
    editor.GroupSizeMaxSlider:UpdateValues()
    editor.GroupSizeMinSlider:UpdateValues()
    editor.InstanceTypeConditionCheckButton.CheckButton:UpdateInstanceTypeConditionCheckButton()
    editor.InstanceTypeConditionDropdown:UpdateSpecDropdown()
end

local function setSettingForCurrentProfile(fieldTable, value)
    local current = module:GetStoredSettingsForProfile(currentlyEditedLayout)
    for i, key in ipairs(fieldTable) do
        if i == #fieldTable then
            local changed = current[key] ~= value
            current[key] = value
            return changed
        else
            current[key] = current[key] or {}
            current = current[key]
        end
    end
    module:UpdateEventHandler()
end

local function getSettingForCurrentProfile(fieldTable)
    local current = module:GetStoredSettingsForProfile(currentlyEditedLayout)
    for i, key in ipairs(fieldTable) do
        current = current[key]
        if not current then
            break
        end
    end
    return current
end

local function getEditModeLayouts()
    local layouts = {}
    local profiles, currentProfile = module:GetEditModeProfiles()
    for i, profileName in ipairs(profiles) do
        table.insert(layouts, {
            label = profileName,
            value = i,
            onclick = function() loadSettingsForProfile(profileName) end
        })
    end
    return layouts, currentProfile
end

function module:GetEditor()
    if not editor then
        editor = CreateFrame("FRAME", nil, _EchoRaidTools.panels.main, "EchoRaidToolsMainConfigWindow")
        editor:Hide()
        editor.Header.HeaderText:SetText("Edit Mode Tools")

        editor.Header.EnableModule:SetScript("OnClick", function(checkbox)
            module:ToggleModuleState()
            checkbox:SetChecked(module:GetModuleState())
        end)


        editor.ProfileDropdown = CreateFrame("Frame", nil, editor,
            "EchoRaidToolsConfig_Dropdown")
        editor.ProfileDropdown.TopText:SetText("Profile")
        editor.ProfileDropdown.TopText:Show()
        editor.ProfileDropdown:SetWidth(200)
        editor.ProfileDropdown:SetPoint("TOPLEFT", editor.Header, "BOTTOMLEFT", 10, -20)


        editor.SpecConditionCheckButton = CreateFrame("Frame", nil, editor, "EchoRaidToolsConfig_CheckButton")


        editor.SpecConditionCheckButton:SetSize(25, 25)
        editor.SpecConditionCheckButton.CheckButton:SetSize(25, 25)

        editor.SpecConditionCheckButton.TopText:Show()
        editor.SpecConditionCheckButton.TopText:SetText("Enable Spec")
        editor.SpecConditionCheckButton:SetPoint("TOPLEFT", editor.ProfileDropdown,
            "BOTTOMLEFT", 0, -40)




        editor.SpecConditionDropdown = CreateFrame("FRAME", nil, editor, "EchoRaidToolsConfig_Dropdown")


        editor.SpecConditionCheckButton.CheckButton.OptionsField = { "Spec", "Enabled" }


        function editor.SpecConditionCheckButton.CheckButton:UpdateSpecConditionCheckButton()
            if getSettingForCurrentProfile(self.OptionsField) == true then
                self:SetChecked(true)
                editor.SpecConditionDropdown:Enable()
                editor.SpecConditionDropdown:Enable()
            else
                self:SetChecked(false)
                editor.SpecConditionDropdown:Disable()
                editor.SpecConditionDropdown:Disable()
            end
        end

        editor.SpecConditionCheckButton.CheckButton:SetScript("OnClick", function(self)
            setSettingForCurrentProfile(self.OptionsField, self:GetChecked())
            self:UpdateSpecConditionCheckButton()
        end)



        editor.SpecConditionDropdown:SetPoint("TOPLEFT", editor.SpecConditionCheckButton, "BOTTOMLEFT", 0, 0)
        editor.SpecConditionDropdown:SetSize(200, 38)

        function editor.SpecConditionDropdown:UpdateSpecDropdown()
            assert(currentlyEditedLayout)
            local settings = module:GetStoredSettingsForProfile(currentlyEditedLayout)
            assert(settings)
            local specSettings = settings.Spec.Specs

            local specs = {
                togglesEnabled = true,
            }

            for i, spec in ipairs(module:GetSpecs()) do
                table.insert(specs, {
                    icon = spec.specIcon,
                    label = string.format("%s %s", spec.specName, spec.className),
                    value = spec.specID,
                    onclick = function(button, down, value, self)
                        setSettingForCurrentProfile({ "Spec", "Specs", spec.specID }, self.CheckButton:GetChecked())
                    end,
                    toggle = function(checked, button, down, value)
                        setSettingForCurrentProfile({ "Spec", "Specs", spec.specID }, checked)
                    end,
                    checked = specSettings[spec.specID] or false,
                })
            end
            editor.SpecConditionDropdown:SetupSelections(specs, 0)
        end

        editor.GroupSizeConditionCheckButton = CreateFrame("Frame", nil, editor, "EchoRaidToolsConfig_CheckButton")


        editor.GroupSizeConditionCheckButton:SetSize(25, 25)
        editor.GroupSizeConditionCheckButton.CheckButton:SetSize(25, 25)

        editor.GroupSizeConditionCheckButton.TopText:Show()
        editor.GroupSizeConditionCheckButton.TopText:SetText("Enable Group Size")
        editor.GroupSizeConditionCheckButton:SetPoint("TOPLEFT", editor.SpecConditionDropdown,
            "BOTTOMLEFT", 0, -40)




        editor.GroupSizeMaxSlider = CreateFrame("FRAME", nil, editor, "EchoRaidTools_BetterSlider")
        editor.GroupSizeMinSlider = CreateFrame("FRAME", nil, editor, "EchoRaidTools_BetterSlider")


        editor.GroupSizeConditionCheckButton.CheckButton.OptionsField = { "GroupSize", "Enabled" }
        function editor.GroupSizeConditionCheckButton.CheckButton:UpdateGroupSizeConditionCheckButton()
            if getSettingForCurrentProfile(self.OptionsField) == true then
                self:SetChecked(true)
                editor.GroupSizeMaxSlider:Enable()
                editor.GroupSizeMinSlider:Enable()
            else
                self:SetChecked(false)
                editor.GroupSizeMaxSlider:Disable()
                editor.GroupSizeMinSlider:Disable()
            end
        end

        editor.GroupSizeConditionCheckButton.CheckButton:SetScript("OnClick", function(self)
            setSettingForCurrentProfile(self.OptionsField, self:GetChecked())
            self:UpdateGroupSizeConditionCheckButton()
        end)



        local boundaries = { 1, 40, 1 }
        editor.GroupSizeMaxSlider:SetPoint("TOPLEFT", editor.GroupSizeConditionCheckButton, "BOTTOMLEFT", 0, 0)
        editor.GroupSizeMaxSlider:SetSize(200, 32)
        editor.GroupSizeMaxSlider.TopText:SetText("Max")
        editor.GroupSizeMaxSlider.TopText:ClearAllPoints()
        editor.GroupSizeMaxSlider.TopText:SetPoint("BOTTOM", editor.GroupSizeMaxSlider, "TOP")
        editor.GroupSizeMaxSlider.TopText:Show()

        editor.GroupSizeMaxSlider:Setup(
            boundaries[1], boundaries[2], boundaries[3],
            function()
                if not currentlyEditedLayout then return boundaries[2] end
                return getSettingForCurrentProfile({ "GroupSize", "Max" })
            end,
            function(value)
                setSettingForCurrentProfile({ "GroupSize", "Max" }, value)
            end, boundaries[1], boundaries[2])


        editor.GroupSizeMinSlider:SetPoint("TOPLEFT", editor.GroupSizeMaxSlider, "BOTTOMLEFT", 0, -10)
        editor.GroupSizeMinSlider:SetSize(200, 32)
        editor.GroupSizeMinSlider.TopText:SetText("Min")
        editor.GroupSizeMinSlider.TopText:ClearAllPoints()
        editor.GroupSizeMinSlider.TopText:SetPoint("BOTTOM", editor.GroupSizeMinSlider, "TOP")
        editor.GroupSizeMinSlider.TopText:Show()
        editor.GroupSizeMinSlider:Setup(
            boundaries[1], boundaries[2], boundaries[3],
            function()
                if not currentlyEditedLayout then return boundaries[1] end
                return getSettingForCurrentProfile({ "GroupSize", "Min" })
            end,
            function(value)
                setSettingForCurrentProfile({ "GroupSize", "Min" }, value)
            end, boundaries[1], boundaries[2])

        ---dropdown with all editmode profiles
        ---multidropdown spec list
        ---
        editor.InstanceTypeConditionCheckButton = CreateFrame("Frame", nil, editor, "EchoRaidToolsConfig_CheckButton")


        editor.InstanceTypeConditionCheckButton:SetSize(25, 25)
        editor.InstanceTypeConditionCheckButton.CheckButton:SetSize(25, 25)

        editor.InstanceTypeConditionCheckButton.TopText:Show()
        editor.InstanceTypeConditionCheckButton.TopText:SetText("Enable Instance Type")
        editor.InstanceTypeConditionCheckButton:SetPoint("TOPLEFT", editor.GroupSizeMinSlider,
            "BOTTOMLEFT", 0, -40)
        editor.InstanceTypeConditionDropdown = CreateFrame("FRAME", nil, editor, "EchoRaidToolsConfig_Dropdown")


        editor.InstanceTypeConditionCheckButton.CheckButton.OptionsField = { "InstanceType", "Enabled" }


        function editor.InstanceTypeConditionCheckButton.CheckButton:UpdateInstanceTypeConditionCheckButton()
            if getSettingForCurrentProfile(self.OptionsField) == true then
                self:SetChecked(true)
                editor.InstanceTypeConditionDropdown:Enable()
            else
                self:SetChecked(false)
                editor.InstanceTypeConditionDropdown:Disable()
            end
        end

        editor.InstanceTypeConditionCheckButton.CheckButton:SetScript("OnClick", function(self)
            setSettingForCurrentProfile(self.OptionsField, self:GetChecked())
            self:UpdateInstanceTypeConditionCheckButton()
        end)



        editor.InstanceTypeConditionDropdown:SetPoint("TOPLEFT", editor.InstanceTypeConditionCheckButton, "BOTTOMLEFT", 0,
            0)
        editor.InstanceTypeConditionDropdown:SetSize(200, 38)

        function editor.InstanceTypeConditionDropdown:UpdateSpecDropdown()
            assert(currentlyEditedLayout)
            local settings = module:GetStoredSettingsForProfile(currentlyEditedLayout)
            assert(settings)

            local instanceSettings = settings.InstanceType.InstanceTypes
            assert(instanceSettings)

            local instanceTypes = {
                {
                    label = "None",
                    value = 0,
                },
                {
                    label = "Looking For Raid",
                    value = 17,
                },
                {
                    label = "Normal Raid",
                    value = 14,
                },
                {
                    label = "Heroic Raid",
                    value = 15,
                },
                {
                    label = "Mythic Raid",
                    value = 16,
                },
                {
                    label = "Normal Dungeon",
                    value = 1,
                },
                {
                    label = "Heroic Dungeon",
                    value = 2,
                },
                {
                    label = "Timewalking Dungeon",
                    value = 24,
                },
                {
                    label = "Myhtic Dungeon",
                    value = 23,
                },
            }
            local instanceTypesDropdownList = {
                togglesEnabled = true,
            }
            for i, info in ipairs(instanceTypes) do
                instanceTypesDropdownList[i] = {
                    label = info.label,
                    value = info.value,
                    onclick = function(button, down, value, self)
                        setSettingForCurrentProfile({ "InstanceType", "InstanceTypes", info.value, },
                            self.CheckButton:GetChecked())
                    end,
                    toggle = function(checked, button, down, value)
                        setSettingForCurrentProfile({ "InstanceType", "InstanceTypes", info.value, },
                            checked)
                    end,
                    checked = instanceSettings[info.value] or false,
                }
            end
            editor.InstanceTypeConditionDropdown:SetupSelections(instanceTypesDropdownList, 0)
        end

        editor:SetScript("OnShow", function()
            editor.Header.EnableModule:SetChecked(module:GetModuleState())
            local layouts, activeLayout = getEditModeLayouts()
            editor.ProfileDropdown:SetupSelections(layouts, activeLayout)
            loadSettingsForProfile(layouts[activeLayout].label)
        end)
    end

    return editor
end
