local SummonShopUI = class("SummonShopUI", BaseUI)

function SummonShopUI:getPrefabPath()
	return "assets/prefabs/ui/summon/summon_shop_ui.prefab"
end

function SummonShopUI:getCurrencyParams()
	local params = {}
	params.showType = GConst.CURRENCY_TYPE.HORIZONTAL
	params.itemIds = {
		self.shopItemId,
	}
	return params
end

function SummonShopUI:ctor(param)
	self.shopType = param.shopType
	self.titleTx = GConst.EMPTY_STRING
	self.descTx = GConst.EMPTY_STRING
	self.descTitleHero = GConst.EMPTY_STRING
	self.descDown = GConst.EMPTY_STRING
	self.descTx = GConst.EMPTY_STRING
	self.shopItemId = nil
	if self.shopType == 1 then
		self.titleTx = I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESCR_1)
		self.descTx = I18N:getGlobalText(I18N.GlobalConst.SUMMON_HELP_1)
		self.descTitleHero = I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESCR_3)
		self.descDown = I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESCR_6)
		self.shopItemId = GConst.ItemConst.ITEM_ID_SUMMON_SHOP_GOLD_COMMON
	elseif self.shopType == 2 then
		self.titleTx = I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESCR_2)
		self.descTx = I18N:getGlobalText(I18N.GlobalConst.SUMMON_HELP_2)
		self.descTitleHero = I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESCR_3)
		self.descDown = I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESCR_6)
		self.shopItemId = GConst.ItemConst.SUMMON_SHOP_GOLD_ACT
	elseif self.shopType == 3 then
		self.titleTx = I18N:getGlobalText(I18N.GlobalConst.CORE_SUMMON_DESC_21)
		self.descTx = I18N:getGlobalText(I18N.GlobalConst.CORE_SUMMON_DESC_24)
		self.descTitleHero = I18N:getGlobalText(I18N.GlobalConst.CORE_SUMMON_DESC_22)
		self.descDown = I18N:getGlobalText(I18N.GlobalConst.CORE_SUMMON_DESC_25)
		self.shopItemId = GConst.ItemConst.SOUL_SHOP_GOLD
	elseif self.shopType == 4 then
		self.titleTx = I18N:getGlobalText(I18N.GlobalConst.MERCENARY_DESC_18)
		self.descTx = I18N:getGlobalText(I18N.GlobalConst.MERCENARY_DESC_19)
		self.descTitleHero = I18N:getGlobalText(I18N.GlobalConst.MERCENARY_DESC_21)
		self.descDown = I18N:getGlobalText(I18N.GlobalConst.MERCENARY_DESC_20)
		self.shopItemId = GConst.ItemConst.ITEM_ID_MERCENARY_EXCHANGE
	end
	ModuleManager.SummonManager:reqSummonShopMarkUnlockTip(self.shopType)
end

function SummonShopUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	self.btnMaskClose = uiMap["summon_shop_ui.btn_mask_close"]
	self.content = uiMap["summon_shop_ui.content"]
	self.txTitle = uiMap["summon_shop_ui.content.tx_title"]
	self.btnClose = uiMap["summon_shop_ui.content.btn_close"]
	self.scrollrect = uiMap["summon_shop_ui.content.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
	self.txDescTips = uiMap["summon_shop_ui.content.tx_desc_tips"]
	self.titleHero = uiMap["summon_shop_ui.content.scrollrect.title_hero"]
	self.txDescTitleHero = uiMap["summon_shop_ui.content.scrollrect.title_hero.tx_desc_title_hero"]
	self.btnTips = uiMap["summon_shop_ui.content.tx_title.btn_tips"]
	self.itemIcon = uiMap["summon_shop_ui.content.tx_desc_tips.item_icon"]
	self:_addListeners()
end

function SummonShopUI:_addListeners()
	self.btnTips:addClickListener(function()
		local params = {}
		params.desc = self.descTx
		ModuleManager.TipsManager:showHelpTips(params)
	end)
	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
	self.btnMaskClose:addClickListener(function()
		self:closeUI()
	end)
	self:bind(DataManager.SummonData, "isDirty", function()
		self:onRefresh()
	end)
	self:bind(DataManager.BagData.ItemData, "dirty", function()
		self:onRefresh()
	end)
end

function SummonShopUI:isFullScreen()
	return false
end

function SummonShopUI:setParentUI(parentUI)
	self.parentUI = parentUI
end

function SummonShopUI:onRefresh()
	self.txTitle:setText(self.titleTx)
	local width = self.txTitle:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
	width = math.min(400, width)
	self.txTitle:setSizeDeltaX(width)
	self.txDescTitleHero:setText(self.descTitleHero)
	self.txDescTips:setText(self.descDown)

	local width = self.txDescTips:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
	local sizeW = self.txDescTips:getSizeDeltaX()
	if width < sizeW then
		self.itemIcon:setAnchoredPositionX((width - sizeW)/2)
	else
		self.itemIcon:setAnchoredPositionX(0)
	end
	self.itemIcon:setSprite(ModuleManager.ItemManager:getItemIcon(self.shopItemId))
	self:_refreshScrollrect()
end

--刷新滑动列表
function SummonShopUI:_refreshScrollrect()
	if self.shopType == 3 then
		self.scrollrect:setCellWidth(146)
		self.scrollrect:setCellHeight(210)
	else
		self.scrollrect:setCellWidth(144)
		self.scrollrect:setCellHeight(200)
	end
	local list = DataManager.SummonData:getSummonShopList(self.shopType)
	self.shopList = {}
	for i = #list, 1, -1 do  -- 从后往前遍历
		local v = list[i]
		local itemId = GFunc.getRewardId(v.hero)
		local isRemove = false
		if v.force_id ~= nil then
			if not DataManager.HeroData:getHeroIsUnlock(v.force_id) and v.display == 1 then
				isRemove = true
			end
		end
		if not isRemove then
			table.insert(self.shopList, v)
		end
	end

	table.sort(self.shopList, function(a, b)
		local itemId1 = GFunc.getRewardId(a.hero)
		local itemId2 = GFunc.getRewardId(b.hero)
		-- local type1 = ModuleManager.ItemManager:getItemType(itemId1)
		-- local type2 = ModuleManager.ItemManager:getItemType(itemId2)
		-- 判断是否为未解锁英雄
		local isLockedHero1 = DataManager.HeroData:getHeroIsUnlock(itemId1)
		local isLockedHero2 = DataManager.HeroData:getHeroIsUnlock(itemId2)
		if isLockedHero1 == isLockedHero2 then
			return a.id < b.id
		elseif isLockedHero1 and not isLockedHero2 then
			return true
		elseif not isLockedHero1 and isLockedHero2 then
			return false
		end
		return a.id < b.id
	end)

	if not self.initScrollrect then
		self.initScrollrect = true
		self.scrollrect:addInitCallback(function()
			return "app/ui/summon/cell/summon_shop_cell"
		end)
		self.scrollrect:addRefreshCallback(function(index, cell)
			cell:setShowType(GConst.HeroConst.PAGE_TYPE.SHOP, function()
				local cfg = self.shopList[index]
				local id = GFunc.getRewardId(cfg.hero)
				local type = ModuleManager.ItemManager:getItemType(id)
                if type == GConst.ItemConst.ITEM_TYPE.HERO_FRAGMENT then
					if not DataManager.HeroData:getHeroIsUnlock(cfg.force_id) then
						GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESCR_5))
						return
					end
				end
				ModuleManager.SummonManager:showSummonShopBoxUI(self.shopType, self.shopList[index])
			end)
			cell:refresh(self.shopList[index], false, self.shopType)
		end)
		self.scrollrect:clearCells()
		self.scrollrect:refillCells(#self.shopList)
	else
		-- 刷新或重新填充列表
		if self.scrollrect:getTotalCount() == #self.shopList then
			self.scrollrect:updateAllCell()
		else
			self.scrollrect:refillCells(#self.shopList)
		end
	end
end

return SummonShopUI
