AsControl = XLGetObject("As.Control")
ListCtrlSelf = nil
function OnBind(self)
	local attr = self:GetAttribute()
	attr.Control = self
	attr.top = 0
	attr.LastPos = 0
	attr.ListItems = {}
	attr.Count = 0
	attr.TopIndex = 0
	attr.Offset = 0
	attr.WhellCount = 0
	attr.IndexMap = {}
	ListCtrlSelf = self
end
function GetShortPathForTextObj(path,textobj)
	CalcTextObj:SetText(path)
 		local iTextWidth, iTextHeight = CalcTextObj:GetTextExtent()	
    	if iTextWidth > 260 then
    		local l 
    		l,_ = string.find(path,'\\')
    		local str1 = string.sub(path,1,l)
    		local str2 = string.match(path, ".+(\\.-%..+)")  -- 最后文件名
    		if str2 == nil or str1 == nil then
    			textobj:SetText(path)
    			return
    		end
    		CalcTextObj:SetText(str1.."..."..str2)
 			iTextWidth, iTextHeight = CalcTextObj:GetTextExtent()	
    		while iTextWidth < 250 do
    			l = l + 3
    			str1 = string.sub(path,1,l)
    			CalcTextObj:SetText(str1.."..."..str2)
    			iTextWidth, iTextHeight = CalcTextObj:GetTextExtent()	
    		end
    			textobj:SetText(str1.."..."..str2);
        else
        		textobj:SetText(path)
    	end
end
function OnInitControl(self)
	local attr = self:GetAttribute()
	local VirusType = self:GetControlObject("VirusType")
	local Path = self:GetControlObject("Path")
	CalcTextObj = self:GetControlObject("CalcTextObj")
	CType = self:GetControlObject("CVirusType")
	VirusType:SetText(attr.Name)
	GetShortPathForTextObj(attr.Path,Path)
	--Path:SetText(attr.Path)
	CType:SetText(attr.Type)
end

function SetSel(self,value)
	local bkg = self:GetControlObject("background")
	if not bkg then return end
	if value then
		bkg:SetResID("Res.Scanning.Item.Red")
	else
		bkg:SetResID("")	
	end
end

function List_GetSel(self)
	local attr = self:GetAttribute()
	return attr.selitem
end

function List_SetSel(self,item)
	local attr = self:GetAttribute()
	if attr.selitem then
		attr.selitem:SetSel(false)
	end
	attr.selitem = item
	item:SetSel(true)
	self:FireExtEvent("OnSelectChanged")
end

function OnLButtonDown(self)
	local list = self:GetOwnerControl()
	list:SetSel(self)
end

function InsertItem(self,Name,Path,Type)
	local content = self:GetControlObject("content")
	local cnt = content:GetChildCount()
	local objFactory = XLGetObject("Xunlei.UIEngine.ObjectFactory")
	local obj = objFactory:CreateUIObject("listitem."..cnt, "ListItem")
	local attr = obj:GetAttribute()
	attr.Name = Name
	attr.Path = Path
	attr.Type = Type
	attr.Index = cnt
	attr.Trusted = false
	attr.Selected = true
	content:AddChild(obj)
	local left,top,right,bottom = content:GetObjPos()
	obj:SetObjPos(0, cnt*50, right-left,cnt*50+50)
	
	local ctrlattr = ListCtrlSelf:GetAttribute()
	--ctrlattr.IndexMap[cnt - 1] = cnt - 1
	--SetTrueTotal(self,cnt + 1)
end

function SetAdjust(self)
	local attr = self:GetAttribute()
	local top1 = attr.top
	local content = self:GetControlObject("content")
	local left,top,right,bottom = content:GetObjPos()
	local cnt = content:GetChildCount()-1
	for i=0,cnt do
		local item = content:GetChildByIndex(i)
		item:SetObjPos(0, top1, right-left,top1+50)
		top1 = top1 + 50
	end
end

function DeleteAllItems(self)
	local content = self:GetControlObject("content")
	for i=content:GetChildCount()-1,0,-1 do
		content:RemoveChild(content:GetChildByIndex(i))
	end
end

function OnLButtonDbClick(self)

end

function OnRButtonDbClick(self)

end

function MouseWheel(self, distance)
	local content = self:GetControlObject("content")
	local left,top,right,bottom = content:GetObjPos()
	local height = bottom - top
	local attr = self:GetAttribute()
	local top1 = attr.top
	if distance > 0 then
		top1 = top1 + 50
		elseif distance < 0 then
			top1 = top1 - 50
		end
		local cnt = content:GetChildCount()
		if height - (cnt*50 + top1) < 0 then
			attr.top = top1
		else
			attr.top = height - (cnt*50 + top1) + attr.top - 50		
		end
		if attr.top > 0 then attr.top = 0 end
		self:SetAdjust()
		local vscroll = self:GetControlObject("vscroll")
		vscroll:SetScrollPos(-attr.top, true)
end

function OnMouseWheel(self, x, y, distance)
	self:GetOwnerControl():MouseWheel(distance)
end

function OnMouseWheel_Item(self, x, y, distance)
	self:GetOwnerControl():MouseWheel(distance)
end

function UpdateScroll(self)
	local content = self:GetControlObject("content")
	local left,top,right,bottom = content:GetObjPos()
	local height = bottom - top
	local vscroll = self:GetControlObject("vscroll")
	vscroll:SetPageSize(height)
	local cnt = content:GetChildCount()
	local range = cnt*50 - height
	if range < 0 then
		vscroll:SetVisible(false)
		vscroll:SetChildrenVisible(false)
		range = 0
	else
		vscroll:SetVisible(true)
		vscroll:SetChildrenVisible(true)
	end
	vscroll:SetScrollRange(0, range)
	vscroll:SetScrollPos(0, true)
