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

local pairs, string = pairs, string
local UIDropDownMenu_SetSelectedID, UIDropDownMenu_SetText, UIDropDownMenu_AddButton = UIDropDownMenu_SetSelectedID, UIDropDownMenu_SetText, UIDropDownMenu_AddButton
local CreateFrame, CloseDropDownMenus, UIDropDownMenu_GetText = CreateFrame, CloseDropDownMenus, UIDropDownMenu_GetText
local table = table

ss["ConfigProfile"] = {
};

function ss:InitializeConfigProfilePanel()
  -- Create the config preview frame
  ss.ConfigProfile = CreateFrame("Frame","SeamoreSpheresConfigProfile",ss.Config)
  ss.ConfigProfile:SetFrameStrata("HIGH")
  ss.ConfigProfile:SetSize(457,250)
  ss.ConfigProfile:SetPoint("RIGHT",450,125)

  -- Add the background
  ss.ConfigProfile: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.ConfigProfile.ConfigPreviewCloseButton = CreateFrame("Button", "ConfigProfileCloseButton", ss.ConfigProfile, "UIPanelCloseButton")
  ss.ConfigProfile.ConfigPreviewCloseButton:SetSize(35, 35)
  ss.ConfigProfile.ConfigPreviewCloseButton:SetPoint("TOPLEFT", 422, 0)
  ss.ConfigProfile.ConfigPreviewCloseButton:SetText("")
  ss.ConfigProfile.ConfigPreviewCloseButton:SetScript("OnClick", function()
    ss.ConfigProfile:Hide()
  end)

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

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

  -- Add profile select dropdown
  ss.ConfigProfile.fontLayer1.profileToUseLabel = ss.ConfigProfile.fontLayer1:CreateFontString("ProfileToUseLabel")
  ss.ConfigProfile.fontLayer1.profileToUseLabel:SetFont(ss.private.font["general"], 19)
  ss.ConfigProfile.fontLayer1.profileToUseLabel:SetPoint("TOPLEFT",25,-55)
  ss.ConfigProfile.fontLayer1.profileToUseLabel:SetSize(181, 39)
  ss.ConfigProfile.fontLayer1.profileToUseLabel:SetTextColor(1,0,0,1)
  ss.ConfigProfile.fontLayer1.profileToUseLabel:SetText("Profile to Use:")
  ss.ConfigProfile.fontLayer1.profileToUseLabel:SetJustifyV("TOP")

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

      for k,v in pairs(ss.global) do
        local profileStart = {string.split("_", k)};

        if profileStart[1] == "SSProfile" then
          stuff = {}
          stuff.text = profileStart[2]
          stuff.value = k
          stuff.checked = ss.profile.profileName == k

          stuff.func = function(self)
            UIDropDownMenu_SetSelectedID(ss.ConfigProfile.ProfileToUseDropDown, self:GetID())
            UIDropDownMenu_SetText(ss.ConfigProfile.ProfileToUseDropDown, self:GetText())
            ss.profile.profileName = k
            ss.db = ss:SetMetatables(ss.global[k], ss.private["SSProfile_Default"]);

            if not ss.db.Initialized then
              ss.ConfigProfile.InitClassModesButton:Show()
            else
              ss.ConfigProfile.InitClassModesButton:Hide()
            end

            ss.Layout:Refresh()

            CloseDropDownMenus()
          end

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

  -- Add profile copy dropdown
  ss.ConfigProfile.fontLayer1.copyProfileLabel = ss.ConfigProfile.fontLayer1:CreateFontString("CopyProfileLabel")
  ss.ConfigProfile.fontLayer1.copyProfileLabel:SetFont(ss.private.font["general"], 19)
  ss.ConfigProfile.fontLayer1.copyProfileLabel:SetPoint("TOPLEFT",25,-85)
  ss.ConfigProfile.fontLayer1.copyProfileLabel:SetSize(181, 39)
  ss.ConfigProfile.fontLayer1.copyProfileLabel:SetTextColor(1,0,0,1)
  ss.ConfigProfile.fontLayer1.copyProfileLabel:SetText("Copy Profile:")
  ss.ConfigProfile.fontLayer1.copyProfileLabel:SetJustifyV("TOP")

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

      for k,v in pairs(ss.global) do
        local profileStart = {string.split("_", k)};

        if profileStart[1] == "SSProfile" then
          stuff = {}
          stuff.notCheckable = true
          stuff.text = profileStart[2]
          stuff.value = profileStart[2]

          stuff.func = function(self)
            UIDropDownMenu_SetText(ss.ConfigProfile.CopyProfileDropDown, self:GetText())
            ss.global[ss.profile.profileName] = table.copy(ss.global[k], true)
            ss.db = ss.global[ss.profile.profileName]

            if not ss.db.Initialized then
              ss.ConfigProfile.InitClassModesButton:Show()
            else
              ss.ConfigProfile.InitClassModesButton:Hide()
            end

            ss.Layout:Refresh()

            CloseDropDownMenus()
          end

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

  -- Add the create profile field
  ss.ConfigProfile.newProfileEditBox = CreateFrame("EditBox","NewProfileEditBox",ss.ConfigProfile, "InputBoxTemplate")
  ss.ConfigProfile.newProfileEditBox:SetSize(150, 25)
  ss.ConfigProfile.newProfileEditBox:SetPoint("BOTTOMLEFT",75,60)
  ss.ConfigProfile.newProfileEditBox:SetMaxLetters(20)
  ss.ConfigProfile.newProfileEditBox:SetText("")
  ss.ConfigProfile.newProfileEditBox:SetAutoFocus(false)
  ss.ConfigProfile.newProfileEditBox:SetScript("OnEnterPressed", function(self)
    self:ClearFocus()
  end)

  ss.ConfigProfile.CreateProfileButton = CreateFrame("Button", "CreateProfileButton", ss.ConfigProfile, "GameMenuButtonTemplate")
  ss.ConfigProfile.CreateProfileButton:SetSize(133, 25)
  ss.ConfigProfile.CreateProfileButton:SetPoint("BOTTOMLEFT", 250, 60)
  ss.ConfigProfile.CreateProfileButton:SetText("Create Profile")
  ss.ConfigProfile.CreateProfileButton:SetScript("OnClick", function()
    local val = string.gsub(ss.ConfigProfile.newProfileEditBox:GetText(), "_", "")

    if val ~= "" and not ss.global["SSProfile_"..val] then
      ss.global["SSProfile_"..val] = {}
      ss.profile.profileName = "SSProfile_"..val
      ss.db = ss:SetMetatables(ss.global[ss.profile.profileName], ss.pVars.spheres["SSProfile_Default"]);

      if not ss.db.Initialized then
        ss.ConfigProfile.InitClassModesButton:Show()
      else
        ss.ConfigProfile.InitClassModesButton:Hide()
      end

      ss.Layout:Refresh()
    end
  end)

  -- Add the delete profile field
  ss.ConfigProfile.DeleteProfileDropDown = ss.Config.createDropdown{
    name = "DeleteProfileDropDown",
    parent = ss.ConfigProfile,
    tAlign = "LEFT",
    len = 150,
    width = 50,
    anchor = "BOTTOMLEFT",
    x = 55,
    y = 20,
    init = function(self, level)
      local stuff = {}

      for k,v in pairs(ss.global) do
        local profileStart = {string.split("_", k)};

        if profileStart[1] == "SSProfile" then
          stuff = {}
          stuff.notCheckable = true
          stuff.text = profileStart[2]
          stuff.value = profileStart[2]

          stuff.func = function(self)
            UIDropDownMenu_SetText(ss.ConfigProfile.DeleteProfileDropDown, self:GetText())
            CloseDropDownMenus()
          end

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

  ss.ConfigProfile.DeleteProfileButton = CreateFrame("Button", "DeleteProfileButton", ss.ConfigProfile, "GameMenuButtonTemplate")
  ss.ConfigProfile.DeleteProfileButton:SetSize(133, 25)
  ss.ConfigProfile.DeleteProfileButton:SetPoint("BOTTOMLEFT", 250, 20)
  ss.ConfigProfile.DeleteProfileButton:SetText("Delete Profile")
  ss.ConfigProfile.DeleteProfileButton:SetScript("OnClick", function()
    local val = UIDropDownMenu_GetText(ss.ConfigProfile.DeleteProfileDropDown)

    if val ~= "" and val ~= "Select Profile" and val ~= "Default" then
      if ss.profile.profileName == "SSProfile_"..val then
        ss.profile.profileName = "SSProfile_Default"
        ss.db = ss:SetMetatables(ss.global["SSProfile_Default"], ss.private["SSProfile_Default"]);

        if not ss.db.Initialized then
          ss.ConfigProfile.InitClassModesButton:Show()
        else
          ss.ConfigProfile.InitClassModesButton:Hide()
        end
      end

      ss.global["SSProfile_"..val] = nil
      ss.Layout:Refresh()
      UIDropDownMenu_SetText(ss.ConfigProfile.DeleteProfileDropDown, "Select Profile")
    end
  end)

  ss.ConfigProfile.InitClassModesButton = CreateFrame("Button", "InitClassModesButton", ss.ConfigProfile, "GameMenuButtonTemplate")
  ss.ConfigProfile.InitClassModesButton:SetSize(133, 25)
  ss.ConfigProfile.InitClassModesButton:SetPoint("CENTER", 0, -15)
  ss.ConfigProfile.InitClassModesButton:SetText("Initialize Class Modes")
  ss.ConfigProfile.InitClassModesButton:SetScript("OnClick", function()
    if not ss.db.Initialized then
      ss:InitModes()
      ss.ConfigProfile.InitClassModesButton:Hide()
    end
  end)

  ss.ConfigProfile.InitClassModesButton:SetScript("OnShow", function()
    if ss.db.Initialized then
      ss.ConfigProfile.InitClassModesButton:Hide()
    end
  end)
end
