local SummonResultUI = class("SummonResultUI", BaseUI)

local CELL_HEIGHT = 230
local CELL_HEIGHT_1 = 120
local NOT_OWN_NODE_HEIGHT = 56
local DIFF_HEIGHT = 20
local ForceCell = "app/ui/summon/cell/summon_force_cell"

function SummonResultUI:isFullScreen()
	return false
end

function SummonResultUI:ctor(params)
	self.rewards = params or {}
	self.forceList = {}
	self.rewardList = {}
end

function SummonResultUI:getPrefabPath()
	return "assets/prefabs/ui/summon/summon_result_ui.prefab"
end

function SummonResultUI:onClose()
	AudioManager:resumeMusic()
end

function SummonResultUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.root:addClickListener(function()
		self:closeUI()
	end)

	uiMap["summon_result_ui.title_bg.title_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_FORCE_21))
	uiMap["summon_result_ui.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_FORCE_24, #self.rewards))
	uiMap["summon_result_ui.force_bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_FORCE_22))
	uiMap["summon_result_ui.reward_bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_FORCE_23))
	uiMap["summon_result_ui.continue_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_FORCE_25))

	self.forceScrollrectObject = uiMap["summon_result_ui.force_scrollrect"]
	self.forceScrollrect = self.forceScrollrectObject:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.rewardBgrewardScrollrectObject = uiMap["summon_result_ui.reward_scrollrect"]
	self.rewardBgrewardScrollrect = self.rewardBgrewardScrollrectObject:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
end

function SummonResultUI:initData()
	local map = {}
	local rewardMap = {}
	for i,v in ipairs(self.rewards) do
		local cfg = ConfigManager:getConfig("item")[v.item.id]
		if cfg.type == 4 then
			if not map[v.item.id] then
				map[v.item.id] = {}
			end
			map[v.item.id].count = (map[v.item.id].count or 0) + v.item.count
			map[v.item.id].sort = cfg.qlt * 10000000000 + map[v.item.id].count * 10000000 + v.item.id
		else
			if not rewardMap[v.item.id] then
				rewardMap[v.item.id] = {}
			end
			rewardMap[v.item.id].count = (rewardMap[v.item.id].count or 0) + v.item.count
			rewardMap[v.item.id].sort = cfg.qlt * 10000000000 + v.item.id
			rewardMap[v.item.id].type = v.type
		end
	end
	for k, v in pairs(map) do
		table.insert(self.forceList, {id = k, count = v.count, sort = v.sort})
	end
	for k, v in pairs(rewardMap) do
		table.insert(self.rewardList, {item = {id = k, count = v.count}, sort = v.sort, type = v.type})
	end
	table.sort(self.forceList, function (a, b)
		return a.sort > b.sort
	end)
	table.sort(self.rewardList, function (a, b)
		return a.sort > b.sort
	end)

	self.unlockList = DataManager.SummonData:getSummonUnlock()
	self.unlockCount = DataManager.SummonData:getSummonUnlockItemCount()
end

function SummonResultUI:onRefresh()
	self:initData()
	self:_refreshForceScrollrect()
	self:_refreshRewardScrollrect()
	self:checkUnlockMap()
end

function SummonResultUI:_refreshForceScrollrect()
	self.forceScrollrect:addInitCallback(function()
		return ForceCell
	end)
	self.forceScrollrect:addRefreshCallback(function(index, cell)
		cell:refresh(self.forceList[index])
	end)
	self.forceScrollrect:clearCells()
	self.forceScrollrect:refillCells(#self.forceList)
end

function SummonResultUI:_refreshRewardScrollrect()
	self.rewardBgrewardScrollrect:addInitCallback(function()
		return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
	end)
	self.rewardBgrewardScrollrect:addRefreshCallback(function(index, cell)
		cell:refresh(self.rewardList[index])
	end)
	self.rewardBgrewardScrollrect:clearCells()
	self.rewardBgrewardScrollrect:refillCells(#self.rewardList)
end

function SummonResultUI:checkUnlockMap()
	if #self.unlockList <= 0 and self.unlockCount <= 0 then
		return
	end
	if #self.unlockList > 0 then
		ModuleManager.ForceManager:showForceUnlockUI(self.unlockList, self.unlockCount)
	else
		ModuleManager.ForceManager:showForceConvertUI(self.unlockCount)
	end
end

return SummonResultUI