local HeroDetailUI = class("HeroDetailUI", BaseUI)
local COMP_HERO = "app/ui/hero/hero_info_comp"
local COMP_STAR = "app/ui/hero/star_info_comp"
local COMP_EQUIP = "app/ui/hero/equip_info_comp"
local SIZE_DELTA_Y_HERO = 942
local SIZE_DELTA_Y_LOOK_HERO = 802

function HeroDetailUI:isFullScreen()
	return false
end

function HeroDetailUI:getPrefabPath()
	return "assets/prefabs/ui/hero/hero_detail_ui.prefab"
end

function HeroDetailUI:onPressBackspace()
	self:closeUI()
end

function HeroDetailUI:ctor(parmas)
	self.page = parmas.panelType or GConst.HeroConst.PANEL_TYPE.HERO
	self.formationType = parmas.formationType
	self.onlyLook = parmas.onlyLook
	if parmas.heroEntity then
		self.heroEntity = parmas.heroEntity
	else
		local heroId = parmas.heroId
		self.heroEntity = DataManager.HeroData:getHeroById(heroId)
	end
	self:initList()
end

function HeroDetailUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()

	uiMap["hero_detail_ui.common.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

	self.heroInfo = uiMap["hero_detail_ui.hero_info"]
	self.starInfo = uiMap["hero_detail_ui.star_info"]
	self.equipInfo = uiMap["hero_detail_ui.equip_info"]
	self.titleTx = uiMap["hero_detail_ui.common.name_tx"]
	self.bg = uiMap["hero_detail_ui.common.bg"]
	self.leftBtn = uiMap["hero_detail_ui.common.left_btn"]
	self.rightBtn = uiMap["hero_detail_ui.common.right_btn"]

	self.heroNode = uiMap["hero_detail_ui.hero_node"]
	self.elementBg = uiMap["hero_detail_ui.common.hero_node.element_bg"]
	self.elementDesc = uiMap["hero_detail_ui.common.hero_node.element_bg.element_desc_tx"]
	self.heroElement = uiMap["hero_detail_ui.common.hero_node.hero_element"]
	self.spineObjAvatar = uiMap["hero_detail_ui.common.hero_node.ui_spine_obj_avatar"]
	self.powerTx = uiMap["hero_detail_ui.common.power_img.power_tx"]
	self.qltBg = uiMap["hero_detail_ui.common.qlt_bg"]
	self.qltBgTx = uiMap["hero_detail_ui.common.qlt_bg.qlt_tx"]
	self.starComp = uiMap["hero_detail_ui.common.hero_node.star_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.STAR_CELL)
	self.btnsLayout = uiMap["hero_detail_ui.common.btns"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
	self.redNodeLayout = uiMap["hero_detail_ui.red_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)

	self.btnTxs = {I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4), I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_16), I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_1)}
	self.pageBtns = {}
	self.pageBtnTxs = {}
	self.pageBtnLocks = {}
	self.pageRedImgs = {}
	for i = 1, 3 do
		self.pageBtns[i] = uiMap["hero_detail_ui.common.btns.page_btn_" .. i]
		self.pageBtnTxs[i] = uiMap["hero_detail_ui.common.btns.page_btn_" .. i .. ".text"]
		self.pageBtnLocks[i] = uiMap["hero_detail_ui.common.btns.page_btn_" .. i .. ".lock_img"]
		self.pageRedImgs[i] = uiMap["hero_detail_ui.red_node.red_img_" .. i]
		if self.pageBtnLocks[i] then
			self.pageBtnLocks[i]:setActive(not self:getIsOpen(i))
		end
		self.pageBtns[i] :addClickListener(function()
			if not self:getIsOpen(i) or self.page == i then
				return
			end
			self.page = i
			self:onRefresh()
		end)
	end

	self.leftBtn:addClickListener(function()
		if not self:isShowLeftArrow() then
			return
		end
		self.heroEntity = DataManager.HeroData:getHeroById(self.heroList[self.idxLast].cfgId)
		self:onRefresh()
	end)
	self.rightBtn:addClickListener(function()
		if not self:isShowRightArrow() then
			return
		end
		self.heroEntity = DataManager.HeroData:getHeroById(self.heroList[self.idxNext].cfgId)
		self:onRefresh()
	end)
	self:bind(DataManager.BagData.ItemData, "dirty", function()
		self:refreshPageBtn()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.HERO_UPGRADE_SUCCESS, function(id)
		self:refreshPageBtn()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.EQUIP_UPSECTION_SUCCESS, function(part)
		if part == GConst.EquipConst.PART_TYPE.STAR then
			self.compStar:playEffect(true, true)
		else
			self.compEquip:playUpgradeEffect(part)
		end
		self:refreshPageBtn()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.EQUIP_UPGRADE_SUCCESS, function(part)
		if part == GConst.EquipConst.PART_TYPE.STAR then
			self.compStar:playEffect(true, false)
		else
			self.compEquip:playUpgradeEffect(part)
		end
		self:refreshPageBtn()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.GO_DUNGEON_UI, function()
		self:closeUI()
	end)
end

function HeroDetailUI:initList()
	self.heroList = DataManager.HeroData:getAllHeroesSort(self.formationType)
end

function HeroDetailUI:getIsOpen(page)
	if page == GConst.HeroConst.PANEL_TYPE.HERO then
		return true
	elseif page == GConst.HeroConst.PANEL_TYPE.STAR then
		return DataManager.HeroData:isStarOpen()
	elseif page == GConst.HeroConst.PANEL_TYPE.EQUIP then
		return true
	end
	return false
end

function HeroDetailUI:onRefresh()
	self:updateHero()
	self:updateSide()
	self:refreshPageInfo()
end

function HeroDetailUI:changePage(page)
	self.page = page
	self:onRefresh()
end

function HeroDetailUI:updateSide()
	for index, data in ipairs(self.heroList) do
		if data.cfgId == self.heroEntity:getCfgId() then
			self.idxLast = index - 1
			self.idxNext = index + 1
		end
	end
end

function HeroDetailUI:updateHero()
	local matchType = self.heroEntity:getMatchType()
	self.elementDesc:setText(ModuleManager.HeroManager:getMatchTypeName(matchType))
	local elementTxRectWidth = self.elementDesc:getRectWidth()
	local elementTxWidth = self.elementDesc:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
	if elementTxWidth > elementTxRectWidth then
		self.elementBg:setSizeDeltaX(52 + elementTxWidth)
	else
		self.elementBg:setSizeDeltaX(52 + elementTxRectWidth)
	end

	self.elementBg:setSprite(ModuleManager.HeroManager:getMatchTypeBg(matchType))
	self.heroElement:setSprite(GConst.ATLAS_PATH.ICON_HERO, ModuleManager.HeroManager:getMatchTypeIcon(matchType))
	self.spineObjAvatar:getSkeletonGraphic().enabled = false
	self.spineObjAvatar:loadAssetAsync(self.heroEntity:getModelId(), function()
		self.spineObjAvatar:getSkeletonGraphic().enabled = true
		self.spineObjAvatar:playAnim("idle", true, true, true)
	end, true)

	local qlt = self.heroEntity:getQlt()
	self.starComp:refresh(self.heroEntity:getStar())
	self.qltBg:setSprite(GFunc.getHeroQltImg(qlt))
	self.qltBgTx:setText(GFunc.getHeroQltStr(qlt))
end

function HeroDetailUI:refreshPageInfo()
	self.titleTx:setText(self.heroEntity:getName())
	if self.page == GConst.HeroConst.PANEL_TYPE.HERO then
		self:showHeroInfo()
	elseif self.page == GConst.HeroConst.PANEL_TYPE.STAR then
		self:showStarInfo()
	elseif self.page == GConst.HeroConst.PANEL_TYPE.EQUIP then
		self:showEquipInfo()
	end
	self:refreshPageBtn()
end

-- 刷新标签红点
function HeroDetailUI:refreshPageBtn()
	if self.onlyLook then -- 仅查看的不显示升级和激活按钮
		self.leftBtn:setActive(false)
		self.rightBtn:setActive(false)
		for i = 1, 3 do
			self.pageBtns[i]:setActive(false)
			self.pageRedImgs[i]:setActive(false)
		end
	else
		self.leftBtn:setActive(self:isShowLeftArrow())
		self.rightBtn:setActive(self:isShowRightArrow())
		for i = 1, 3 do
			self.pageBtns[i]:setActive(true)
			if self.page == i then
				self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.COMMON, "common_tab_1")
				self.pageBtnTxs[i]:setText(self.btnTxs[i])
			else
				self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.COMMON, "common_tab_2")
				self.pageBtnTxs[i]:setText("<color=#72778C>" .. self.btnTxs[i] .. "</color>")
			end
			self.pageRedImgs[i]:setActive(self.heroEntity:showRedPoint(i))
		end
	end
