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.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.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.titleTx = uiMap["hero_detail_ui.common.name_tx"]
	self.bg = uiMap["hero_detail_ui.common.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.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.btnTxs = {I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4), I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_16)}
	self.pageBtns = {}
	self.pageBtnTxs = {}
	self.pageBtnLocks = {}
	self.pageRedImgs = {}
	for i = 1, 2 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.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))
	-- self.powerTx:setText(self.heroEntity:getPower())
	-- 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.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.compSkin: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.compSkin: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()
	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.SKIN then
		self:showskinInfo()
	end
	self:refreshPageBtn()
end

-- 刷新标签红点
function HeroDetailUI:refreshPageBtn()
	if self.onlyLook then -- 仅查看的不显示升级和激活按钮
		self.leftBtn:setActive(false)
		self.rightBtn:setActive(false)
		for i = 1, 2 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, 2 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.selectHero:setActive(true)
	self.starInfo:setActive(false)
	-- self.selectStar:setActive(false)
	self.skinInfo:setActive(false)
	-- self.selectSkin: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.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.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