local lolBox = LibStub("LibAddon"):NewAddon("lolBox", "LibEvent", "LibTimer")
local common = LibStub("LibAddon"):GetAddon("common")

local l_bIsVisibl 				= false;
function lolBox:OnInitialize()
	xlog("@@@:Lol OnInitialize()")
--	self:print("@@@:OnInitialize()")	
	--
--	self:DisableDebug()	
	self:init_wnd();
	self:registerProtocolEvents();
	self.obj_main_wnd:hide();	
	
	--self:createButton("Stc_Ult1","12123",500,100,100,100);
	--self:addTimerShow(1,"12",100,100,0,0,1);
	
	--self:addSummner(1," ","222","rank","szDuan","szWinLose","szWinRatio","szWinPoint")
	--self:addSummner(1,"E:\\Game\\USRiot Games\\League of Legends\\RADS\\projects\\lol_air_client\\releases\\0.0.1.134\\deploy\\assets\\images\\champions\\Akali_Square_0.png","111","rank","szDuan","szWinLose","szWinRatio","szWinPoint")
--	self:addSummner(1,"E:\\Game\\USRiot Games\\League of Legends\\RADS\\projects\\lol_air_client\\releases\\0.0.1.134\\deploy\\assets\\images\\champions\\Ahri_Square_0.png","222","rank","szDuan","szWinLose","szWinRatio","szWinPoint")
end

function lolBox:registerProtocolEvents()
	self:print("registerProtocolEvents")
	self.root:subscribeEvent("KeyDown", self.on_Key_Down, self);
	self.root:subscribeEvent("KeyUp", self.on_Key_Up, self);
	
	self:RegisterEvent("YYT_TO_PIP_AddSummonerInfo", "addSummner");
	self:RegisterEvent("YYT_TO_PIP_AddMonsterTime", "addTimerShow");
	--self:RegisterEvent("WM_PRODUCT_COMMIT_IDX", "on_commit_idx")
	--self:RegisterEvent("WM_PRODUCT_COMMIT_SVRID", "on_commit_svrid")
	--self:RegisterEvent("WM_SHOWINTERNALYY", "on_show_internalyy")
	--self:RegisterEvent("WM_SHOW_TEAMVOICE_UI", "on_show_teamvoice")
end

function lolBox:FixPosion()
	local width  	= self.root:getParentPixelWidth();
	local mainWndWid 	= self.obj_main_wnd:getPixelSize();
	self.obj_main_wnd:setPosition(CEGUI.UVector2(CEGUI.UDim(0, (width-mainWndWid.width)/2),CEGUI.UDim(0.2, 0)))
-- 	xlog(string.format("@@@:%d",mainWndWid.width))
 	--xlog("@@@:%d",Size:width());
end

function lolBox:on_Key_Up(args)
	local window_args = CEGUI.toKeyEventArgs(args)	

	if window_args.scancode == 0x29 and l_bIsVisibl then 
		self.obj_main_wnd:hide();
	else				
		window_args.handled  = -1;
	end		
	return 0;
end

function lolBox:on_Key_Down(args)
	local window_args = CEGUI.toKeyEventArgs(args)	

	if window_args.scancode == 0x29 and l_bIsVisibl then 
		self.obj_main_wnd:show();
	else			
		window_args.handled  = -1;
	end	
	--xlog("@@@:MaxPos:%x",window_args.scancode);	
	return 0;
end
function lolBox:init_wnd()
	--xlog("@@@: addItem11");
	self.wnd_mgr = CEGUI.WindowManager:getSingleton();
	self.root = CEGUI.System:getSingleton():getGUISheet();
	self.renderer = CEGUI.System:getSingleton():getRenderer()
	self.root:subscribeEvent("Sized", self.on_root_sized, self);
	
	self.img_mgr = CEGUI.ImagesetManager:getSingleton();
	self.img_set = self.img_mgr:get("smartYYUI")
	self.wnd_BotLan 			= self.wnd_mgr:getWindow("Txt_LanCool")
	--self.wnd_BotLan:show();
	self.obj_main_wnd 			= self.wnd_mgr:getWindow("SummonerMainWnd")
	self.obj_main_wnd 			= CEGUI.toFrameWindow(self.obj_main_wnd)
	
	self.lst_Name 					= CEGUI.toListbox(self.wnd_mgr:getWindow("LstName"));	
	
	self.lst_Rank 					= CEGUI.toListbox(self.wnd_mgr:getWindow("LstRank"));	
	self.lst_DuanWei				= CEGUI.toListbox(self.wnd_mgr:getWindow("LstDuanWei"));	
	self.lst_WinLose 				= CEGUI.toListbox(self.wnd_mgr:getWindow("LstWinLose"));	
	self.lst_WinRatio 			= CEGUI.toListbox(self.wnd_mgr:getWindow("LstWinRatio"));	
	self.lst_WinPoint 			= CEGUI.toListbox(self.wnd_mgr:getWindow("LstWinPoint"));	
	
	self:FixPosion();
