--===================================================
--
--                GHM_Slider
--              GHM_Slider.lua
--
--              GHM_Slider object for GHM
--
--       (c)2013 The Gryphonheart Team
--            All rights reserved
--===================================================

local count = 1;

function GHM_Slider(parent, main, profile)
  local frame = CreateFrame("Frame", "GHM_Slider" .. count, parent, "GHM_Slider_Template");
  count = count + 1;
  
  -- declaration and initialization
  profile = profile or {};
  local label = profile.label;
  
  local miscAPI = GHI_MiscAPI().GetAPI();
  local loc = GHI_Loc();
  
  -- setup
  local labelFrame = _G[frame:GetName() .. "TextLabel"];
  local slider = _G[frame:GetName() .. "Slider"];
  local valueLabel = _G[slider:GetName() .. "ValueLabel"];
  
  
  if profile.isStackSlider then
    labelFrame:SetText("Stack size:")
	slider.SliderValues = profile.values or {1,5,10,20,50,100}
  elseif profile.isSlotSlider then
    local text = profile.text or "Slots:"
    labelFrame:SetText(text)
	slider.SliderValues = {1,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36}
  elseif profile.isTimeSlider then
    slider.SliderValues = profile.values or {1,5,10,15,30,60,120,60*5,60*10,60*15,60*30,60*60,60*60*2,60*60*5,60*60*10,60*60*20,60*60*24}
	local text = profile.text or profile.label or ""
    labelFrame:SetText(text);
  else
    local text = profile.text or profile.label or ""
    labelFrame:SetText(text);
	slider.SliderValues = profile.values or {1,2,3}
  end
  
  slider:SetValueStep(1)
  slider:SetMinMaxValues(1,#slider.SliderValues)  
  
  local width = profile.width or frame:GetWidth();
  frame:SetWidth(width); 
  
  GHM_FramePositioning(frame,profile,parent);
  
  slider:SetScript("OnValueChanged",function(self,value)
      slider.val = slider.SliderValues[self:GetValue()];
      
      if profile.isTimeSlider then
        valueLabel:SetText(SecondsToTime(slider.val));
      elseif profile.isSlotSlider then
        valueLabel:SetText(slider.val..loc.SLOTS)
      else
        valueLabel:SetText(slider.val);
      end
      
      
      if not(slider.main == nil) then
        slider.main.SetLabel(self.label,self.val);
      end
	  if profile.OnValueChanged then
          profile.OnValueChanged(self,slider.val)
      end
  end)
  
  -- functions
  local varAttFrame;
  
  local Force1 = function(data)
    if type(data) == "number" and slider.SliderValues then
      for i=1,#(slider.SliderValues) do
        if slider.SliderValues[i] == data then
          slider:SetValue(tonumber(i));
          break;
        end
      end
    end
  end
  
  local Force2 = function(inputType, inputValue)
    if (inputType == "attribute" or inputType == "variable") and varAttFrame then
      slider:SetValue(1);
      varAttFrame:SetValue(inputType, inputValue);
      
    else -- static
      varAttFrame:Clear();
      slider:SetValue(inputValue);
    end
  end
  
  frame.Force = function(self, ...)
    if self ~= frame then return frame.Force(frame, self, ...); end
    local numInput = #({ ... });
    
    if numInput == 1 then
      Force1(...);
    elseif numInput == 2 then
      Force2(...);
    end
  end
  
  frame.Clear = function(self)
    slider:SetValue(1);
  end
  
  
  frame.EnableVariableAttributeInput = function(self, scriptingEnv, item)
    if not (varAttFrame) then
      varAttFrame = GHM_VarAttInput(frame, slider, slider:GetWidth());
      frame:SetHeight(frame:GetHeight() + 15);
    end
    varAttFrame:EnableVariableAttributeInput(scriptingEnv, item, profile.outputOnly)
  end
  
  frame.GetValue = function(self)
    if (varAttFrame and not (varAttFrame:IsStaticTabShown())) then
      return varAttFrame:GetValue();
    else
      local sliderIndex = slider:GetValue();
	  local value = slider.SliderValues[sliderIndex]
      return value
    end
  end
  
  
  
  if type(profile.OnLoad) == "function" then
    profile.OnLoad(frame);
  end
  
  
  frame:Show();
  --GHM_TempBG(frame);
  
  return frame;
end
