--===================================================
--
--				GHM_CustomDD
--  			GHM_CustomDD.lua
--
--	          (description)
--
-- 	  (c)2013 The Gryphonheart Team
--			All rights reserved
--===================================================

local count = 1;
local listCount = 1;

GHM_CreateButtonList = function(list, ddFrame, width)
-- ddFrame = frame that triggers the DD to show.
-- list = properties of the DD Menu items
	local menuFrame
	local class = GHClass("GHM_ButtonMenuList"..listCount);
	local buttons = {}
	local t = {
		buttons,
		name = "GHM_ButtonMenuList" .. listCount,
		theme = "BlankTheme",
		useWindow = false,
	}

	listCount = listCount + 1
	
		
	local function insertMenuButton(buttonIndex, buttonData, menuParent)
		local button = {}
				
		if type(buttonData) == "string" then
			button.type = "TextButton"
			button.text = buttonData
			button.value = buttonData
			button.index = buttonIndex
			button.width = (width or 128)
			button.OnClick = function(self)
				if ddFrame then
					ddFrame.index = self.index
					ddFrame.value = self.value
					if ddFrame.text then
						ddFrame.text:SetText(buttonData.text or buttonData.value)
					end
				end
				if ddFrame.OnSelect then
					ddFrame.OnSelect(self.index, self.value)
				end
				if menuFrame:IsShown() then
					menuFrame:Hide()
				else
					menuFrame:Show()
				end
			end
			
			return button
			
		elseif type(buttonData) == "table" then
			local subMenuFrame
			if buttonData.subMenu then
				subMenuFrame = GHM_CreateButtonList(buttonData.subMenu, self,width, menuFrame)
			end
			if buttonData.font then
				button.font = buttonData.font
			end
			if buttonData.color then
				button.color = buttonData.color
			end
			if buttonData.column then
				
			end
			button.type = "TextButton"
			button.text = buttonData.text or buttonData.value
			button.value = buttonData.value or buttonData.text
			button.index = buttonData.index or i
			button.width = (width or 128)
			if buttonData.subMenu then
			
				button.OnClick = function(self)
					if ddFrame then
						ddFrame.index = self.index
						ddFrame.value = self.value
					end
					
					if ddFrame.OnSelect then
						ddFrame.OnSelect(self.index, self.value, buttonData.text)
					end
					if buttonData.OnClick then
						buttonData.OnClick(self, self.index, self.value, buttonData.text)
					end
					subMenuFrame:SetParent(self)
					subMenuFrame:SetPoint("TOPLEFT",self,"TOPRIGHT",4,0)
					if subMenuFrame:IsShown() then
						subMenuFrame:Hide()
						menuParent:Hide()
					else
						subMenuFrame:Show()
					end
				end
			else
				button.OnClick = function(self)
					if ddFrame then
						ddFrame.index = self.index
						ddFrame.value = self.value
					end					
					if ddFrame.OnSelect then
						ddFrame.OnSelect(self.index, self.value, buttonData.text)
					end
					if buttonData.OnClick then
						buttonData.OnClick(self, self.index, self.value, buttonData.text)
					end
					if menuFrame:IsShown() then
						menuFrame:Hide()
					else
						menuFrame:Show()
					end
				end			
			end
			
			return button
		end
	end
	
	local row = 1
	local listPos

	for i, v in pairs(list) do		  
	  if not buttons[i] and i <= 10 then
		buttons[i] = {}
	  end
	  listPos = i
	  row = listPos	  
	  if row > 10 then
		row = row - (10 * math.floor(listPos / 10))
		if row == 0 then row = 10 end
	  end
	  local button = insertMenuButton(i, v, ddFrame)
	  table.insert(buttons[row],button)
	end
	
	t.width = (#buttons[1] * (width or 128))
		
	menuFrame = GHM_NewFrame(class,t)
	menuFrame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",
		edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
		tile = true,
		tileSize = 16,
		edgeSize = 16, 
		insets = { left = 4, right = 4, top = 4, bottom = 4 }
	})
	menuFrame:SetBackdropBorderColor(TOOLTIP_DEFAULT_COLOR.r, TOOLTIP_DEFAULT_COLOR.g, TOOLTIP_DEFAULT_COLOR.b);
	menuFrame:SetBackdropColor(TOOLTIP_DEFAULT_BACKGROUND_COLOR.r, TOOLTIP_DEFAULT_BACKGROUND_COLOR.g, TOOLTIP_DEFAULT_BACKGROUND_COLOR.b);
	
	if ddFrame then
		menuFrame:SetFrameStrata("DIALOG")
		menuFrame:SetPoint("TOPLEFT",ddFrame,"BOTTOMLEFT",0,-4)
	end
	
	menuFrame:SetScript("OnLeave", function(self, motion)
		if motion == true then
			local newFrame = GetMouseFocus()
			if newFrame:GetParent():GetParent():GetParent() ~= menuFrame then
					GHI_Timer(function()
					menuFrame:Hide()
					end,
				3,true)
			end
		end
	end)
	
	return menuFrame
end

