

local function createSpacer(width)
	local label = WhatsNext.AceGUI:Create('Label');
	label:SetText(" ");
	if not width or width <= 0 then
		label:SetFullWidth(true)
	else
		label:SetWidth(width)
	end
	return label
end

local function UpdateSelected()
	local frame = WhatsNext.rotationPickerFrame;
	
	local selected = frame.scrollTable:GetSelection()
	frame.removeConditionButton:SetDisabled(not selected)
	frame.okayButton:SetDisabled(not selected)
end

local function UpdateRotationTable()
	local tableData = WhatsNext.rotationPickerFrame.tableData
	wipe(tableData)
	
	for i, rotation in ipairs(WhatsNext.db.profile.castSequences[WhatsNext.class][WhatsNext.spec]) do
		tinsert(tableData,{i,rotation.name,#rotation,rotation.lastModified and date("%m/%d/%y %H:%M",rotation.lastModified)})
	end
	
	WhatsNext.rotationPickerFrame.scrollTable:SortData();
end

function WhatsNext:ShowRotationPickerFrame(rtnFunction)
	if not self.rotationPickerFrame then
		local frame = self.AceGUI:Create("Window")
		frame:PauseLayout()
		_G["WhatsNext_RotationPickerFrame"] = frame
		WhatsNext_AddOptionsScreen(frame.frame)
		
		
		--tinsert(UISpecialFrames, "WhatsNext_RotationPickerFrame") 
		self.rotationPickerFrame = frame
		frame:SetWidth(750)
		frame:SetHeight(278)
		frame:SetTitle("What's Next? - Rotation Picker")
		frame:SetStatusText("")
		frame:EnableResize(false);
		frame:SetLayout("Flow")
		frame:SetAutoAdjustHeight(false)
		
		frame.frame.texture = frame.frame:CreateTexture(nil, "BACKGROUND")
		frame.frame.texture:SetColorTexture(0,0,0,1)
		frame.frame.texture:SetPoint("TOPLEFT",frame.frame,"TOPLEFT",8,-8)
		frame.frame.texture:SetPoint("BOTTOMRIGHT",frame.frame,"BOTTOMRIGHT",-6,8)
		
		local cols = {{
				['name'] = '',
				['width'] = 1,
				['align'] = 'LEFT',
			},{
				['name'] = 'Name',
				['width'] = 250,
				['align'] = 'LEFT',
			},{
				['name'] = 'Size',
				['width'] = 40,
				['align'] = 'LEFT',
			},{
				['name'] = 'Last Modified',
				['width'] = 95,
				['align'] = 'CENTER',
			}
		}
				
				
		frame.tableData = {};
		frame.scrollTable = self.ScrollingTable:CreateST(cols,6,nil,{["r"] = .2, ["g"] = .2, ["b"] = .2, ["a"] = 1,});
		frame.scrollTable:SetData(frame.tableData, true);
		frame.scrollTable:EnableSelection(true)

		frame.scrollTable:RegisterEvents({
			['OnClick'] = function(rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, btn,...)
				WhatsNext:ScheduleTimer(UpdateSelected, .0001)
			end,	
			['OnDoubleClick'] = function(rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, btn,...)
				for i, rotation in ipairs(WhatsNext.db.profile.castSequences[WhatsNext.class][WhatsNext.spec]) do
					rotation.use = i == data[realrow][1]
				end
				rtnFunction();
			end
		});
		
		local tableWrapper = self.AceGUI:Create('lib-st'):WrapST(frame.scrollTable);
		tableWrapper.head_offset = 15;
		tableWrapper.frame:SetScale(1.74)
		frame:AddChild(tableWrapper);
			
		
		
		
		
		
		frame.addConditionButton = self.AceGUI:Create('Button');
		frame.addConditionButton:SetWidth(80);
		frame.addConditionButton:SetText('Create');
		frame.addConditionButton:SetCallback('OnClick', function()
			for i, rotation in ipairs(WhatsNext.db.profile.castSequences[WhatsNext.class][WhatsNext.spec]) do
				rotation.use = false
			end
			rtnFunction();
		end);
		
		frame.removeConditionButton = self.AceGUI:Create('Button');
		frame.removeConditionButton:SetWidth(80);
		frame.removeConditionButton:SetText('Delete');
		frame.removeConditionButton:SetCallback('OnClick', function()
			local selected = frame.scrollTable:GetSelection()
			tremove(WhatsNext.db.profile.castSequences[WhatsNext.class][WhatsNext.spec],selected)
			frame.scrollTable:ClearSelection()
			UpdateRotationTable()
			UpdateSelected()
			
		end);
		
		frame.okayButton = self.AceGUI:Create('Button');
		frame.okayButton:SetWidth(80);
		frame.okayButton:SetText('Activate');
		frame.okayButton:SetCallback('OnClick', function()
			for i, rotation in ipairs(WhatsNext.db.profile.castSequences[WhatsNext.class][WhatsNext.spec]) do
				rotation.use = false
			end
		
			local selected = frame.scrollTable:GetSelection()
			for i, rotation in ipairs(WhatsNext.db.profile.castSequences[WhatsNext.class][WhatsNext.spec]) do
				rotation.use = i == selected
			end
			rtnFunction();
		end);
		
		frame:AddChild(createSpacer())
		frame:AddChild(createSpacer())
		frame:AddChild(createSpacer())
		frame:AddChild(createSpacer())
		frame:AddChild(createSpacer())
		frame:AddChild(createSpacer())
		frame:AddChild(createSpacer())
		frame:AddChild(createSpacer())
		frame:AddChild(frame.addConditionButton)
		frame:AddChild(frame.removeConditionButton)
		frame:AddChild(createSpacer(486))
		frame:AddChild(frame.okayButton)
		frame:ResumeLayout()
		frame:DoLayout()
	end
	self.rotationPickerFrame:SetCallback("OnClose", rtnFunction)
	UpdateRotationTable()
	self.rotationPickerFrame.scrollTable:ClearSelection()
	UpdateSelected()
	WhatsNext_ShowOptionsScreen(self.rotationPickerFrame.frame)
end