--[[
ģƣUIڹ
ģ鹦ܣڵɾˢµ
ģ޸ʱ䣺2017.07.26
]]
local base = _G
local sys = require"sys"
local table = require"table"
local print,assert,type,ipairs = base.print,base.assert,base.type,base.ipairs
module(...)

--ڹջ
local stack = {}
--ǰĴID
local winid = 0

local function allocid()
	winid = winid + 1
	return winid
end

local function losefocus()
	if stack[#stack] and stack[#stack]["onlosefocus"] then
		stack[#stack]["onlosefocus"]()
	end	
end

--[[
add
  һ
  
		wndڵԪԼϢ
ֵID
]]
function add(wnd)
	---ע½ӿ
	assert(wnd.onupdate)
	if type(wnd) ~= "table" then
		assert("unknown uiwin type "..type(wnd))
	end
	--һִʧȥĴ
	losefocus()
	--Ϊ´ڷ䴰ID
	wnd.id = allocid()
	--´ջ
	sys.dispatch("UIWND_ADD",wnd)
	return wnd.id
end

--[[
remove
  Ƴһ
  
		winidID
ֵ
]]
function remove(winid)
	sys.dispatch("UIWND_REMOVE",winid)
end

local function onadd(wnd)
	table.insert(stack,wnd)
	stack[#stack].onupdate()
end

local function onremove(winid)
	local istop,k,v
	for k,v in ipairs(stack) do
		if v.id == winid then
			istop = (k==#stack)
			table.remove(stack,k)
			if #stack~=0 and istop then
				stack[#stack].onupdate()
			end
			return
		end
	end
end

local function onupdate()
	stack[#stack].onupdate()
end

--[[
isactive
  жһǷǰʾ
  
		winidID
ֵtrueʾǰʾʾǰʾ
]]
function isactive(winid)
	return stack[#stack].id==winid
end

 sys.regapp({
 	UIWND_ADD = onadd,
 	UIWND_REMOVE = onremove,
 	UIWND_UPDATE = onupdate,
 	UIWND_TOUCH = onTouch,
 	UIWND_KEY = onKey,
 })