end

function List_OnInitControl(self)

end

function OnVScroll(self, fun, type, newpos)
	local owner = self:GetOwnerControl()
	local attr = owner:GetAttribute()
	if type == 4 then
		attr.top = -newpos
		attr.Offset = attr.Offset + (newpos - attr.LastPos)
		attr.LastPos = newpos
		if attr.Offset > 50 * 5 then
			attr.Offset = attr.Offset - 50 * 5
			GetNextItem(attr,5)
		end
		if attr.Offset < -50 * 5 then
			attr.Offset = attr.Offset + 50 * 5 
			GetPrevItem(attr,5)
		end

		owner:SetAdjust()
	end
end

function OnPosChange(self)
	local content = self:GetControlObject("content")
	local left,top,right,bottom = content:GetObjPos()
	local height = bottom - top
	local attr = self:GetAttribute()
	local cnt = content:GetChildCount()
	if height >= cnt*50 then attr.top = 0 self:SetAdjust() end
	--self:UpdateScroll()
end

function OnScrollBarMouseWheel(self,func,x,y,distance)
	self:GetOwnerControl():MouseWheel(distance)
end

function GetNextItem(attr,count)
	
	--attr.Control:FireExtEvent("OnGetNextItem")
end
function GetPrevItem(attr,count)
	--attr.Control:FireExtEvent("OnGetPrevItem")
	--SetTrueTotal(nil,60)
end

function AddNextItem(self,Name, Path, Type, EngineName,Selected,Trusted)
	local attr = ListCtrlSelf:GetAttribute()
	--local cnt = content:GetChildCount()-1
	--attr.top = attr.top + 50 * 5
	--XLMessageBox(Name)
	--if attr.Count < 25 then
		InsertItem(ListCtrlSelf,Name,Path,Type)
		

	--else
		--attr.TopIndex = attr.TopIndex + 1
		--UpdateItems(true,Name, Path, Type, EngineName,Selected,Trusted)
	--end

	--table.insert(attr.ListItems,{Name = Name,Path = Path,Type = Type,EngineName = EngineName,Selected = true,Trusted = false}
end

function AddPrevItem(self,Name, Path, Type, EngineName,Selected,Trusted)
	--local attr = ListCtrlSelf:GetAttribute()
	--local cnt = content:GetChildCount()-1
	--attr.top = attr.top + 50 * 5

		--attr.TopIndex = attr.TopIndex - 1
	--UpdateItems(false,Name, Path, Type, EngineName,Selected,Trusted)	

		--UpdateItems(false,Name, Path, Type, EngineName,Selected,Trusted)

	--table.insert(attr.ListItems,{Name = Name,Path = Path,Type = Type,EngineName = EngineName,Selected = true,Trusted = false}
end

function UpdateItems(next,Name, Path, Type, EngineName,Selected,Trusted)

	local attr = ListCtrlSelf:GetAttribute()
	if next == true then
		local top = attr.IndexMap[0]
		for i = 0,23 do
			attr.IndexMap[i] = attr.IndexMap[i + 1]
		end
		attr.IndexMap[24] = top
		local content = ListCtrlSelf:GetControlObject("content")
		local item = content:GetChildByIndex(top)
		local ItemAttr = item:GetAttribute()
		local left,top,right,bottom = content:GetObjPos()
		item:SetObjPos(0, attr.top + 50 * 25, right-left,attr.top + 50 * 26)
		attr.top = attr.top + 50

		ItemAttr.Name = Name
		ItemAttr.Path = Path
		ItemAttr.Type = Type
		OnInitControl(item)
		XLMessageBox(Name)
	else
		local tial = attr.IndexMap[24]
		for i = 1,24 do
			attr.IndexMap[i] = attr.IndexMap[i - 1]
		end
		attr.IndexMap[0] = tial
		local content = ListCtrlSelf:GetControlObject("content")
		local item = content:GetChildByIndex(tial)
		local ItemAttr = item:GetAttribute()
		local left,top,right,bottom = content:GetObjPos()
		item:SetObjPos(0, attr.top - 50, right-left,attr.top)
		attr.top = attr.top - 50

		ItemAttr.Name = Name
		ItemAttr.Path = Path
		ItemAttr.Type = Type
		OnInitControl(item)

	end
end

function SetTrueTotal(self,value)
	local attr = ListCtrlSelf:GetAttribute()
	attr.Count = value
	local content = ListCtrlSelf:GetControlObject("content")
	local left,top,right,bottom = content:GetObjPos()
	local height = bottom - top
	local vscroll = ListCtrlSelf:GetControlObject("vscroll")
	vscroll:SetPageSize(height)
	local range = value*50 - height
	if range < 0 then
		vscroll:SetVisible(false)
		vscroll:SetChildrenVisible(false)
		range = 0
	else
		vscroll:SetVisible(true)
		vscroll:SetChildrenVisible(true)
	end
	vscroll:SetScrollRange(0, range)
	vscroll:SetScrollPos(0, true)
end

function OnAddToTrustClick(self)
	local attr = self:GetOwnerControl():GetAttribute()
	if attr.Trusted == true then
		AsControl:InformControl(10,attr.Index,0)
		attr.Trusted = false
		self:SetText("添加信任")
	else
		AsControl:InformControl(10,attr.Index,1)
		attr.Trusted = true
		self:SetText("取消信任")
	end
end