autoImport("TeamPwsData")
autoImport("TeamPwsRankCell")
TeamPwsRankPopUp = class("TeamPwsRankPopUp", BaseView)

TeamPwsRankPopUp.ViewType = UIViewType.PopUpLayer

local playerTipFunc = {"SendMessage", "AddFriend", "ShowDetail", "Tutor_InviteBeStudent", "Tutor_InviteBeTutor"}
local playerTipFunc_Friend = {"SendMessage", "ShowDetail", "Tutor_InviteBeStudent", "Tutor_InviteBeTutor"}

function TeamPwsRankPopUp:Init()
	self:FindObjs()
	self:AddButtonEvt()
	self:AddViewEvt()
end

function TeamPwsRankPopUp:FindObjs()
	self.objLoading = self:FindGO("LoadingRoot")
	self.objEmptyList = self:FindGO("EmptyList")
	self.inputSearch = self:FindComponent("InputSearch", UIInput)

	self.listRanks = WrapListCtrl.new(self:FindGO("rankContainer"), TeamPwsRankCell, "TeamPwsRankCell", WrapListCtrl_Dir.Verticle)
end

function TeamPwsRankPopUp:AddButtonEvt()
	self:AddClickEvent(self:FindGO("SearchButton"), function() self:ClickButtonSearch() end)
	self:AddClickEvent(self:FindGO("CloseButton"), function() self:CloseSelf() end)
	self:AddClickEvent(self:FindGO("Mask"), function() self:CloseSelf() end)
end

function TeamPwsRankPopUp:AddViewEvt()
	self:AddListenEvt(ServiceEvent.MatchCCmdQueryTeamPwsRankMatchCCmd, self.HandleQueryTeamPwsRankMatchCCmd)

	self.listRanks:AddEventListener(MouseEvent.MouseClick, self.ClickCellHead, self);
end

function TeamPwsRankPopUp:HandleQueryTeamPwsRankMatchCCmd(note)
	self:ClearTeamPwsRankData()
	self.data = ReusableTable.CreateArray()
	local datas = note.body.rankinfo
	local rankData
	for i = 1, #datas do
		rankData = ReusableTable.CreateTable()
		self.data[#self.data + 1] = TeamPwsData.ParseRankData(rankData, datas[i])
	end
	self:UpdateData()
end

function TeamPwsRankPopUp:GetTeamPwsRankSearchResult(keyword)
	if (not self.teamPwsRankSearchResult) then
		self.teamPwsRankSearchResult = ReusableTable.CreateArray()
	end
	TableUtility.ArrayClear(self.teamPwsRankSearchResult)

	keyword = string.lower(keyword);
	for i = 1, #self.data do
		local data = self.data[i]
		if data.name and string.find(string.lower(data.name) , keyword) then
			self.teamPwsRankSearchResult[#self.teamPwsRankSearchResult + 1] = data
		end
	end
end

function TeamPwsRankPopUp:ClearTeamPwsSearchData()
	if (self.teamPwsRankSearchResult) then
		ReusableTable.DestroyAndClearArray(self.teamPwsRankSearchResult)
		self.teamPwsRankSearchResult = nil
	end
end

function TeamPwsRankPopUp:UpdateData()
	self.objLoading:SetActive(false)
	if (not self.data) then return end
	local showData = self.teamPwsRankSearchResult and self.teamPwsRankSearchResult or self.data

	self.objEmptyList:SetActive(#showData < 1)
	self.listRanks:ResetDatas(showData)
end

function TeamPwsRankPopUp:ClickButtonSearch()
	if (self.objLoading.activeSelf) then
		return
	end
	local input = self.inputSearch.value
	if (#input > 0) then
		self:GetTeamPwsRankSearchResult(input)
	else
		self:ClearTeamPwsSearchData()
	end
	self:UpdateData()
end


function TeamPwsRankPopUp:ClearTeamPwsRankData()
	if (self.data) then
		for i = 1, #self.data do
			ReusableTable.DestroyAndClearTable(self.data[i])
		end
		ReusableTable.DestroyAndClearArray(self.data)
		self.data = nil
	end
	self:ClearTeamPwsSearchData()
end

function TeamPwsRankPopUp:ClickCellHead(cellCtl)
	--[[local cellData = cellCtl.data
	if (cellCtl == self.curCell or cellCtl.charID == Game.Myself.data.id) then
		FunctionPlayerTip.Me():CloseTip()
		self.curCell = nil
		return
	end
	self.curCell = cellCtl

	local player = PlayerTipData.new()
	player:SetByBossKillerData(note.body)
	playerTip:SetData({playerData = player, funckeys = FriendProxy.Instance:IsFriend(id) and playerTipFunc_Friend or playerTipFunc})
	playerTip:AddIgnoreBound(cellCtl.headIcon.gameObject)
	playerTip.closecallback = function() self.curCell = nil end]]
end

function TeamPwsRankPopUp:OnEnter()
	self.super.OnEnter(self)

	self.objLoading:SetActive(true)
	self.objEmptyList:SetActive(false)
	ServiceMatchCCmdProxy.Instance:CallQueryTeamPwsRankMatchCCmd()
end

function TeamPwsRankPopUp:OnExit()
	self:ClearTeamPwsRankData()
	self.super.OnExit(self)
end