autoImport("ServantRecommendItemData")

ServantRecommendProxy = class('ServantRecommendProxy', pm.Proxy)
ServantRecommendProxy.Instance = nil;
ServantRecommendProxy.NAME = "ServantRecommendProxy"

ServantRecommendProxy.STATUS=
{
	GO = SceneUser2_pb.ERECOMMEND_STATUS_GO,			-- 可出發
	RECEIVE = SceneUser2_pb.ERECOMMEND_STATUS_RECEIVE, 	-- 可領獎
	FINISHED = SceneUser2_pb.ERECOMMEND_STATUS_FINISH, 	-- 已完成
}

function ServantRecommendProxy:ctor(proxyName, data)
	self.proxyName = proxyName or ServantRecommendProxy.NAME
	if(ServantRecommendProxy.Instance == nil) then
		ServantRecommendProxy.Instance = self
	end
	if data ~= nil then
		self:setData(data)
	end
	self:Init()
end

function ServantRecommendProxy:Init()
	self.rewardStatusMap = {}
	self.classifiedData = {}
	self.servantImproveDataMap = {}
	-- self.servantImproveDataList = {}
	self.servantImproveUnlockFunctionList = {}
	self.servantImproveUnlockMap = {}
end

function ServantRecommendProxy:HandleRecommendData(data)
	if(nil==self.recommendMap)then
		self.recommendMap={}
	end
	TableUtility.TableClear(self.classifiedData)
	local finished = SceneUser2_pb.ERECOMMEND_STATUS_FINISH
	for i=1,#data do
		local cell_data = ServantRecommendItemData.new(data[i])
		if(nil~=cell_data.staticData)then
			local needDel = cell_data.staticData.NeedDel
			if(cell_data.status == finished and needDel and needDel==1)then
				self.recommendMap[cell_data.id] = nil
			else
				self.recommendMap[cell_data.id] = cell_data
			end
		else
			redlog("女僕---> 伺服器發的ID未在recommend表中找到，錯誤id： ",cell_data.id)
		end
	end
	self.classifiedData[0]={}
	for _,data in pairs(self.recommendMap) do
		TableUtility.ArrayPushBack(self.classifiedData[0],data)
	end
	for id,data in pairs(self.recommendMap) do
		local pageType = data.staticData and data.staticData.PageType
		if(pageType)then
			for i=1,#pageType do
				local singleType = pageType[i]
				if(nil==self.classifiedData[singleType])then
					self.classifiedData[singleType]={}
				end
				TableUtility.ArrayPushBack(self.classifiedData[singleType],data)
			end
		end
	end
end

function ServantRecommendProxy:HandleRewardStatus(data)
	for i=1,#data do
		local serviceItem = data[i]
		-- helplog("ServantRecommendProxy HandleRewardStatus serviceItem.favorability: ",serviceItem.favorability,"status: ",serviceItem.status)
		self.rewardStatusMap[serviceItem.favorability]=serviceItem.status
	end
end

function ServantRecommendProxy:GetRecommendMap()
	return self.recommendMap
end

function ServantRecommendProxy:GetRecommendById(id)
	return self.recommendMap[id]
end

function ServantRecommendProxy:GetRewardStatusMap()
	return self.rewardStatusMap
end

-- 0 不可領取 1 可領取 2 已領取
local rewardCfg = GameConfig.Servant.reward
function ServantRecommendProxy:GetFavorRewardID()
	for i=1,#rewardCfg do
		local cell = rewardCfg[i]
		if(cell and cell.value)then
			if(self.rewardStatusMap[cell.value]==1)then
				return cell.rewardid
			end
		end
	end
	return nil
end

function ServantRecommendProxy:GetRecommendDataByType(t,sort)
	local typeData = self.classifiedData[t]
	if(typeData)then
		if(sort)then
			table.sort(typeData,function (l,r)
				return self:_sortData(l,r)
			end)
		end
	end
	return typeData
end