end

function HeroDetailUI:showHeroInfo()
	self.heroInfo:setActive(true)
	self.starInfo:setActive(false)
	self.equipInfo:setActive(false)
	self.bg:setActive(true)

	local power = self.heroEntity:getPower()
	self.powerTx:setText(GFunc.num2Str2(power, 2))
	if not self.compHero then
		self.heroInfo:initPrefabHelper()
		self.heroInfo:genAllChildren()
		self.compHero = self.heroInfo:addLuaComponent(COMP_HERO)
		self.compHero:setParentUI(self)
	end

	self.compHero:setHeroData(self.heroEntity, self.onlyLook)
	self.compHero:refresh()
end

function HeroDetailUI:showStarInfo()
	self.heroInfo:setActive(false)
	self.starInfo:setActive(true)
	self.equipInfo:setActive(false)
	self.bg:setActive(true)

	if not self.compStar then
		self.starInfo:initPrefabHelper()
		self.starInfo:genAllChildren()
		self.compStar = self.starInfo:addLuaComponent(COMP_STAR)
		self.compStar:setParentUI(self)
	end

	self.compStar:setHeroData(self.heroEntity, self.onlyLook)
	self.compStar:refresh()
end

function HeroDetailUI:showEquipInfo()
	self.heroInfo:setActive(false)
	self.starInfo:setActive(false)
	self.equipInfo:setActive(true)
	self.bg:setActive(true)

	if not self.compEquip then
		self.equipInfo:initPrefabHelper()
		self.equipInfo:genAllChildren()
		self.compEquip = self.equipInfo:addLuaComponent(COMP_EQUIP)
		self.compEquip:setParentUI(self)
	end

	self.compEquip:setHeroData(self.heroEntity)
	self.compEquip:refresh()
end

-- 是否显示左箭头
function HeroDetailUI:isShowLeftArrow()
	if self.page == GConst.HeroConst.PANEL_TYPE.RUNES then
		return false
	end

	return self.idxLast and self.idxLast > 0
end

-- 是否显示右箭头
function HeroDetailUI:isShowRightArrow()
	if self.page == GConst.HeroConst.PANEL_TYPE.RUNES then
		return false
	end

	return self.idxNext and self.idxNext <= #self.heroList
end

return HeroDetailUI