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

local DEFAULT_FACTOR = GConst.BattleConst.DEFAULT_FACTOR

local BTN_LIGHT = {
	"act_common_btn_16_1",
	"act_common_btn_17_1",
	"act_common_btn_25_1",
}

local BTN_NORMAL = {
	"act_common_btn_16_2",
	"act_common_btn_17_2",
	"act_common_btn_25_2",
}

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.curLevel = self.heroEntity:getLv()
	self:initList()
	self:checkPage()
end

function HeroDetailUI:checkPage()
	if self.heroEntity:showRedPoint(GConst.HeroConst.PANEL_TYPE.STAR, self.formationType) then
		self.page = GConst.HeroConst.PANEL_TYPE.STAR
	elseif self.heroEntity:showRedPoint(GConst.HeroConst.PANEL_TYPE.EQUIP, self.formationType) then
		self.page = GConst.HeroConst.PANEL_TYPE.EQUIP
	end
end

function HeroDetailUI:onLoadRootComplete()
	local uiMap = self.root:genAllChildren()
	uiMap["hero_detail_ui.bottom.close_btn"]:addClickListener(function()
		self:closeUI()
	end)

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

	self.heroNode = uiMap["hero_detail_ui.hero_node"]
	self.heroElement = uiMap["hero_detail_ui.top.hero_element"]
	self.spineObjAvatar = uiMap["hero_detail_ui.top.hero_node.ui_spine_obj_avatar"]
	self.powerTx = uiMap["hero_detail_ui.top.power_img.power_tx"]
	self.qltBg = uiMap["hero_detail_ui.top.qlt_bg"]
	self.qltBgTx = uiMap["hero_detail_ui.top.qlt_bg.qlt_tx"]
	self.starComp = uiMap["hero_detail_ui.top.star_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.STAR_CELL)

	self.lvTx = uiMap["hero_detail_ui.top.attr_node.bg_4.lv_tx"]
	self.bg5 = uiMap["hero_detail_ui.top.attr_node.bg_5"]
	self.bg6 = uiMap["hero_detail_ui.top.attr_node.bg_6"]
	self.hpNameTx = uiMap["hero_detail_ui.top.attr_node.bg_6.hp_name_tx"]
	self.atkNameTx = uiMap["hero_detail_ui.top.attr_node.bg_5.atk_name_tx"]
	self.hpTx = uiMap["hero_detail_ui.top.attr_node.bg_6.hp_tx"]
	self.atkTx = uiMap["hero_detail_ui.top.attr_node.bg_5.atk_tx"]
	self.bg4Vfx01 = uiMap["hero_detail_ui.top.attr_node.bg_4.vfx_c1_ui_up_b01"]
	self.bg5Vfx01 = uiMap["hero_detail_ui.top.attr_node.bg_5.vfx_c1_ui_up_b01"]
	self.bg6Vfx01 = uiMap["hero_detail_ui.top.attr_node.bg_6.vfx_c1_ui_up_b01"]
	self.bg4Vfx01:setActive(false)
	self.bg5Vfx01:setActive(false)
	self.bg6Vfx01:setActive(false)
	self.bg5:addClickListener(function()
		UIManager:showUI("app/ui/hero/hero_attr_ui", {heroEntity = self.heroEntity})
	end)
	self.bg6:addClickListener(function()
		UIManager:showUI("app/ui/hero/hero_attr_ui", {heroEntity = self.heroEntity})
	end)
	self.hpNameTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_2))
	self.atkNameTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_3))

	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.pageRedImgs = {}
	for i = 1, 3 do
		self.pageBtns[i] = uiMap["hero_detail_ui.bottom.btns.page_btn_" .. i]
		self.pageBtnTxs[i] = uiMap["hero_detail_ui.bottom.btns.page_btn_" .. i .. ".text"]
		self.pageRedImgs[i] = uiMap["hero_detail_ui.bottom.btns.page_btn_" .. i .. ".red_img"]
		self.pageBtnTxs[i]:setText(self.btnTxs[i])
		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.skillNode = uiMap["hero_detail_ui.top.bg_1"]
	self.attrNode = uiMap["hero_detail_ui.top.attr_node"]
	self.skillBg = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_bg"]
	self.imgSkill = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_bg.skill_icon"]
	self.skillNameTx = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_bg.skill_name_tx"]
	self.skill = {}
	self.skillIcon = {}
	self.skillDescTxs = {}
	self.skillVfxs = {}
	self.skillNeedStarNodes = {}
	self.skillNeedStarDescTx1s = {}
	self.skillNeedStarStarImgs = {}
	self.skillNeedStarDescTx2s = {}
	for i = 1, 4 do
		self.skill[i] = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_up_" .. i]
		self.skillIcon[i] = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_up_" .. i .. ".icon"]
		self.skillDescTxs[i] = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_up_" .. i .. ".desc_tx"]
		self.skillNeedStarNodes[i] = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_up_" .. i .. ".need_star_node"]
		self.skillNeedStarDescTx1s[i] = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_up_" .. i .. ".need_star_node.desc_tx_1"]
		self.skillNeedStarStarImgs[i] = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_up_" .. i .. ".need_star_node.star_img"]
		self.skillNeedStarDescTx2s[i] = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_up_" .. i .. ".need_star_node.desc_tx_2"]
		self.skillVfxs[i] = uiMap["hero_detail_ui.top.bg_1.skill_node.skill_up_" .. i .. ".vfx_c1_ui_up_b05"]
		self.skillVfxs[i]:setActive(false)
	end
	self.skillBg:addClickListener(function()
		local cfg = ConfigManager:getConfig("skill")[self.heroEntity:getBaseSkill()]
		ModuleManager.HeroManager:showHeroSkillInfoUI2(self.heroEntity, cfg.buff_id)
	end)

	self.lvUpVfxs = {}
	for i = 1, 5 do
		self.lvUpVfxs[i] = uiMap["hero_detail_ui.top.vfx_c1_ui_uplight_b0" .. i]
		self.lvUpVfxs[i]:setActive(false)
	end

	self.leftBtn:addClickListener(function()
		if not self:isShowLeftArrow() then
			return
		end
		self.heroEntity = DataManager.HeroData:getHeroById(self.heroList[self.idxLast].cfgId)
		self.curLevel = self.heroEntity:getLv()
		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.curLevel = self.heroEntity:getLv()
		self:onRefresh()
	end)
	self:bind(DataManager.BagData.ItemData, "dirty", function()
		self:refreshPageBtn()
	end)
	self:bind(DataManager.HeroData, "isDirty", function()
		self:onRefresh(true)
	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)

	local middle = uiMap["hero_detail_ui.middle"]
	local rect = self.root:getRectSize()
	local bgHeight = rect.height * 0.64
	local bgHeight1 = rect.height * 0.66
	middle:setSizeDeltaY(bgHeight1)
	self.top:setAnchoredPositionY(bgHeight)
	self.bg:setAnchoredPositionY(bgHeight)
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 DataManager.EquipData:isOpen()
	end
	return false
