-------------------------------------------------------------------------------
--Developer Advice --
--Response to "CONFIG_INIT_EVENT" -
--	userMods.SendEvent("CONFIG_INIT_EVENT_RESPONSE", { sender = common.GetAddonName() })
--Response to "CONFIG_EVENT_"..common.GetAddonName() -
--	Required -
--		NoB - This is the number of the button to be created.
--		name - This is for the name of the button.
--		btnType - This is the type of button; 
--			"Simple" - For simple buttons.
--			"T/F" - For true and false buttons.
--			"EditLine" - For user input lines.
--			"Color" - For a customizable color button.
--			"Slider" - For a sliding button.
--			"Tabled" - For a forward/backward table button.
--	Optional -
--		state - This is for the "T/F" button, true or false.
--		value - This is mainly for the button types "Tabled" and "EditLine" to update the value.
--		steps - This is for the number of steps for the "Slider" button.
--		pos - This is for the position the "Slider" button should start at.
--		color - This is the RGBA current value of the color button.
--		texture - This is the textureId of a texture.
--		sizeX - Size for X value of "Tabled" texture.
--		sizeY - Size for Y value of "Tabled" texture.
--		text - Text for "Tabled" text.
--		scale - Scale for "Tabled" text.
--	Example -
--		userMods.SendEvent("CONFIG_EVENT_RESPONSE", {NoB = 0, name = "Dance", btnType = "T/F", state = true})
--Response to "CONFIG_CHANGE_BUTTON_RESPONSE" -
--	Required -
--		NoB - Number of the button to change text.
--	Optional -
--		name - Name of the button. "EditLine" or "Tabled" buttons.
--		value - The new value for the button. "EditLine" or "Tabled" buttons.
--		texture - The new texture. "Tabled" buttons only.
--		sizeX - Size for X value of "Tabled" texture.
--		text - Text for "Tabled" text.
--		scale - Scale for "Tabled" text.
--	Example -
--		userMods.SendEvent("CONFIG_CHANGE_BUTTON_RESPONSE", {NoB = 0, name = "Dance2", value = "Tango"})
--Response from "CONFIG_SIMPLE_"..common.GetAddonName() -
--	Values Sent - {name}
--Response from "CONFIG_BUTTON_"..common.GetAddonName() -
--	Values Sent - {name, state}
--Response from "CONFIG_EDIT_LINE_"..common.GetAddonName() -
--	Values Sent - {name, text}
--Response from "CONFIG_COLOR_"..common.GetAddonName() -
--	Values Sent - {name, color}
--Response from "CONFIG_SLIDER_"..common.GetAddonName() -
--	Values Sent - {name, pos}
--Response from "CONFIG_TABLED_"..common.GetAddonName() -
--	Values Sent - {name, dir} (dir is "+" or "-")
--Response to "CONFIG_CLOSED_"..common.GetAddonName()
-------------------------------------------------------------------------------

