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_SKIN = "app/ui/hero/skin_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.panelType = 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
end

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

	self.commonInfo = uiMap["hero_detail_ui.common"]
	self.heroInfo = uiMap["hero_detail_ui.hero_info"]
	self.starInfo = uiMap["hero_detail_ui.star_info"]
	self.skinInfo = uiMap["hero_detail_ui.skin_info"]
	self.txTitle = uiMap["hero_detail_ui.common.img_title.tx_title"]
	self.btnClose = uiMap["hero_detail_ui.common.btn_close"]
	self.bg = uiMap["hero_detail_ui.common.bg.bg"]
	self.btnHero = uiMap["hero_detail_ui.common.btns.btn_hero"]
	self.txHero1 = uiMap["hero_detail_ui.common.btns.btn_hero.tx_btn"]
	self.selectHero = uiMap["hero_detail_ui.common.btns.btn_hero.select"]
	self.txHero2 = uiMap["hero_detail_ui.common.btns.btn_hero.select.tx_select"]
	self.btnStar = uiMap["hero_detail_ui.common.btns.btn_star"]
	self.lockStar = uiMap["hero_detail_ui.common.btns.btn_star.content.img_lock"]
	self.txStar1 = uiMap["hero_detail_ui.common.btns.btn_star.content.tx_btn"]
	self.selectStar = uiMap["hero_detail_ui.common.btns.btn_star.select"]
	self.txStar2 = uiMap["hero_detail_ui.common.btns.btn_star.select.tx_select"]
	self.btnSkin = uiMap["hero_detail_ui.common.btns.btn_skin"]
	self.lockSkin = uiMap["hero_detail_ui.common.btns.btn_skin.content.img_lock"]
	self.txSkin1 = uiMap["hero_detail_ui.common.btns.btn_skin.content.tx_btn"]
	self.selectSkin = uiMap["hero_detail_ui.common.btns.btn_skin.select"]
	self.txSkin2 = uiMap["hero_detail_ui.common.btns.btn_skin.select.tx_select"]
	self.btnLeft = uiMap["hero_detail_ui.common.btn_left"]
	self.btnRight = uiMap["hero_detail_ui.common.btn_right"]

	self.heroNode = uiMap["hero_detail_ui.hero_node"]
	self.elementBg = uiMap["hero_detail_ui.hero_node.element_bg"]
	self.elementDesc = uiMap["hero_detail_ui.hero_node.element_bg.element_desc"]
	self.heroElement = uiMap["hero_detail_ui.hero_node.hero_element"]
	self.spineObjAvatar = uiMap["hero_detail_ui.hero_node.ui_spine_obj_avatar"]

	self.txHero1:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4))
	self.txHero2:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4))
	-- self.txStar1:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_1))
	-- self.txStar2:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_1))
	-- self.txSkin1:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_2))
	-- self.txSkin2:setText(I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_2))
	self.txStar1:setText("升星")
	self.txStar2:setText("升星")
	self.txSkin1:setText(I18N:getGlobalText(I18N.GlobalConst.SKIN))
	self.txSkin2:setText(I18N:getGlobalText(I18N.GlobalConst.SKIN))
	if not DataManager.HeroData:isStarOpen() then
		self.lockStar:setVisible(true)
		GFunc.centerImgAndTx(self.lockStar, self.txStar1, 5)
	else
		self.lockStar:setVisible(false)
	end
	if not DataManager.HeroData:isSkinOpen() then
		self.lockSkin:setVisible(true)
		GFunc.centerImgAndTx(self.lockSkin, self.txSkin1, 5)
	else
		self.lockSkin:setVisible(false)
	end

	self.btnHero:addClickListener(function()
		self.panelType = GConst.HeroConst.PANEL_TYPE.HERO
		self:refreshShow()
	end)
	self.btnStar:addClickListener(function()
		if not DataManager.HeroData:isStarOpen(true) then
			return
		end
		self.panelType = GConst.HeroConst.PANEL_TYPE.STAR
		self:refreshShow()
	end)
	self.btnSkin:addClickListener(function()
		if not DataManager.HeroData:isSkinOpen(true) then
			return
		end
		self.panelType = GConst.HeroConst.PANEL_TYPE.SKIN
		self:refreshShow()
	end)
	self.btnClose:addClickListener(function()
		self:closeUI()
	end)
	self.btnLeft:addClickListener(function()
		if not self:isShowLeftArrow() then
			return
		end
		self.heroEntity = DataManager.HeroData:getHeroById(self.heroList[self.idxLast].cfgId)
		self:updateSide()
		self:refreshShow()
	end)
	self.btnRight:addClickListener(function()
		if not self:isShowRightArrow() then
			return
		end
		self.heroEntity = DataManager.HeroData:getHeroById(self.heroList[self.idxNext].cfgId)
		self:updateSide()
		self:refreshShow()
	end)
	self:bind(DataManager.BagData.ItemData, "dirty", function()
		self:refreshRedPoint()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.HERO_UPGRADE_SUCCESS, function(id)
		self:refreshRedPoint()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.EQUIP_UPSECTION_SUCCESS, function(part)
		Logger.logHighlight("升段："..part)
		if part == GConst.EquipConst.PART_TYPE.STAR then
			self.compStar:playEffect(true, true)
		else
			self.compSkin:playUpgradeEffect(part)
		end
		self:refreshRedPoint()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.EQUIP_UPGRADE_SUCCESS, function(part)
		Logger.logHighlight("升级："..part)
		if part == GConst.EquipConst.PART_TYPE.STAR then
			self.compStar:playEffect(true, false)
		else
			self.compSkin:playUpgradeEffect(part)
		end
		self:refreshRedPoint()
	end)
	self:addEventListener(EventManager.CUSTOM_EVENT.GO_DUNGEON_UI, function()
		self:closeUI()
	end)
