if not dsc then return end

local onReact, onEvent = {}, {}

local wtBase, wtCheck, wtHelp, wtIcon
local clrFade = {r=1.5, g=1.5,b=1.5,a=1}

onEvent.EVENT_WIDGET_SHOW_CHANGED = function ( pars )
	--exObj("EVENT_WIDGET_SHOW_CHANGED", pars )
	if pars.addonName=="ContextBag" then
		if wtBase and pars.widget:GetInstanceId() == wtBase:GetInstanceId() then
			if not wtBase:IsVisible() then
				stop()
				resetCheck()
			end
		end
	end
end

onReact.mouse_left_click = function ( pars )

	local name = pars.widget:GetName()
	local IID = pars.widget:GetInstanceId()

	if false then
	elseif IID == wtCheck:GetInstanceId() then
		local b = pars.widget:GetVariant() == 1
		if pars.widget:GetVariantCount() > 1 then 
			pars.widget:SetVariant( b and 0 or 1 ) 
		end
		if not b then
			wtIcon:SetBackgroundColor({r=1, g=1,b=1,a=1})
			wtSetVal(wtHelp,L("Open one-two boxes in stack to start automaticaly open others boxes in the stack"))
			wtHelp:GetParent():Show( true )
			start()
		else
			stop()
			wtIcon:SetBackgroundColor(clrFade)
			wtHelp:GetParent():Show( false )
		end
	end
end

function resetCheck()
	wtCheck:SetVariant( 0 )
	wtHelp:GetParent():Show( false )
end
----------------------------------------------------------------
------ INIT
----------------------------------------------------------------
function GUIinit()

	local w, n, p, pBase, txt

	---ContextBag:Bag
	w = stateMainForm:GetChildUnchecked("ContextBag", false)
	if w then
		wtBase = w:GetChildUnchecked("Bag", false)
	end

	if not wtBase then ErrToChat("Bag not found!") return end

	wtBase:SetOnShowNotification( true )
	wtBase:AddChild( mainForm )

	w = WCD( dsc.Check, "autoOpener_chk", nil, { posX = 20, posY = 40}, true )
	w:SetVariant( 0 )
	wtCheck = w

	w = WCD( dsc.Bar, "icon", nil, { posX = 50, posY = 30, sizeX = 50, sizeY=50, alignX=0, alignY=0}, true )
	w:SetBackgroundTexture(common.GetAddonRelatedTexture("ChestWithTreasures"))
	w:SetBackgroundColor(clrFade)
	w:SetPriority( 400 )
	wtIcon = w

	w = WCD( dsc.Border, nil, nil, { alignX = 3, posX=40, highPosX = 30, posY = 10, sizeY=90}, false )
	w:SetPriority( 500 )
	n = WCD( dsc.Text, nil, w, { alignX = 3, alignY=3, posX=10, highPosX = 10, posY=10, highPosY = 10}, true )
	n:SetMultiline(true)
	n:SetWrapText(true)
	n:SetFormat(ToWS("<html alignx='left' aligny='middle' outline='1' fontsize='14'><r name='value'/></html>"))
	wtHelp = n


	RegisterEventHandlers( onEvent )
	RegisterReactionHandlers( onReact )
end