end

function HeroDetailUI:onRefresh(checkUp)
	self.checkLvUpState = checkUp
	self:updateTop()
	self:updateHero()
	self:updateSide()
	self:refreshPageInfo()
	self:refreshSkillInfo(checkUp)
	self:checkLvUp(checkUp)
	self.checkLvUpState = nil
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:updateTop()
	local lv = self.heroEntity:getLv()
	local hpStr
	local atkStr
	if not self.heroEntity:isActived() then
		hpStr = self.heroEntity:getCfgHp(self.heroEntity:getBeginLv()) // DEFAULT_FACTOR
		atkStr = self.heroEntity:getCfgAtk(self.heroEntity:getBeginLv()) // DEFAULT_FACTOR
	else
		local curHp = self.heroEntity:getHp() // DEFAULT_FACTOR
		local curAtk = self.heroEntity:getAtk() // DEFAULT_FACTOR
		local addHp = (self.heroEntity:getCfgHp(lv + 1) - self.heroEntity:getCfgHp()) // DEFAULT_FACTOR
		local addAtk = (self.heroEntity:getCfgAtk(lv + 1) - self.heroEntity:getCfgAtk()) // DEFAULT_FACTOR
		if addHp <= 0 then
			hpStr = curHp
		else
			hpStr = curHp .. "<color=#48E557>+" .. addHp .. "</color>"
		end
		if addAtk <= 0 then
			atkStr = curAtk
		else
			atkStr = curAtk .. "<color=#48E557>+" .. addAtk .. "</color>"
		end
	end
	self.hpTx:setText(hpStr)
	self.atkTx:setText(atkStr)
	self.lvTx:setText(I18N:getGlobalText(I18N.GlobalConst.RUNES_DESC_26, self.heroEntity:getLv()))