Global("MainPanel", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("MainPanel", false))
Global("SideContainer", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("SideContainer", false))
Global("NormalContainer", MainPanel:GetChildChecked("NormalContainer", false))
Global("ColorPanel", MainPanel:GetChildChecked("ColorPanel", false))
Global("ConfigButton", {})
Global("ConfigText", {})
Global("ContIntPanel", {})
Global("UpperTV", MainPanel:GetChildChecked("Upper", false))
Global("VisibleConfig", nil)
Global("ColorSlider", {})
Global("ColorSliderP", {})
Global("ColorSliderPanel", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("ColorSliderPanel", false))
Global("CusCol", {r=1;g=1;b=1;a=1})
Global("previousEdit", nil)
Global("pEditName", nil)
Global("EditLine", MainPanel:GetChildChecked("EditLine", false))
Global("ConfigTextDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("Config", false):GetWidgetDesc())
Global("SideBtnDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("SideBtn", false):GetWidgetDesc())
Global("SimpleBtnDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("SimpleBtn", false):GetWidgetDesc())
Global("TFBtnDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("TFBtn", false):GetWidgetDesc())
Global("ELDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("ELBtn", false):GetWidgetDesc())
Global("ColorDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("Color", false):GetWidgetDesc())
Global("SliderDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("SliderPanel", false):GetWidgetDesc())
Global("TabledDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("TabledPanel", false):GetWidgetDesc())
Global("ContIntDesc", mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("InternalPanel", false):GetWidgetDesc())
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("Config", false):DestroyWidget()
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("SideBtn", false):DestroyWidget()
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("SimpleBtn", false):DestroyWidget()
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("TFBtn", false):DestroyWidget()
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("ELBtn", false):DestroyWidget()
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("Color", false):DestroyWidget()
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("SliderPanel", false):DestroyWidget()
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("TabledPanel", false):DestroyWidget()
mainForm:GetChildChecked("MMainPanel", false):GetChildChecked("InternalPanel", false):DestroyWidget()
-------------------------------------------------------------------------------
-- FUNCTION
-------------------------------------------------------------------------------
function InitAddonResponse(p)
	local button = mainForm:CreateWidgetByDesc(SideBtnDesc)
	button:SetName(p.sender)
	button:SetVal("name", userMods.ToWString(p.sender))
	SideContainer:PushBack(button)
end

function SideBtnPressed(r)
	if r.active then
		if VisibleConfig then
			userMods.SendEvent("CONFIG_CLOSED_"..VisibleConfig, {})
		end
		for i in ContIntPanel do ContIntPanel[i]:DestroyWidget() end
		ColorPanel:Show(false)
		EditLine:Show(false)
		previousEdit = nil
		UpperTV:SetVal("name", userMods.ToWString(r.sender))
		VisibleConfig = r.sender
		userMods.SendEvent("CONFIG_EVENT_"..(r.sender), {})
	end
end

function AddonOpenResponse(p)
	if p.sender then
		if p.sender ~= VisibleConfig then
			if VisibleConfig then
				userMods.SendEvent("CONFIG_CLOSED_"..VisibleConfig, {})
			end
			for i in ContIntPanel do ContIntPanel[i]:DestroyWidget() end
			ColorPanel:Show(false)
			EditLine:Show(false)
			previousEdit = nil
			UpperTV:SetVal("name", userMods.ToWString(p.sender))
			VisibleConfig = p.sender
			userMods.SendEvent("CONFIG_EVENT_"..(p.sender), {})
			mainForm:GetChildChecked("MMainPanel", false):Show(not mainForm:IsVisible())
		else
			if VisibleConfig then
				userMods.SendEvent("CONFIG_CLOSED_"..VisibleConfig, {})
			end
			for i in ContIntPanel do ContIntPanel[i]:DestroyWidget() end
			ColorPanel:Show(false)
			EditLine:Show(false)
			previousEdit = nil
			mainForm:GetChildChecked("MMainPanel", false):Show(false)
			VisibleConfig = nil
		end
	end
end

function ClearButtonsResponse(p)
	for i in ContIntPanel do ContIntPanel[i]:DestroyWidget() end
	ColorPanel:Show(false)
	EditLine:Show(false)
	previousEdit = nil
end

function NormAddonResponse(p)
	if mainForm:GetChildChecked("MMainPanel", false):IsVisible() == false then
		mainForm:GetChildChecked("MMainPanel", false):Show(true)
	end
	ConfigText[p.NoB] = mainForm:CreateWidgetByDesc(ConfigTextDesc)
	ConfigText[p.NoB]:SetVal("name", userMods.ToWString(p.name..": "..(p.value and p.value or "")))
	if p.btnType == "Simple" then
		ConfigButton[p.NoB] = mainForm:CreateWidgetByDesc(SimpleBtnDesc)
	elseif p.btnType == "T/F" then
		ConfigButton[p.NoB] = mainForm:CreateWidgetByDesc(TFBtnDesc)
		if tostring(p.state) == "true" then
			ConfigButton[p.NoB]:SetVariant(1)
		end
	elseif p.btnType == "EditLine" then
		ConfigButton[p.NoB] = mainForm:CreateWidgetByDesc(ELDesc)
	elseif p.btnType == "Color" then
		ConfigButton[p.NoB] = mainForm:CreateWidgetByDesc(ColorDesc)
		ConfigButton[p.NoB]:SetForegroundColor(p.color)
		ColorPanel:Show(true)
	elseif p.btnType == "Slider" then
		ConfigButton[p.NoB] = mainForm:CreateWidgetByDesc(SliderDesc)
		ConfigButton[p.NoB]:GetChildChecked("Slider", false):SetPos(p.pos or 0)
		ConfigButton[p.NoB]:GetChildChecked("Slider", false):SetStepsCount(p.steps)
	elseif p.btnType == "Tabled" then
		ConfigButton[p.NoB] = mainForm:CreateWidgetByDesc(TabledDesc)
		if p.texture then
			ConfigButton[p.NoB]:GetChildChecked("Icon", false):SetBackgroundTexture(p.texture)
			local P = ConfigButton[p.NoB]:GetChildChecked("Icon", false):GetPlacementPlain()
			P.sizeX = p.sizeX
			P.sizeY = p.sizeY
			ConfigButton[p.NoB]:GetChildChecked("Icon", false):SetPlacementPlain(P)
			local T = ConfigButton[p.NoB]:GetPlacementPlain()
			T.sizeY = p.sizeY
			T.sizeX = p.sizeX + 56
			ConfigButton[p.NoB]:SetPlacementPlain(T)
			ConfigButton[p.NoB]:GetChildChecked("Icon", false):Show(true)
		end
		if p.text then
			ConfigButton[p.NoB]:GetChildChecked("TTV", false):SetVal("name", userMods.ToWString(p.text))
			ConfigButton[p.NoB]:GetChildChecked("TTV", false):SetTextScale(p.scale)
			ConfigButton[p.NoB]:GetChildChecked("TTV", false):Show(true)
		end
	end
	ConfigButton[p.NoB]:SetName(p.name)
	ContIntPanel[p.NoB] = mainForm:CreateWidgetByDesc(ContIntDesc)
	ContIntPanel[p.NoB]:AddChild(ConfigText[p.NoB])
	ContIntPanel[p.NoB]:AddChild(ConfigButton[p.NoB])
	if p.sizeY then
		local P = ContIntPanel[p.NoB]:GetPlacementPlain()
		P.sizeY = p.sizeY
		ContIntPanel[p.NoB]:SetPlacementPlain(P)
	end
	NormalContainer:PushBack(ContIntPanel[p.NoB])
end

function SimpleBtnPressed(r)
	if r.active then
		userMods.SendEvent("CONFIG_SIMPLE_"..VisibleConfig, {["name"] = r.sender})
	end
end

function TrueFalse(r)
	if r.active then
		if r.widget:GetVariant() == 1 then
			r.widget:SetVariant(0)
		else
			r.widget:SetVariant(1)
		end
		userMods.SendEvent("CONFIG_BUTTON_"..VisibleConfig, {["name"] = r.sender, ["state"] = (r.widget:GetVariant() == 1)})
	end
end

function EditLinePressed(r)
	if r.active then
		if previousEdit then
			previousEdit:SetVariant(0)
		end
		previousEdit = r.widget
		r.widget:SetVariant(1)
		pEditName = r.sender
		EditLine:Show(true)
		EditLine:SetFocus(true)
	end
end

function EditLineEnter(r)
	local text = r.widget:GetText()
	if text then
		userMods.SendEvent("CONFIG_EDIT_LINE_"..VisibleConfig, {["name"] = pEditName, ["text"] = userMods.FromWString(text)})
	end
	r.widget:SetFocus(false)
end

function EditLineEscape(r)
	r.widget:SetFocus(false)
end

function ColorSliderChanged()
	local red = ColorSlider[0]:GetPos()
	local green = ColorSlider[1]:GetPos()
	local blue = ColorSlider[2]:GetPos()
	local alpha = ColorSlider[3]:GetPos()
	CusCol = ColorPanel:GetBackgroundColor()
	CusCol.r = red / 255
	CusCol.b = blue / 255
	CusCol.g = green / 255
	CusCol.a = alpha / 255
	ColorPanel:SetBackgroundColor(CusCol)
end

function ColorButton(r)
	if r.active then
		r.widget:SetForegroundColor(CusCol)
		userMods.SendEvent("CONFIG_COLOR_"..VisibleConfig, {["name"] = r.sender, ["color"] = CusCol})
	end
end

function SliderChanged(r)
	if r.active then
		userMods.SendEvent("CONFIG_SLIDER_"..VisibleConfig, {["name"] = r.widget:GetParent():GetName(), ["pos"] = r.widget:GetPos()})
	end
end

function TabledButton(r)
	if r.active then
		local dir
		if r.name == "TB_Forward" then
			dir = "+"
		else
			dir = "-"
		end
		userMods.SendEvent("CONFIG_TABLED_"..VisibleConfig, {["name"] = r.widget:GetParent():GetName(), ["dir"] = dir})
	end
end

function ChangeButtonResponse(p)
	if p.value then
		ConfigText[p.NoB]:SetVal("name", userMods.ToWString(p.name..": "..p.value))
	end
	if p.texture then
		ConfigButton[p.NoB]:GetChildChecked("Icon", false):SetBackgroundTexture(p.texture)
		local P = ConfigButton[p.NoB]:GetChildChecked("Icon", false):GetPlacementPlain()
		P.sizeX = p.sizeX
		ConfigButton[p.NoB]:GetChildChecked("Icon", false):SetPlacementPlain(P)
	end
	if p.text then
		ConfigButton[p.NoB]:GetChildChecked("TTV", false):SetVal("name", userMods.ToWString(p.text))
		ConfigButton[p.NoB]:GetChildChecked("TTV", false):SetTextScale(p.scale)
	end
end

function Slash(p)
	if userMods.FromWString(p.text) == "/cw" then
		if mainForm:GetChildChecked("MMainPanel", false):IsVisible() then
			if VisibleConfig then
				userMods.SendEvent("CONFIG_CLOSED_"..VisibleConfig, {})
			end
			for i in ContIntPanel do ContIntPanel[i]:DestroyWidget() end
			ColorPanel:Show(false)
			EditLine:Show(false)
			previousEdit = nil
			UpperTV:ClearValues()
			VisibleConfig = nil
		end
		mainForm:GetChildChecked("MMainPanel", false):Show(not mainForm:GetChildChecked("MMainPanel", false):IsVisible())
	end
end

function CloseButton(r)
	if VisibleConfig then
		userMods.SendEvent("CONFIG_CLOSED_"..VisibleConfig, {})
	end
	for i in ContIntPanel do ContIntPanel[i]:DestroyWidget() end
	ColorPanel:Show(false)
	EditLine:Show(false)
	previousEdit = nil
	UpperTV:ClearValues()
	VisibleConfig = nil
	mainForm:GetChildChecked("MMainPanel", false):Show(false)
end
--------------------------------------------------------------------------------
-- INITIALIZATION
--------------------------------------------------------------------------------
function Init()
	common.RegisterEventHandler( InitAddonResponse, "CONFIG_INIT_EVENT_RESPONSE" )
	common.RegisterEventHandler( AddonOpenResponse, "CONFIG_OPEN_EVENT_RESPONSE" )
	common.RegisterEventHandler( NormAddonResponse, "CONFIG_EVENT_RESPONSE" )
	common.RegisterEventHandler( ChangeButtonResponse, "CONFIG_CHANGE_BUTTON_RESPONSE" )
	common.RegisterEventHandler( ClearButtonsResponse, "CONFIG_CLEAR_BUTTONS" )
	common.RegisterEventHandler( Slash, "EVENT_UNKNOWN_SLASH_COMMAND" )
	userMods.SendEvent("CONFIG_INIT_EVENT", {})
	common.RegisterReactionHandler( SideBtnPressed, "Side_Btn_Pressed" )
	common.RegisterReactionHandler( SimpleBtnPressed, "Simple_Btn_Pressed" )
	common.RegisterReactionHandler( TrueFalse, "TF_Btn_Pressed" )
	common.RegisterReactionHandler( EditLinePressed, "EL_Btn_Pressed")
	common.RegisterReactionHandler( EditLineEnter, "EL_Enter" )
	common.RegisterReactionHandler( EditLineEscape, "EL_Escape")
	common.RegisterReactionHandler( ColorSliderChanged, "CS_Changed")
	common.RegisterReactionHandler( ColorButton, "CB_Pressed")
	common.RegisterReactionHandler( SliderChanged, "S_Changed")
	common.RegisterReactionHandler( TabledButton, "TB_Forward")
	common.RegisterReactionHandler( TabledButton, "TB_Backward")
	common.RegisterReactionHandler( CloseButton, "CloseBtn")
	
	local ColorSliderDesc = ColorSliderPanel:GetWidgetDesc()
	for i = 0, 3 do
		ColorSliderP[i] = mainForm:CreateWidgetByDesc(ColorSliderDesc)
		local P = ColorSliderP[i]:GetPlacementPlain()
		P.posX = 32 * i + P.posX
		ColorSliderP[i]:SetPlacementPlain(P)
		ColorSlider[i] = ColorSliderP[i]:GetChildChecked("ColorSlider", false)
		ColorPanel:AddChild(ColorSliderP[i])
	end
	ColorSlider[0]:SetBackgroundColor({r=1;g=0;b=0;a=.4})
	ColorSlider[1]:SetBackgroundColor({r=0;g=1;b=0;a=.4})
	ColorSlider[2]:SetBackgroundColor({r=0;g=0;b=1;a=.4})
	ColorSlider[3]:SetBackgroundColor({r=1;g=1;b=1;a=.1})
	ColorSliderPanel:DestroyWidget()
end
--------------------------------------------------------------------------------
common.RegisterEventHandler( Init, "EVENT_AVATAR_CREATED")
if avatar.IsExist() then
Init()
end
--------------------------------------------------------------------------------