-- This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
-- To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
-- or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

local MereHealingFrames, privateVars = ...

MereHealingFrames.Config = MereHealingFrames.Config or {}
MereHealingFrames.Config.UI = MereHealingFrames.Config.UI or {}

MereHealingFrames.Config.UI.SampleUnits = {}

local SampleUnits = MereHealingFrames.Config.UI.SampleUnits

function SampleUnits:new (parentFrame)
	local this = {}

	setmetatable(this, self)
	self.__index = self
	this.initialized = false

	this.Frame = UI.CreateFrame("Frame", "SampleUnitsFrame", parentFrame)

	this.Frame.OriginalSetVisible = this.Frame.SetVisible
	this.Frame.SetVisible = function (...) MereHealingFrames.Config.UI.SampleUnits.SetVisible(this, ...) end
	return this
end

function SampleUnits:SetVisible(frame, visible)
	if (visible) then
		self:CreateUI()

		self.Frame.SetVisible = nil
		self.Frame.OriginalSetVisible = nil
		self.Frame:SetVisible(visible)
	else
		self.Frame:OriginalSetVisible(visible)
	end
end

function SampleUnits:GetContent()
	return self.Frame
end

function SampleUnits:CreateUI()
	self.initialized = true
	local parentFrame = self:GetContent()

	self.FiveManButton = UI.CreateFrame("RiftButton", "FiveManButton", parentFrame)
	self.TenManButton = UI.CreateFrame("RiftButton", "TenManButton", parentFrame)
	self.TwentyManButton = UI.CreateFrame("RiftButton", "TwentyManButton", parentFrame)
	self.CustomButton = UI.CreateFrame("RiftButton", "CustomButton", parentFrame)
	self.ClearButton = UI.CreateFrame("RiftButton", "ClearButton", parentFrame)

	self.FiveManButton:SetText("Set as 5 man")
	self.TenManButton:SetText("Set as 10 man")
	self.TwentyManButton:SetText("Set as 20 man")
	self.CustomButton:SetText("Set as custom")
	self.ClearButton:SetText("Clear units")

	self.FiveManButton.Event.LeftClick = function () MereHealingFrames.RaidManagement.AddFakes(5, false, 1, 1, 1, 2) end
	self.TenManButton.Event.LeftClick = function () MereHealingFrames.RaidManagement.AddFakes(10, false, 2, 3, 1, 4) end
	self.TwentyManButton.Event.LeftClick = function () MereHealingFrames.RaidManagement.AddFakes(20, false, 3, 6, 2, 9) end
	self.CustomButton.Event.LeftClick = function ()  SampleUnits.SetupCustomUnits(self) end
	self.ClearButton.Event.LeftClick = function () MereHealingFrames.RaidManagement.RemoveFakes() end

	self.TankSlider = MereHealingFrames.Config.UI.Utilities.LabelSlider:new(parentFrame, "Tanks:", 0, 20)
	self.HealSlider = MereHealingFrames.Config.UI.Utilities.LabelSlider:new(parentFrame, "Healers:", 0, 20)
	self.SupportSlider = MereHealingFrames.Config.UI.Utilities.LabelSlider:new(parentFrame, "Support:", 0, 20)
	self.DPSSlider = MereHealingFrames.Config.UI.Utilities.LabelSlider:new(parentFrame, "DPS:", 0, 20)

	self.TankSlider:GetContent():SetPoint("TOPCENTER", parentFrame, "TOPCENTER", 0, 30)
	self.HealSlider:GetContent():SetPoint("TOPCENTER", self.TankSlider:GetContent(), "BOTTOMCENTER")
	self.SupportSlider:GetContent():SetPoint("TOPCENTER", self.HealSlider:GetContent(), "BOTTOMCENTER")
	self.DPSSlider:GetContent():SetPoint("TOPCENTER", self.SupportSlider:GetContent(), "BOTTOMCENTER")

	self.TankSlider:SetCurrentValue(3)
	self.HealSlider:SetCurrentValue(6)
	self.SupportSlider:SetCurrentValue(2)
	self.DPSSlider:SetCurrentValue(9)

	self.CustomButton:SetPoint("TOPCENTER", self.DPSSlider:GetContent(), "BOTTOMCENTER")

	self.FiveManButton:SetPoint("TOPCENTER", self.CustomButton, "BOTTOMCENTER", 0, 30)
	self.TenManButton:SetPoint("TOPCENTER", self.FiveManButton, "BOTTOMCENTER")
	self.TwentyManButton:SetPoint("TOPCENTER", self.TenManButton, "BOTTOMCENTER")
	self.ClearButton:SetPoint("TOPCENTER", self.TwentyManButton, "BOTTOMCENTER", 0, 30)
end

function SampleUnits:SetupCustomUnits()
	local tanks, heals, support, dps = self.TankSlider:GetCurrentValue(), self.HealSlider:GetCurrentValue(), self.SupportSlider:GetCurrentValue(), self.DPSSlider:GetCurrentValue()
	local total = tanks + heals + support + dps

	MereHealingFrames.RaidManagement.AddFakes(total, false, tanks, heals, support, dps)
end