end

function HeroDetailUI:onRefresh()
	self.heroList = DataManager.HeroData:getAllHeroesSort(self.formationType)
	self:updateSide()
	self:refreshShow()
	self:updateHero()
end

function HeroDetailUI:updateHero()
	self.elementDesc:setText(ModuleManager.HeroManager:getMatchTypeName(self.heroEntity:getMatchType()))
	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.heroElement:setSprite(GConst.ATLAS_PATH.HERO, ModuleManager.HeroManager:getMatchTypeIcon(self.heroEntity:getMatchType()))
	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)
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:changePage(panelType)
	self.panelType = panelType
	self:refreshShow()
end

function HeroDetailUI:refreshShow()
	self.txTitle:setText(self.heroEntity:getName())
	if self.panelType == GConst.HeroConst.PANEL_TYPE.HERO then
		self:showHeroInfo()
	elseif self.panelType == GConst.HeroConst.PANEL_TYPE.STAR then
		self:showStarInfo()
	elseif self.panelType == GConst.HeroConst.PANEL_TYPE.SKIN then
		self:showskinInfo()
	end
	if self.onlyLook then -- 仅查看的不显示升级和激活按钮
		self.btnHero:setActive(false)
		self.btnStar:setActive(false)
		self.btnSkin:setActive(false)
		self.btnLeft:setActive(false)
		self.btnRight:setActive(false)
		if self.panelType == GConst.HeroConst.PANEL_TYPE.HERO then
			self.commonInfo:setSizeDeltaY(SIZE_DELTA_Y_LOOK_HERO)
		end
	else
		self.btnHero:setActive(true)
		self.btnStar:setActive(true)
		self.btnSkin:setActive(true)
		self.btnLeft:setActive(self:isShowLeftArrow())
		self.btnRight:setActive(self:isShowRightArrow())
		self.commonInfo:setSizeDeltaY(SIZE_DELTA_Y_HERO)
		self:refreshRedPoint()
	end
end

-- 刷新标签红点
function HeroDetailUI:refreshRedPoint()
	if self.heroEntity:canLvUp() then
		self.btnHero:addRedPoint(-55, 0, 0.6)
	else
		self.btnHero:removeRedPoint()
	end
	-- if DataManager.EquipData:canUpgradeStar(self.heroEntity:getCfgId()) then
	-- 	self.btnStar:addRedPoint(-55, 0, 0.6)
	-- else
	-- 	self.btnStar:removeRedPoint()
	-- end
	-- if DataManager.EquipData:canUpgradeSkin(self.heroEntity:getCfgId()) then
	-- 	self.btnSkin:addRedPoint(-55, 0, 0.6)
	-- else
	-- 	self.btnSkin:removeRedPoint()
	-- end
end

function HeroDetailUI:showHeroInfo()
	self.heroInfo:setActive(true)
	self.selectHero:setActive(true)
	self.starInfo:setActive(false)
	self.selectStar:setActive(false)
	self.skinInfo:setActive(false)
	self.selectSkin:setActive(false)
	self.bg:setActive(true)

	if not self.compHero then
		self.heroInfo:initPrefabHelper()
		self.heroInfo:genAllChildren()
		self.compHero = self.heroInfo:addLuaComponent(COMP_HERO)
	end

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

function HeroDetailUI:showStarInfo()
	self.heroInfo:setActive(false)
	self.selectHero:setActive(false)
	self.starInfo:setActive(true)
	self.selectStar:setActive(true)
	self.skinInfo:setActive(false)
	self.selectSkin: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:showskinInfo()
	self.heroInfo:setActive(false)
	self.selectHero:setActive(false)
	self.starInfo:setActive(false)
	self.selectStar:setActive(false)
	self.skinInfo:setActive(true)
	self.selectSkin:setActive(true)
	self.bg:setActive(true)

	if not self.compSkin then
		self.skinInfo:initPrefabHelper()
		self.skinInfo:genAllChildren()
		self.compSkin = self.skinInfo:addLuaComponent(COMP_SKIN)
		self.compSkin:setParentUI(self)
	end

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

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

	return self.idxLast and self.idxLast > 0
end

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

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

return HeroDetailUI