end

function lolBox:createButton( name, text , x, y, w, h)
--	xlog(("@@@:createButton name:%s, id:%s, text:%s, sid:%s, x:%d, y:%d, w:%d, h:%d, bMenu:%s, father:%s"):
--		format(name, tostring(id), text, sid, x, y, w, h, tostring(bMenu), tostring(father) ))
	--xlog("@@@:%s",name);
	local wnd = CEGUI.WindowManager:getSingleton():createWindow( "smartYY/StaticText", name )
	
	wnd:setSize(CEGUI.UVector2( CEGUI.UDim(0, w), CEGUI.UDim(0, h) ))  
	wnd:setPosition(CEGUI.UVector2( CEGUI.UDim(0, x), CEGUI.UDim(0, y) )) 	

	wnd:setText(text)

	wnd:setProperty( "TextColours", "tl:FF00FF00 tr:FF00FF00 bl:FF00FF00 br:FF00FF00" )
	wnd:setProperty( "FrameEnabled", "False" )
	wnd:setProperty( "BackgroundEnabled", "False" )
	wnd:setProperty( "TextParsingEnabled", "False" )
	wnd:setProperty( "MousePassThroughEnabled", "True" )
	wnd:setProperty( "Visible", "True" )

	local RootWnd 		= self.wnd_mgr:getWindow("Summoner_root_frm")	
	RootWnd:addChildWindow(wnd);
	--xlog("@@@:222")

	return wnd
end

function lolBox:addTimerShow(event,...)	
	local szTxt,pX,pY,nWid,nHegit,nTxtId = ...;
	local szStcName 				= string.format("Txt_Timer%d",nTxtId);
	local ceTxt 						= self.wnd_mgr:getWindow(szStcName);	
	if ceTxt == nil then
		ceTxt 								= self:createButton(szStcName,szTxt,0,0,40,20);
	end	
	--xlog("@@@:Lol addTimerShow()")
	
	if pX == 0x0 and pY == 0x0 then 
		ceTxt:setProperty( "Visible", "False" )
	else				
		ceTxt:setProperty( "Visible", "True" )
	end	
	
	--ceTxt:setSize(CEGUI.UVector2( CEGUI.UDim(0, 9), CEGUI.UDim(0, 12) ))  
	ceTxt:setPosition(CEGUI.UVector2( CEGUI.UDim(0, pX), CEGUI.UDim(0, pY) ))
	ceTxt:setText(szTxt);
end

function lolBox:addSummner(event,...)	
	l_bIsVisibl 			= true;
	local szIconPath,name,rank,szDuan,szWinLose,szWinRatio,szWinPoint = ...;
	local item = CEGUI.createListboxExtraItem( name, 0 )

	item:setLeftStep(1)
	item:setTextParsingEnabled(false)
	item:setTextColours(CEGUI.colour( 67/255, 137/255, 209/255, 1 ))
	item:setMouseOnColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setSelectionColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setMouseOnBrushImage("smartYYUI", "ListboxMouseonBrush")
	item:setSelectionBrushImage( "smartYYUI", "ListboxSelectionBrush")
	item:setHeightStep(1)
	item:setSingleLine(true)
	--	xlog("@@@:NRet:%s",szIconPath);
	local imageset_name 		= name;		
	local texture = self.renderer:createTexture();

	if szIconPath ~= " " then
			local nRet 		= texture:loadFromFile(szIconPath, "")

	local imageset;
	
	if self.img_mgr:isDefined(imageset_name)	then
		imageset 					= self.img_mgr:get(imageset_name);		
	else	
		imageset = self.img_mgr:create(imageset_name, texture, CEGUI.XREA_REPLACE)
	end

	if nRet then
		local texPixel = texture:getSize()	
		if not imageset:isImageDefined(name)  then
			imageset:defineImage( name, CEGUI.Rect(0, 0, texPixel.width, texPixel.height),  CEGUI.Vector2(0, 0))			
		end			
		item:addIcon(imageset:getImage( name ), 20, 20)
	else
		self.renderer:destroyTexture(texture)
	end
	end
	

