--function to make any frame object movable
local AddOnName, nsVars = ...
local ss = nsVars.SS

local CreateFrame, UIDropDownMenu_AddButton = CreateFrame, UIDropDownMenu_AddButton
local pairs, CloseDropDownMenus, UIDropDownMenu_SetText = pairs, CloseDropDownMenus, UIDropDownMenu_SetText
local table, GetSpellCharges, strlower = table, GetSpellCharges, strlower
local select, string, tonumber = select, string, tonumber
local UIDropDownMenu_SetSelectedID, UIDropDownMenu_GetText = UIDropDownMenu_SetSelectedID, UIDropDownMenu_GetText

ss["ConfigMicroSpheres"] = {
};

function ss:InitializeConfigMicroSpheresPanel()
    -- Create the config preview frame
    ss.ConfigMicroSpheres = CreateFrame("Frame","SeamoreSpheresConfigMicroSpheres",ss.Config)
    ss.ConfigMicroSpheres:SetFrameStrata("HIGH")
    ss.ConfigMicroSpheres:SetSize(457,500)
    ss.ConfigMicroSpheres:SetPoint("RIGHT",450,0)

    -- Add the background
    ss.ConfigMicroSpheres:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
        tile = true,
        tileSize = 32,
        edgeSize = 32,
        insets = {
            left = 11,
            right = 12,
            top = 12,
            bottom = 11
        },
    });

    ss.ConfigMicroSpheres:SetScript("OnHide", function()
        ss.ConfigMicroSpheres.selectedMicroGroup = ""
        UIDropDownMenu_SetText(ss.Config.MicroSphereSettingsDropDown, "Select Group")
        UIDropDownMenu_SetText(ss.Config.SecondaryMicroSphereSettingsDropDown, "Select Group")
    end)

    ss.ConfigMicroSpheres.ConfigPreviewCloseButton = CreateFrame("Button", "ConfigMicroSpheresCloseButton", ss.ConfigMicroSpheres, "UIPanelCloseButton")
    ss.ConfigMicroSpheres.ConfigPreviewCloseButton:SetSize(35, 35)
    ss.ConfigMicroSpheres.ConfigPreviewCloseButton:SetPoint("TOPLEFT", 422, 0)
    ss.ConfigMicroSpheres.ConfigPreviewCloseButton:SetText("")
    ss.ConfigMicroSpheres.ConfigPreviewCloseButton:SetScript("OnClick", function()
        ss.ConfigMicroSpheres:Hide()
    end)

    -- Add the title
    ss.ConfigMicroSpheres.fontLayer1 = CreateFrame("Frame", "ConfigMicroSpheresFontLayer", ss.ConfigMicroSpheres)
    ss.ConfigMicroSpheres.fontLayer1:SetAllPoints(ss.ConfigMicroSpheres)

    ss.ConfigMicroSpheres.fontLayer1.Title = ss.ConfigMicroSpheres.fontLayer1:CreateFontString(nil, "BossEmoteNormalHuge")
    ss.ConfigMicroSpheres.fontLayer1.Title:SetFont(ss.private.font["general"], 26)
    ss.ConfigMicroSpheres.fontLayer1.Title:SetPoint("TOP",0,-18)
    ss.ConfigMicroSpheres.fontLayer1.Title:SetSize(400, 39)
    ss.ConfigMicroSpheres.fontLayer1.Title:SetTextColor(1,0,0,1)
    ss.ConfigMicroSpheres.fontLayer1.Title:SetText("Micro Sphere Settings")
    ss.ConfigMicroSpheres.fontLayer1.Title:SetJustifyV("TOP")

    ss.ConfigMicroSpheres.selectedMicroGroup = ""
    ss.ConfigMicroSpheres.recources = {}
    ss.ConfigMicroSpheres.group1 = {}
    ss.ConfigMicroSpheres.group2 = {}

    -- Add resource type select dropdown
    ss.ConfigMicroSpheres.fontLayer1.resourceTypeLabel = ss.ConfigMicroSpheres.fontLayer1:CreateFontString("ResourceTypeLabel")
    ss.ConfigMicroSpheres.fontLayer1.resourceTypeLabel:SetFont(ss.private.font["general"], 19)
    ss.ConfigMicroSpheres.fontLayer1.resourceTypeLabel:SetPoint("TOPLEFT",25,-55)
    ss.ConfigMicroSpheres.fontLayer1.resourceTypeLabel:SetSize(181, 39)
    ss.ConfigMicroSpheres.fontLayer1.resourceTypeLabel:SetTextColor(1,0,0,1)
    ss.ConfigMicroSpheres.fontLayer1.resourceTypeLabel:SetText("Resource Type:")
    ss.ConfigMicroSpheres.fontLayer1.resourceTypeLabel:SetJustifyV("TOP")

    ss.ConfigMicroSpheres.resourceGroups = {
        buff = ss.BuffData.hasBuffs() and "Buff" or nil,
        spellCharges = ss.SpellChargeData.hasSpells() and "Spell Charges" or nil,
    };

    ss.ConfigMicroSpheres.ResourceTypeDropDown = ss.Config.createDropdown{
        name = "ResourceTypeDropDown",
        parent = ss.ConfigMicroSpheres,
        tAlign = "LEFT",
        len = 400,
        width = 50,
        anchor = "TOPLEFT",
        x = 200,
        y = -50,
        init = function(self, level)
            local stuff = {}

            for k,v in pairs(ss.ConfigMicroSpheres.resourceGroups) do
                stuff = {}
                stuff.text = v
                stuff.value = k
                stuff.notCheckable = true

                stuff.func = function(self)
                    UIDropDownMenu_SetText(ss.ConfigMicroSpheres.ResourceTypeDropDown, self:GetText())

                    if k == "buff" then
                        ss.ConfigMicroSpheres.resourceOptionList = "BuffData"
                    elseif k == "spellCharges" then
                        ss.ConfigMicroSpheres.resourceOptionList = "SpellChargeData"
                    else
                        ss.ConfigMicroSpheres.resourceOptions = nil
                    end

                    CloseDropDownMenus()
                end

                UIDropDownMenu_AddButton(stuff, level)
            end
        end,
    };

    -- Add resource add select dropdown
    ss.ConfigMicroSpheres.fontLayer1.resourceAddLabel = ss.ConfigMicroSpheres.fontLayer1:CreateFontString("ResourceAddLabel")
    ss.ConfigMicroSpheres.fontLayer1.resourceAddLabel:SetFont(ss.private.font["general"], 19)
    ss.ConfigMicroSpheres.fontLayer1.resourceAddLabel:SetPoint("TOPLEFT",25,-85)
    ss.ConfigMicroSpheres.fontLayer1.resourceAddLabel:SetSize(181, 39)
    ss.ConfigMicroSpheres.fontLayer1.resourceAddLabel:SetTextColor(1,0,0,1)
    ss.ConfigMicroSpheres.fontLayer1.resourceAddLabel:SetText("Add Resource:")
    ss.ConfigMicroSpheres.fontLayer1.resourceAddLabel:SetJustifyV("TOP")

    ss.ConfigMicroSpheres.ResourceAddDropDown = ss.Config.createDropdown{
        name = "ResourceAddDropDown",
        parent = ss.ConfigMicroSpheres,
        tAlign = "LEFT",
        len = 400,
        width = 50,
        anchor = "TOPLEFT",
        x = 200,
        y = -80,
        init = function(self, level)
            local stuff = {}

            if ss.ConfigMicroSpheres.resourceOptionList and ss[ss.ConfigMicroSpheres.resourceOptionList] then
                for k,v in pairs(ss[ss.ConfigMicroSpheres.resourceOptionList][strlower(ss.curClass)] or {}) do
                    if (ss.ConfigMicroSpheres.resourceOptionList == "SpellChargeData" and GetSpellCharges(v.name)) or ss.ConfigMicroSpheres.resourceOptionList ~= "SpellChargeData" or (v.name == "Stormstrike" and strlower(ss.curSpecName) == "enhancement") then
                        stuff = {}
                        stuff.notCheckable = true
                        stuff.text = ss.ConfigMicroSpheres.resourceOptionList == "BuffData" and v[1] or v.name
                        stuff.value = k

                        stuff.func = function(self)
                            v.ssClass = v.ssClass or ss.ConfigMicroSpheres.resourceOptionList

                            if ss.ConfigMicroSpheres.resourceOptionList == "BuffData" then
                                ss.ConfigMicroSpheres.recources = ss.ConfigMicroSpheres.recources.buffGroup and ss.ConfigMicroSpheres.recources or { buffGroup = {}, }

                                if table.getn(ss.ConfigMicroSpheres.recources.buffGroup) < 10 then
                                    table.insert(ss.ConfigMicroSpheres.recources.buffGroup, v)
                                end
                            else
                                ss.ConfigMicroSpheres.recources = v
                            end

                            CloseDropDownMenus()
                        end

                        UIDropDownMenu_AddButton(stuff, level)
                    end
                end
            end
        end,
    };

    -- Add resource remove select dropdown
    ss.ConfigMicroSpheres.fontLayer1.resourceRemoveLabel = ss.ConfigMicroSpheres.fontLayer1:CreateFontString("ResourceRemoveLabel")
    ss.ConfigMicroSpheres.fontLayer1.resourceRemoveLabel:SetFont(ss.private.font["general"], 19)
    ss.ConfigMicroSpheres.fontLayer1.resourceRemoveLabel:SetPoint("TOPLEFT",25,-115)
    ss.ConfigMicroSpheres.fontLayer1.resourceRemoveLabel:SetSize(181, 39)
    ss.ConfigMicroSpheres.fontLayer1.resourceRemoveLabel:SetTextColor(1,0,0,1)
    ss.ConfigMicroSpheres.fontLayer1.resourceRemoveLabel:SetText("Remove Resource:")
    ss.ConfigMicroSpheres.fontLayer1.resourceRemoveLabel:SetJustifyV("TOP")

    ss.ConfigMicroSpheres.ResourceRemoveDropDown = ss.Config.createDropdown{
        name = "ResourceRemoveDropDown",
        parent = ss.ConfigMicroSpheres,
        tAlign = "LEFT",
        len = 400,
        width = 50,
        anchor = "TOPLEFT",
        x = 200,
        y = -110,
        init = function(self, level)
            local stuff = {}

            if ss.ConfigMicroSpheres.recources and (ss.ConfigMicroSpheres.recources[1] or ss.ConfigMicroSpheres.recources.buffGroup) then
                for k,v in pairs(ss.ConfigMicroSpheres.recources.buffGroup or {ss.ConfigMicroSpheres.recources}) do
                    stuff = {}
                    stuff.notCheckable = true
                    stuff.text = v.ssClass == "BuffData" and v[1] or v.name
                    stuff.value = k

                    stuff.func = function(self)
                        if ss.ConfigMicroSpheres.recources.buffGroup and ss.ConfigMicroSpheres.recources.buffGroup[2] then
                            table.remove(ss.ConfigMicroSpheres.recources.buffGroup, k)
                        else
                            ss.ConfigMicroSpheres.recources = {}
                        end

                        CloseDropDownMenus()
                    end

                    UIDropDownMenu_AddButton(stuff, level)
                end
            end
        end,
    };

    -- Add Backgrounds.
    ss.ConfigMicroSpheres.fontLayer1.backgroundLabel = ss.ConfigMicroSpheres.fontLayer1:CreateFontString("BackgroundLabel")
    ss.ConfigMicroSpheres.fontLayer1.backgroundLabel:SetFont(ss.private.font["general"], 19)
    ss.ConfigMicroSpheres.fontLayer1.backgroundLabel:SetPoint("TOPLEFT",25,-145)
    ss.ConfigMicroSpheres.fontLayer1.backgroundLabel:SetSize(181, 39)
    ss.ConfigMicroSpheres.fontLayer1.backgroundLabel:SetTextColor(1,0,0,1)
    ss.ConfigMicroSpheres.fontLayer1.backgroundLabel:SetText("Background:")
    ss.ConfigMicroSpheres.fontLayer1.backgroundLabel:SetJustifyV("TOP")

    local textureNameTable = ss.FillTextures.list("primary")
    ss.ConfigMicroSpheres.backgroundDropDown = ss.Config.createDropdown{
        name = "backgroundDropDown",
        parent = ss.ConfigMicroSpheres,
        tAlign = "LEFT",
        len = 400,
        width = 50,
        anchor = "TOPLEFT",
        x = 200,
        y = -140,
        init = function(self, level)
            local stuff = {}

            for k,v in pairs(textureNameTable) do
                stuff = {}
                stuff.text = k
                stuff.value = k
                stuff.func = function(self)
                    UIDropDownMenu_SetText(ss.ConfigMicroSpheres.backgroundDropDown, self:GetText())
                    ss.ConfigMicroSpheres.selectedTexture = self:GetText()
                    CloseDropDownMenus()
                end

                stuff.checked = ss.ConfigMicroSpheres.selectedTexture == k
                UIDropDownMenu_AddButton(stuff, level)
            end
        end,
    };

    ss.ConfigMicroSpheres.MicroSphereSaveButton = CreateFrame("Button", "MicroSphereSaveButton", ss.ConfigMicroSpheres, "GameMenuButtonTemplate")
    ss.ConfigMicroSpheres.MicroSphereSaveButton:SetSize(133, 23)
    ss.ConfigMicroSpheres.MicroSphereSaveButton:SetPoint("BOTTOM", 138, 20)
    ss.ConfigMicroSpheres.MicroSphereSaveButton:SetText("Save Changes")
    ss.ConfigMicroSpheres.MicroSphereSaveButton:SetScript("OnClick", function()
        local index = select(2, string.split(" ", ss.ConfigMicroSpheres.selectedMicroGroup[1]))
        local fillTexture = UIDropDownMenu_GetText(ss.ConfigMicroSpheres.backgroundDropDown)

        if ss.ConfigMicroSpheres.region and ss.ConfigMicroSpheres.region == "secondary" then
            ss.profile.microResources.fillTextures[2] = fillTexture and fillTexture
            ss.Layout:UpdateMicroSphereTextures(fillTexture, "mainResource")
            ss.MicroSphereData.setSecondary(ss.ConfigMicroSpheres.recources, tonumber(index), ss.ConfigMicroSpheres.selectedMicroGroup[2])
        else
            ss.profile.microResources.fillTextures[1] = fillTexture and fillTexture
            ss.Layout:UpdateMicroSphereTextures(fillTexture)
            ss.MicroSphereData.set(ss.ConfigMicroSpheres.recources, tonumber(index), ss.ConfigMicroSpheres.selectedMicroGroup[2])
        end
    end)
end