end

function HeroDetailUI:updateHero()
	local matchType = self.heroEntity:getMatchType()
	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:refreshSkillInfo(checkUp)
	local skillId = self.heroEntity:getBaseSkill()
	self.skillNameTx:setText(ModuleManager.HeroManager:getSkillName(skillId))
	-- self.txSkill:setText(ModuleManager.HeroManager:getSkillDesc(skillId))
	self.skillBg:setSprite(GConst.ATLAS_PATH.HERO, ModuleManager.HeroManager:getSkillBg(skillId))
	self.imgSkill:setSprite(GConst.ATLAS_PATH.ICON_SKILL, ModuleManager.HeroManager:getSkillIcon(skillId))

	local skillList = self.heroEntity:getRogueSkillList()
	local unlockIdx = self.heroEntity:getUnlockSkillIdx(checkUp)
	local lv = self.heroEntity:getLv()
	local star = self.heroEntity:getStar()
	for i = 1, 4 do
		self.skillVfxs[i]:setActive(false)
		local skillInfo = skillList[i]
		if skillInfo then
			local skillUnlcokStar = skillInfo[1]
			local skillId = skillInfo[2]
			local skillBg = self.skill[i]
			local skillIcon = self.skillIcon[i]
			local nextLvUpStar = self.heroEntity:getNextRougeLvUp(i)
			skillBg:addClickListener(function()
				local cfg = ConfigManager:getConfig("skill_rogue")[skillId]
				-- ModuleManager.TipsManager:showSkillTips(skillIcon, cfg.buff_id, skillId)
				ModuleManager.HeroManager:showHeroSkillInfoUI(self.heroEntity, i, cfg.buff_id)
			end)
			skillBg:setTouchEnable(true)
			if unlockIdx and unlockIdx == i then
				self.skillVfxs[i]:setActive(true)
			end
			if skillUnlcokStar > star then
				self.skillNeedStarNodes[i]:setActive(true)
				local nextStar = (skillUnlcokStar - 1)%5 + 1
				self.skillNeedStarDescTx1s[i]:setText(nextStar)
				local meshProComp = self.skillNeedStarDescTx1s[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
				local contentWidth = meshProComp.preferredWidth
				self.skillNeedStarDescTx1s[i]:setSizeDeltaX(contentWidth)
				local starType = math.ceil(skillUnlcokStar / 5)
				self.skillNeedStarStarImgs[i]:setSprite(GFunc.getStarImg(starType))
				self.skillNeedStarDescTx2s[i]:setText(I18N:getGlobalText(I18N.GlobalConst.STR_UNLOCK))
				local meshProComp = self.skillNeedStarDescTx2s[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
				local contentWidth = meshProComp.preferredWidth
				self.skillNeedStarDescTx2s[i]:setSizeDeltaX(contentWidth)
				self.skillNeedStarNodes[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()

				skillBg:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueBg(skillId, true) .. "_1")
				skillIcon:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueIcon(skillId) .. "_1")
			else
				if nextLvUpStar then
					self.skillNeedStarNodes[i]:setActive(true)
					local nextStar = (nextLvUpStar - 1)%5 + 1
					self.skillNeedStarDescTx1s[i]:setText(nextStar)
					local meshProComp = self.skillNeedStarDescTx1s[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
					local contentWidth = meshProComp.preferredWidth
					self.skillNeedStarDescTx1s[i]:setSizeDeltaX(contentWidth)
					local starType = math.ceil(nextLvUpStar / 5)
					self.skillNeedStarStarImgs[i]:setSprite(GFunc.getStarImg(starType))
					self.skillNeedStarDescTx2s[i]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_17))
					local meshProComp = self.skillNeedStarDescTx2s[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO)
					local contentWidth = meshProComp.preferredWidth
					self.skillNeedStarDescTx2s[i]:setSizeDeltaX(contentWidth)
					self.skillNeedStarNodes[i]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
				else
					self.skillNeedStarNodes[i]:setActive(false)
				end
				skillBg:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueBg(skillId, true))
				skillIcon:setSprite(GConst.ATLAS_PATH.ICON_SKILL_ROGUE, ModuleManager.HeroManager:getSkillRogueIcon(skillId))
			end
		end
	end

	local needPop, isUnlock, skillIdx = self.heroEntity:checkSkillUnlock()
	if needPop then
		self.root:performWithDelayGlobal(function()
			local skillInfo = skillList[skillIdx]
			if skillInfo then
				local skillId = skillInfo[2]
				local cfg = ConfigManager:getConfig("skill_rogue")[skillId]
				ModuleManager.HeroManager:showHeroSkillInfoUI(self.heroEntity, skillIdx, cfg.buff_id, isUnlock, needPop)
			end
		end, 0.5)
	end
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()

	local power = self.heroEntity:getPower()
	self.powerTx:setText(GFunc.num2Str2(power, 2))
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)
		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.UI_ACT_COMMON, BTN_LIGHT[i])
			else
				self.pageBtns[i]:setSprite(GConst.ATLAS_PATH.UI_ACT_COMMON, BTN_NORMAL[i])
			end
			self.pageRedImgs[i]:setActive(self.heroEntity:showRedPoint(i, self.formationType))
		end
	end