--	xlog("@@@:NRet:%d",nRet);
	
	--item:setAutoWidthShow(true)
--	local image = nil
--		image = self.img_set:getImage( "RadioButtonNormal" )
--		item:addIcon( image, image:getWidth(), image:getHeight() )
--	self.lst_SummerInfo:addItem(item);
--	self.lst_SummerInfo:addItem(item);
	self.lst_Name:addItem(item);
	
	
	item 				= CEGUI.createListboxExtraItem( rank, 0 )
	item:setLeftStep(1)
	item:setTextParsingEnabled(false)
	item:setTextColours(CEGUI.colour( 67/255, 137/255, 209/255, 1 ))
	item:setMouseOnColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setSelectionColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setMouseOnBrushImage("smartYYUI", "ListboxMouseonBrush")
	item:setSelectionBrushImage( "smartYYUI", "ListboxSelectionBrush")
	item:setHeightStep(1)
	item:setSingleLine(true)
	self.lst_Rank:addItem(item);
	
	item 				= CEGUI.createListboxExtraItem( szDuan, 0 )
	item:setLeftStep(1)
	item:setTextParsingEnabled(false)
	item:setTextColours(CEGUI.colour( 67/255, 137/255, 209/255, 1 ))
	item:setMouseOnColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setSelectionColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setMouseOnBrushImage("smartYYUI", "ListboxMouseonBrush")
	item:setSelectionBrushImage( "smartYYUI", "ListboxSelectionBrush")
	item:setHeightStep(1)
	item:setSingleLine(true)
	self.lst_DuanWei:addItem(item);
	
	item 				= CEGUI.createListboxExtraItem( szWinLose, 0 )
	item:setLeftStep(1)
	item:setTextParsingEnabled(false)
	item:setTextColours(CEGUI.colour( 67/255, 137/255, 209/255, 1 ))
	item:setMouseOnColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setSelectionColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setMouseOnBrushImage("smartYYUI", "ListboxMouseonBrush")
	item:setSelectionBrushImage( "smartYYUI", "ListboxSelectionBrush")
	item:setHeightStep(1)
	item:setSingleLine(true)
	self.lst_WinLose:addItem(item);
	
	item 				= CEGUI.createListboxExtraItem( szWinRatio, 0 )
	item:setLeftStep(1)
	item:setTextParsingEnabled(false)
	item:setTextColours(CEGUI.colour( 67/255, 137/255, 209/255, 1 ))
	item:setMouseOnColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setSelectionColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setMouseOnBrushImage("smartYYUI", "ListboxMouseonBrush")
	item:setSelectionBrushImage( "smartYYUI", "ListboxSelectionBrush")
	item:setHeightStep(1)
	item:setSingleLine(true)
	self.lst_WinRatio:addItem(item);
	
	item 				= CEGUI.createListboxExtraItem( szWinPoint, 0 )
	item:setLeftStep(1)
	item:setTextParsingEnabled(false)
	item:setTextColours(CEGUI.colour( 67/255, 137/255, 209/255, 1 ))
	item:setMouseOnColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setSelectionColours(CEGUI.colour( 255/255, 255/255, 255/255, 1 ))
	item:setMouseOnBrushImage("smartYYUI", "ListboxMouseonBrush")
	item:setSelectionBrushImage( "smartYYUI", "ListboxSelectionBrush")
	item:setHeightStep(1)
	item:setSingleLine(true)
	self.lst_WinPoint:addItem(item);
	--xlog("@@@: addItem22");
end



function lolBox:on_root_sized(args)
	self:debug("root:on_sized")
	-- 窗口大小改变时，窗口保持在游戏窗口内
	if self.obj_main_wnd ~= nil and
		self.obj_main_wnd:isVisible() then
		self:clamped_to_screen()
	end
end