function GHM_DropDown(parent, main, profile)
	local frame
	frame = CreateFrame("Frame","GHM_DropDown" .. count ,parent,"GHM_DropDown_Template")
	count = count + 1
	local label = _G[frame:GetName().."Label"];
	local area = _G[frame:GetName().."Area"];
	local ddFrame = _G[area:GetName().."DD"];
	local ddButton = _G[area:GetName().."Button"]
	local miscAPI = GHI_MiscAPI().GetAPI()
	
	frame:SetWidth(profile.width or 80)
		
	local returnIndex = profile.returnIndex;	
	label.label:SetText(profile.text or "");
	
	ddFrame:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background",
		edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
		tile = true,
		tileSize = 16,
		edgeSize = 16, 
		insets = { left = 4, right = 4, top = 4, bottom = 4 }
	})
	ddFrame:SetBackdropBorderColor(TOOLTIP_DEFAULT_COLOR.r, TOOLTIP_DEFAULT_COLOR.g, TOOLTIP_DEFAULT_COLOR.b);
	ddFrame:SetBackdropColor(TOOLTIP_DEFAULT_BACKGROUND_COLOR.r, TOOLTIP_DEFAULT_BACKGROUND_COLOR.g, TOOLTIP_DEFAULT_BACKGROUND_COLOR.b, 0.75);
	
	local menuFrame
	
	local GetData = function()
		if type(profile.dataFunc) == "function" then
			local data = profile.dataFunc() or {}
			frame.data = data
			return data
		elseif type(profile.data) == "table" then
			frame.data = profile.data
			return frame.data;
		elseif type(profile.data) == "function" then
			local data = profile.data() or {}
			frame.data = data
			return data;
		elseif type(frame.dataFunc) == "function" then
			local data = frame.dataFunc() or {}
			frame.data = data
			return data
		elseif type(frame.data) == "table" then
			return frame.data;
		elseif type(frame.data) == "function" then
			local data = frame.data() or {}
			frame.data = data
			return data;
		end
		return {};
	end
		
	local t = GetData()
	menuFrame = GHM_CreateButtonList(t, ddFrame)
	
	if profile.OnSelect then
		ddFrame.OnSelect = function(index, value, text)
			profile.OnSelect(index, value, text)
			if profile.quality == true then
				local labelText = miscAPI.GHI_ColorString(text, ITEM_QUALITY_COLORS[value].r, ITEM_QUALITY_COLORS[value].g,ITEM_QUALITY_COLORS[value].b)
				ddFrame.text:SetText(labelText)
			else
				ddFrame.text:SetText(text or value or index)
			end
		end
	else
		ddFrame.OnSelect = function(index, value, text)
			if profile.quality == true then
				local labelText = miscAPI.GHI_ColorString(text, ITEM_QUALITY_COLORS[value].r, ITEM_QUALITY_COLORS[value].g,ITEM_QUALITY_COLORS[value].b)
				ddFrame.text:SetText(labelText)
			else
			ddFrame.text:SetText(text or value or index)
			end
		end
	end
	
	ddButton:SetScript("OnClick", function(self)
		if menuFrame:IsShown() then
			menuFrame:Hide()
		else
			menuFrame:Show()
		end
	end)
			
	GHM_FramePositioning(frame,profile,parent);
	
	local varAttFrame;
	local Force1 = function(input)
		-- Set the value and ui to 'data', evt using help functions
		if type(input) == "string" then
		
				for i=1,#(frame.data) do
					if frame.data[i].value == input then
						ddFrame.OnSelect(i, frame.data[i].value, frame.data[i].text );
					elseif frame.data[i].text == input then
						break;
					end
				end
				
		elseif type(input) == "number" then
			if profile.quality == true then
				ddFrame.OnSelect((input + 1), input ,frame.data[(input + 1)].text);
			else
				ddFrame.OnSelect(input, input, frame.data[input].text);
			end
		end		
	end

   local Force2 = function(inputType, inputValue)
        if (inputType == "attribute" or inputType == "variable") and varAttFrame then -- Handles input to var/Att frame
            varAttFrame:SetValue(inputType, inputValue);

        else -- static
            varAttFrame:Clear();
            Force1(inputValue);
        end
    end

    frame.Force = function(self, ...) -- Calls Force1 or Force2 depending on the number of inputs. Either Force(value) or Force(Type,value)
        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)
        -- Clear the value / ui to a default state
		ddFrame.value = frame.data[1];
		ddFrame.index = 1;
		ddFrame.text:SetText(ddFrame.value);
    end
		
	frame.EnableVariableAttributeInput = function(self, scriptingEnv, item)
        if not (varAttFrame) then
            varAttFrame = GHM_VarAttInput(frame, area, frame:GetWidth());
            frame:SetHeight(frame:GetHeight());
        end
        varAttFrame:EnableVariableAttributeInput(scriptingEnv, item, profile.outputOnly)
    end

    frame.GetValue = function(self) -- Get the current value
        if (varAttFrame and not (varAttFrame:IsStaticTabShown())) then
            return varAttFrame:GetValue();
        else
            if returnIndex == true then
				return ddFrame.index;
			end
			return ddFrame.value, ddFrame.index;
        end
    end	

    -- Trigger envt onLoad function
    if type(profile.OnLoad) == "function" then
        profile.OnLoad(frame);
    end

    frame:Show();
	frame.Clear()
	
    return frame;	
end