--Lua functions
local AddOnName, nsVars = ...
local ss = nsVars.SS

--local function myColorCallback(restore)
--  local newR, newG, newB, newA;
--  if restore then
--    -- The user bailed, we extract the old color from the table created by ShowColorPicker.
--    newR, newG, newB, newA = unpack(restore);
--  else
--    -- Something changed
--    newA, newR, newG, newB = OpacitySliderFrame:GetValue(), ColorPickerFrame:GetColorRGB();
--  end
--
--  -- Update our internal storage.
--  r, g, b, a = newR, newG, newB, newA;
--  -- And update any UI elements that use this color...
--end

--function ShowColorPicker(r, g, b, a, changedCallback)
--  ColorPickerFrame:SetColorRGB(r,g,b);
--  ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (a ~= nil), a;
--  ColorPickerFrame.previousValues = {r,g,b,a};
--  ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc =
--  changedCallback, changedCallback, changedCallback;
--  ColorPickerFrame:Hide(); -- Need to run the OnShow handler.
--  ColorPickerFrame:Show();
--end

local function myColorCallback(restore)
  local newR, newG, newB, newA;
  if restore then
    -- The user bailed, we extract the old color from the table created by ShowColorPicker.
    newR, newG, newB, newA = unpack(restore);
  else
    -- Something changed
    newA, newR, newG, newB = OpacitySliderFrame:GetValue(), ColorPickerFrame:GetColorRGB();
  end

  -- Update our internal storage.
  r, g, b, a = newR, newG, newB, newA;
  -- And update any UI elements that use this color...
end

local showColorPicker = function(r, g, b, a, changedCallback)
--  ColorPickerFrame:SetColorRGB(r,g,b);
--  ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (a ~= nil), a;
--  ColorPickerFrame.previousValues = {r,g,b,a};
--  ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc =
--  changedCallback, changedCallback, changedCallback;
--  ColorPickerFrame:Hide(); -- Need to run the OnShow handler.
--  ColorPickerFrame:Show();




  local cFrame = CreateFrame("ColorSelect", "SSColorSelect", UIParent)
cFrame:SetWidth(600)
cFrame:SetHeight(300)
cFrame:SetPoint("CENTER",0,0)

  ss:MakeFrameMovable(cFrame)

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


  cFrame.valueTextureFrame = CreateFrame("Frame",nil,cFrame)
  cFrame.valueTextureFrame.texture = cFrame.valueTextureFrame:CreateTexture(nil,"OVERLAY")
  cFrame.valueTextureFrame:SetWidth(40)
  cFrame.valueTextureFrame:SetHeight(120)
  cFrame.valueTextureFrame:SetPoint("Left",20,0)
  cFrame.valueTextureFrame.texture:SetAllPoints(true)
  cFrame:SetColorValueTexture(cFrame.valueTextureFrame.texture)



  cFrame.cWheelTextureFrame = CreateFrame("Frame",nil,cFrame)
  cFrame.cWheelTextureFrame.texture = cFrame.cWheelTextureFrame:CreateTexture(nil,"OVERLAY")
  cFrame.cWheelTextureFrame:SetWidth(100)
  cFrame.cWheelTextureFrame:SetHeight(100)
  cFrame.cWheelTextureFrame:SetPoint("Left",80,0)
  cFrame.cWheelTextureFrame.texture:SetAllPoints(true)
  cFrame:SetColorWheelTexture(cFrame.cWheelTextureFrame.texture)

--  cFrame.valueTexture = cFrame:CreateTexture(nil,"BACKGROUND")


  cFrame:SetColorRGB(1, 2, 0)
  cFrame:Show()

  ss.Debug.describe(cFrame, 3)
end

--slash command method
local function CommandFunc(msg)
  local cmd, rest = msg:match("^(%S*)%s*(.-)$");
  local restdouble2
rest, restdouble2 = rest:match("^(%S*)%s*(.-)$");

  if cmd == "nuke" then
    ss:ResetSpheres()
  elseif cmd == "r" then
    ReloadUI()
  elseif cmd == "debug" then
    if rest == "color" then
      showColorPicker(0.5,0.5,0.5,1,nil)
    elseif rest == "cursor" then
      ss.Debug.describe({GetCursorPosition()})
    elseif rest == "spell" then
      ss.Debug.describe({GetSpellCharges(restdouble2)})
    elseif rest == "spellinfo" then
      ss.Debug.describe({GetSpellInfo(restdouble2)})
    elseif rest == "spellcd" then
      ss.Debug.describe({GetSpellCooldown(restdouble2)})
    elseif rest == "spellcount" then
      ss.Debug.describe({GetSpellCount(restdouble2)})
    elseif rest == "microsphere" then
      ss.Debug.describe(ss.MicroSphereData.get())
    elseif rest == "buffinfo" then
      ss.Debug.describe({UnitBuff("player", restdouble2)})
    elseif rest == "petbuffinfo" then
      ss.Debug.describe({UnitBuff("pet", restdouble2), GetPetTimeRemaining()})
    elseif rest == "isplayerspell" then
      ss.Debug.describe({IsPlayerSpell(restdouble2)})
    elseif rest == "talentinfo" then
      ss.Debug.describe({GetTalentInfo(5,1,1)})
    elseif rest == "microclasses" then
       ss.Layout:ToggleMicroSpheres(false)
       ss.Layout:ToggleMicroSpheres(false, "mainResource")
    elseif rest == "combat" then
      ss.Debug.describe({UnitAffectingCombat("player")})
    elseif rest == "mr" then
      ss.Debug.describe(ss.profile.microResources, 3)
    elseif rest then
      print(UnitHealth(rest, restdouble2))
      print(UnitHealthMax(rest, restdouble2))
      print(UnitPower(rest, restdouble2))
      print(UnitPowerMax(rest, restdouble2))
    end

  else
    ss:ToggleConfig()
  end
end

--setup the slash command stuff, hide the GUI
SlashCmdList["SS"] = CommandFunc;
SLASH_SS1 = "/ss";