end

function HeroDetailUI:showHeroInfo()
	self.heroInfo:setActive(true)
	self.starInfo:setActive(false)
	self.equipInfo:setActive(false)
	self.skillNode:setActive(true)
	self.attrNode:setAnchoredPositionY(-709)

	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.formationType)
	self.compHero:refresh(self.checkLvUpState)
end

function HeroDetailUI:showStarInfo()
	self.heroInfo:setActive(false)
	self.starInfo:setActive(true)
	self.equipInfo:setActive(false)
	self.skillNode:setActive(true)
	self.attrNode:setAnchoredPositionY(-709)

	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.formationType)
	self.compStar:refresh(self.checkLvUpState)
end

function HeroDetailUI:showEquipInfo()
	self.heroInfo:setActive(false)
	self.starInfo:setActive(false)
	self.equipInfo:setActive(true)
	self.skillNode:setActive(false)
	self.attrNode:setAnchoredPositionY(-527)

	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.onlyLook, self.formationType)
	self.compEquip:refresh(self.checkLvUpState)
end

-- 是否显示左箭头
function HeroDetailUI:isShowLeftArrow()
	return self.idxLast and self.idxLast > 0
end

-- 是否显示右箭头
function HeroDetailUI:isShowRightArrow()
	return self.idxNext and self.idxNext <= #self.heroList
end

function HeroDetailUI:checkLvUp(checkLevel)
	if self.page ~= GConst.HeroConst.PANEL_TYPE.HERO then
		return false
	end
	local isLvChange = checkLevel and self.curLevel ~= self.heroEntity:getLv()

	for i = 1, 5 do
		self.lvUpVfxs[i]:setActive(false)
	end
	self.bg4Vfx01:setActive(false)
	self.bg5Vfx01:setActive(false)
	self.bg6Vfx01:setActive(false)

	if isLvChange then
		local match = self.heroEntity:getMatchType()
		self.lvUpVfxs[match]:setActive(true)
		self.bg4Vfx01:setActive(true)
		self.bg5Vfx01:setActive(true)
		self.bg6Vfx01:setActive(true)
	end
end

return HeroDetailUI