function ServantRecommendProxy:_sortData(left,right)
	if left == nil or right == nil then 
	   return false 
	end
	local lReceive = left.status == ServantRecommendProxy.STATUS.RECEIVE
	local rReceive = right.status == ServantRecommendProxy.STATUS.RECEIVE

	local lFinished = left.status == ServantRecommendProxy.STATUS.FINISHED
	local rFinished = right.status == ServantRecommendProxy.STATUS.FINISHED

	local lGo = left.status == ServantRecommendProxy.STATUS.GO
	local rGo = right.status == ServantRecommendProxy.STATUS.GO

	local lData = left.staticData
	local rData = right.staticData
	local sameRecycle = lData.Recycle == rData.Recycle

	local lOpen = left:isActiveOpen()
	local rOpen = right:isActiveOpen()

	if(lOpen and rOpen)then
		return lData.id<rData.id
	end
	if(lOpen or rOpen)then
		return lOpen==true
	end
	
	if(lReceive and rReceive)then
		if(sameRecycle)then
			return  lData.id<rData.id
		else
			return lData.Recycle<rData.Recycle
		end 
	end
	if(lReceive or rReceive)then
		return lReceive ==true
	end
	
	if(lGo and rGo)then
		if(sameRecycle)then
			return lData.id<rData.id
		else
			return lData.Recycle<rData.Recycle
		end
	end
	if(lGo or rGo)then
		return lGo==true
	end
	return lData.id<rData.id
end

