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

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

ss["ConfigProfile"] = {
};

function ss:InitializeConfigProfilePanel()
  -- Create the config preview frame
  ss.ConfigProfile = ss.ConfigTools.createInterfaceOption{
    name = "Profile",
    parent = AddOnName,
    refresh = function()
      if ss.db.Initialized then
        ss.ConfigProfile.InitClassModesButton:Hide()
      end

      local profileStart = {string.split("_", ss.profile.profileName)};
      UIDropDownMenu_SetText(ss.ConfigProfile.ProfileToUseDropDown, profileStart[2])
      UIDropDownMenu_SetText(ss.ConfigProfile.CopyProfileDropDown, "Select Profile")
      UIDropDownMenu_SetText(ss.ConfigProfile.DeleteProfileDropDown, "Select Profile")
    end,
  }

  -- Add the widget container
  ss.ConfigProfile.widgetFrame = ss.ConfigTools.getWidgetContainer(ss.ConfigProfile, "ProfileWidgetFrame")

  -- Add the title
  ss.ConfigProfile.widgetFrame.Title = ss.ConfigTools.createTitle("Profile Settings", ss.ConfigProfile.widgetFrame)

  -- Add profile select dropdown
  ss.ConfigProfile.ProfileToUseDropDown = ss.ConfigTools.createDropdown{
    name = "ProfileToUseDropDown",
    parent = ss.ConfigProfile,
    tAlign = "LEFT",
    width = 400,
    height = 50,
    anchor = "TOPLEFT",
    x = 200,
    y = (ss.ConfigTools.topMargin - (ss.ConfigTools.itemHeight * 2) - 15),
    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,
    label = {
      name = "ProfileToUseLabel",
      text = "Profile to Use",
    },
  }

  -- Add profile copy dropdown
  ss.ConfigProfile.CopyProfileDropDown = ss.ConfigTools.createDropdown{
    name = "CopyProfileDropDown",
    parent = ss.ConfigProfile,
    tAlign = "LEFT",
    width = 400,
    height = 50,
    anchor = "TOPLEFT",
    x = 200,
    y = (ss.ConfigTools.topMargin - (ss.ConfigTools.itemHeight * 3) - 15),
    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,
    label = {
      name = "CopyProfileLabel",
      text = "Copy Profile",
    },
  }

  -- Add the create profile field
  ss.ConfigProfile.newProfileEditBox = ss.ConfigTools.createEditBox{
    parent = ss.ConfigProfile,
    name = "NewProfileEditBox",
    width = 150,
    height = 25,
    anch = "BOTTOMLEFT",
    x = 22,
    y = 48,
    letters = 20,
    onEnterPressed = function(self)
      self:ClearFocus()
    end,
  }

  ss.ConfigProfile.CreateProfileButton =  ss.ConfigTools.createButton{
      parent = ss.ConfigProfile,
      name = "CreateProfileButton",
      anch = "BOTTOMLEFT",
      x = 195,
      y = 50,
      text = "Create Profile",
      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.ConfigTools.createDropdown{
    name = "DeleteProfileDropDown",
    parent = ss.ConfigProfile,
    tAlign = "LEFT",
    width = 150,
    height = 50,
    anchor = "BOTTOMLEFT",
    x = 0,
    y = 10,
    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 =  ss.ConfigTools.createButton{
    parent = ss.ConfigProfile,
    name = "DeleteProfileButton",
    anch = "BOTTOMLEFT",
    x = 195,
    y = 15,
    text = "Delete Profile",
    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,
  }

  -- Add the class modes init button
  ss.ConfigProfile.InitClassModesButton =  ss.ConfigTools.createButton{
    parent = ss.ConfigProfile,
    name = "InitClassModesButton",
    anch = "Top",
    x = 0,
    y = (ss.ConfigTools.topMargin - 60),
    text = "Initialize Class Modes",
    onClick = function()
      if not ss.db.Initialized then
        ss:InitModes()
        ss.ConfigProfile.InitClassModesButton:Hide()
      end
    end,
  }
end
