if not dsc then return end

local onEvent = {}

local REST, STUDY, RUN = "rest","study","run"
local MODE
local currentBoxForItemSlot
local lastSlot, lastStackCount


local function toOpenBoxes()
	--LogInfo( "toOpenBoxes slot:",currentBoxForItemSlot )
	local itemId = avatar.GetInventoryItemId( currentBoxForItemSlot )
	if itemId and itemLib.GetBoxInfo( itemId ) then
		--LogInfo("continue")
		avatar.OpenUnlockedBoxForItem( itemId )
	else
		--LogInfo("else stop&")
		--currentBoxForItemSlot = nil
		stop()
	end
end

--[[
--- слежка за изменениями в инвертаре - для обучения
onEvent.EVENT_INVENTORY_ITEM_ADDED = function  ( par )
	if currentBoxForItemSlot then return end
end
]]

--local function getSlotCount( 
local function onEVENT_INVENTORY_ITEM_CHANGED( pars )
	--exObj("pars", pars)
	local slot = pars.slot
	local itemId = pars.itemId ---avatar.GetInventoryItemId( slot )
	if not itemId or not itemLib.GetBoxInfo( itemId )
		then return end
	--- это сундук
	local name = itemLib.GetItemInfo(itemId).name
	local count = itemLib.GetStackInfo( itemId ).count
	--exObj("stack",itemLibGetStackInfo( itemId ))
	--LogInfo("onInvItemChange  slot:", slot, " count:",count,"  <--",name)
	--if not lastSlot then
		lastSlot = slot
		lastStackCount = count
	--end

end
local function onEVENT_CONTAINER_ITEM_CHANGED( pars )
	if pars.slotType ~= ITEM_CONT_INVENTORY then return end
	onEVENT_INVENTORY_ITEM_CHANGED( pars )
end

local function onEVENT_BOX_OPERATION_RESULT (pars)
	--exObj("BOX_OPERATION_RESULT", pars )
--[[
"ENUM_BoxOperationResult_SlotNumberOutOfBounds"
"ENUM_BoxOperationResult_SlotIsEmpty"
"ENUM_BoxOperationResult_NoFreeSpace"
"ENUM_BoxOperationResult_ItemIsNotBox"
"ENUM_BoxOperationResult_ItemIsNotKey"
"ENUM_BoxOperationResult_BoxLocked"
"ENUM_BoxOperationResult_BoxUnlocked"
"ENUM_BoxOperationResult_KeyDoesNotMatch"
"ENUM_BoxOperationResult_OperationSuccess"
]]
	if pars.sysResult == "ENUM_BoxOperationResult_OperationSuccess" then
		if MODE == STUDY then
			if not lastSlot then
				--- пока еще слот на записался - выход
				return
			end
			common.UnRegisterEventHandler( onEVENT_INVENTORY_ITEM_CHANGED, "EVENT_INVENTORY_ITEM_CHANGED" )
			common.UnRegisterEventHandler( onEVENT_CONTAINER_ITEM_CHANGED, "EVENT_CONTAINER_ITEM_CHANGED" )
			--LogInfo("START with lastSlot:", lastSlot, " lastStackCount:",lastStackCount)
			MODE = RUN
			currentBoxForItemSlot = lastSlot
			resetCheck()
			toOpenBoxes()
			return
		elseif MODE == RUN then
			toOpenBoxes()
			return
		else
			--LogInfo("else in MODE:",MODE," lastSlot:",lastSlot)
		end
	else
		--LogInfo(pars.sysResult)
	end
	stop()
end

function stop()
	--LogInfo("Stop")
	MODE = REST
	lastSlot = nil
	lastStackCount = nil
	currentBoxForItemSlot = nil

	resetCheck()
 	common.UnRegisterEventHandler( onEVENT_BOX_OPERATION_RESULT, "EVENT_BOX_OPERATION_RESULT" )
 	common.UnRegisterEventHandler( onEVENT_INVENTORY_ITEM_CHANGED, "EVENT_INVENTORY_ITEM_CHANGED" )
	common.UnRegisterEventHandler( onEVENT_CONTAINER_ITEM_CHANGED, "EVENT_CONTAINER_ITEM_CHANGED" )
end


--local IDS, Slots = {}, {}

function start()
	--LogToChat("START")
	MODE = STUDY
		lastSlot = nil
		lastStackCount = nil
 	common.RegisterEventHandler( onEVENT_BOX_OPERATION_RESULT, "EVENT_BOX_OPERATION_RESULT" )
 	common.RegisterEventHandler( onEVENT_INVENTORY_ITEM_CHANGED, "EVENT_INVENTORY_ITEM_CHANGED" )
	common.RegisterEventHandler( onEVENT_CONTAINER_ITEM_CHANGED, "EVENT_CONTAINER_ITEM_CHANGED" )

end

--RegisterEventHandlers( onEvent )