--女僕提升
function ServantRecommendProxy:GetImproveGroupList()
	local groupList = {}
	for k,v in pairs(self.servantImproveDataMap) do
		groupList[#groupList + 1] = v
	end
	return groupList
end

function ServantRecommendProxy:GetImproveGroup( groupId )
	return self.servantImproveDataMap[groupId]
end

function ServantRecommendProxy:GetImproveFunctionList()
	return self.servantImproveUnlockFunctionList
end

function ServantRecommendProxy:HandleServantImproveData(severdata)
	-- helplog("==HandleServantImproveData==>>>")
	-- TableUtil.Print222(severdata)
	local isGroupChanged = false
	local isProgressChanged = false
	if severdata.datas and #severdata.datas > 0 then
		-- helplog("==HandleServantImproveData.datas==>>>", #severdata.datas)
		for i=1, #severdata.datas do
			local groupdata = severdata.datas[i]
			local newGroupId
			local groupItems = groupdata.items
			if groupItems and #groupItems > 0 then
				-- helplog("==SetGroupData:Items==>>>", groupItems[1].dwid)
				newGroupId = math.floor(groupItems[1].dwid/1000)
				isGroupChanged = true
			end

			local valueitems = groupdata.valueitems
			if valueitems then
				-- helplog("==SetGroupData:groupid==>>>", valueitems.groupid, valueitems.growth)
				if valueitems.groupid and valueitems.groupid ~= 0 then
					newGroupId = valueitems.groupid
				end
				if valueitems.growth and valueitems.growth ~= 0 then
					isProgressChanged = true
				end
			end

			-- helplog("==HandleServantImproveData.newGroupId==>>>", newGroupId)
			if newGroupId then
				local newGroupType = Table_ServantImproveGroup[newGroupId].type
				local sameTypeGroup
				for k,v in pairs(self.servantImproveDataMap) do
					local oldGroupType = Table_ServantImproveGroup[k].type
					if oldGroupType == newGroupType then
						sameTypeGroup = v
						break;
					end
				end
				if sameTypeGroup then
					local oldGroupId = sameTypeGroup.groupid
					-- helplog("==sameTypeGroup==>>>", oldGroupId, newGroupId)
					if newGroupId == oldGroupId then
						-- helplog("==sameGroup==>>>")
						self.servantImproveDataMap[newGroupId]:updata( newGroupId, groupItems, valueitems )
					else
						TableUtility.TableClear(sameTypeGroup)
						self.servantImproveDataMap[oldGroupId] = nil

						local newGroup = ServantImproveData.new( newGroupId, groupItems, valueitems )
						self.servantImproveDataMap[newGroupId] = newGroup
					end
				else
					-- helplog("==newGroup==>>>")
					local newGroup = ServantImproveData.new( newGroupId, groupItems, valueitems )
					self.servantImproveDataMap[newGroupId] = newGroup
				end
			end
		end
	end

	if isGroupChanged then
		GameFacade.Instance:sendNotification(ServantImproveEvent.ItemListUpdate, groupdata)
	end

	if isProgressChanged then
		-- helplog("==GiftProgressUpdate==>>>")
		GameFacade.Instance:sendNotification(ServantImproveEvent.GiftProgressUpdate, groupdata)
	end

	local unlockitems = severdata.unlockitems
	-- helplog("==HandleServantImproveData.unlockitems==>>>", #unlockitems)
	if unlockitems and #unlockitems > 0 then
		for i=1,#unlockitems do
			local oldItem = self.servantImproveUnlockMap[unlockitems[i]]
			if oldItem == nil then
				self.servantImproveUnlockMap[unlockitems[i]] = unlockitems[i]
				self.servantImproveUnlockFunctionList[#self.servantImproveUnlockFunctionList + 1] = unlockitems[i]
			end
		end
		GameFacade.Instance:sendNotification(ServantImproveEvent.FunctionListUpdate, severdata)
	end	
end

ServantImproveData = class("ServantImproveData")
function ServantImproveData:ctor( newGroupId, itemsData, valueData )
	self:updata( newGroupId, itemsData, valueData )
end

function ServantImproveData:updata( newGroupId, itemsData, valueData )
	-- helplog("==ServantImproveData:updata==>>>", newGroupId)
	self.groupid = newGroupId
	if itemsData then
		-- helplog("==itemsData==>>>", #itemsData)
		if not self.itemList then
			self.itemList = {}
			self.finishList = {}
			self.itemMap = {}
			self.finishMap = {}
		end 
		
		for i=1,#itemsData do
			local newItemInfo = itemsData[i]
			local oldItem = self.itemMap[newItemInfo.dwid]
			if oldItem then
				local isDelete = Table_Growth[oldItem.dwid].NeedDel or 0
				if isDelete == 1 and newItemInfo.status == SceneUser2_pb.EGROWTH_STATUS_FINISH then
					local removeIndex
					local itemListRef = self.itemList
					for j=1, #itemListRef do
						if itemListRef[j] == oldItem then
							removeIndex = j
						end
					end

					if removeIndex then
						local removeItem = self.itemList[removeIndex]

						if self.finishMap[removeItem.dwid] == nil then
							self.finishMap[removeItem.dwid] = removeItem
							self.finishList[#self.finishList + 1] = removeItem
						end
						-- self.finishList[#self.finishList + 1] = self.itemList[removeIndex]
						table.remove(self.itemList, removeIndex)
						self.itemMap[oldItem.dwid] = nil
					end
				else
					oldItem:updata(newItemInfo)
				end
			else
				local isDelete = Table_Growth[newItemInfo.dwid].NeedDel or 0
				if isDelete == 1 and newItemInfo.status == SceneUser2_pb.EGROWTH_STATUS_FINISH then
					local newItem = GrowthItemData.new(newItemInfo)
					if self.finishMap[newItem.dwid] == nil then
						self.finishMap[newItem.dwid] = newItem
						self.finishList[#self.finishList + 1] = newItem
					end
				else
					local newItem = GrowthItemData.new(newItemInfo)
					self.itemList[#self.itemList + 1] = newItem
					self.itemMap[newItemInfo.dwid] = newItem
				end
			end
		end
	end

	if valueData then
		-- helplog("==valueData==>>>", valueData.groupid, valueData.growth, #valueData.everreward)
		if valueData.growth and valueData.growth ~= 0 then
			self.growth = valueData.growth
		end

		local everRewardList = valueData.everreward
		if everRewardList and #everRewardList > 0 then
			if not self.everReward then
				self.everReward = {}
			end
			for i=1,#everRewardList do
				self.everReward[#self.everReward + 1] = everRewardList[i]
			end
		end
	end
end

GrowthItemData = class("GrowthItemData")
function GrowthItemData:ctor(data)
	self:updata(data)
end

function GrowthItemData:updata(data)
	self.dwid = data.dwid
	self.finishtimes = data.finishtimes
	self.status = data.status
end

