local HeroInfoComp = class("HeroInfoComp", LuaComponent)

local DEFAULT_FACTOR = GConst.BattleConst.DEFAULT_FACTOR
local BTN_ICON = {"common_btn_blue_3", "common_btn_yellow_3", "common_btn_grey_3"}
local SIZE_DELTA_Y_HERO = 942
local SIZE_DELTA_Y_LOOK = 802
local LV_CELL = "app/ui/hero/cell/lv_cell"

function HeroInfoComp:init()
	local uiMap = self:getUIMap()

	self.btnUp = uiMap["hero_info.up.up_btn"]
	self.txUpdesc = uiMap["hero_info.up.up_btn.text"]
	self.btnUp5 = uiMap["hero_info.up.up_5_btn"]
	self.txUpdesc5 = uiMap["hero_info.up.up_5_btn.text"]
	self.txElement = uiMap["hero_info.element_bg.element_desc"]
	self.upgrade = uiMap["hero_info.up"]
	self.bgElement = uiMap["hero_info.element_bg"]
	self.vfx04 = uiMap["hero_info.bg_7.scrollrect.viewport.content.vfx_c1_ui_up_b04"]
	self.vfx04:setActive(false)

	self.costCell = uiMap["hero_info.up.reward_cell_1"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	self.txUpdesc:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_15, 1))
	self.txUpdesc5:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_15, 5))

	self.btnUp:addClickListener(function()
		ModuleManager.HeroManager:upgradeHero(self.heroEntity:getCfgId(), self.heroEntity)
	end)
	self.btnUp5:addClickListener(function()
		local canLvUpLv, canLvUp = self.heroEntity:getLvUpLv()
		if not canLvUp then
			return
		end
		ModuleManager.HeroManager:upgradeHero(self.heroEntity:getCfgId(), self.heroEntity, canLvUpLv)
	end)

	self:bind(DataManager.BagData.ItemData, "dirty", function()
		self:refresh()
	end)

	self.scrollrect = uiMap["hero_info.bg_7.scrollrect"]
	self.scrollRectComp = uiMap["hero_info.bg_7.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRectComp:addInitCallback(function()
		return LV_CELL
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.heroEntity, index, self.lvUp)
	end)
    self.scrollRectComp:clearCells()

	self.bg7 = uiMap["hero_info.bg_7"]
end

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

function HeroInfoComp:setHeroData(heroEntity, onlyLook, formationType)
	self.curLevel = heroEntity:getLv()
	self.heroEntity = heroEntity
	self.onlyLook = onlyLook
	self.formationType = formationType
	-- self:bind(self.heroEntity, "isDirty", function()
	-- 	self:refresh(true)
	-- end)
end

function HeroInfoComp:refresh(lvUp)
	self.lvUp = lvUp
	self:refreshLvInfo()
	self:refreshUpInfo()
end

function HeroInfoComp:refreshLvInfo()
	local list = self.heroEntity:getLvAttrPointList()
	self.scrollRectComp:refillCells(#list)

	self.vfx04:setActive(false)
	if self.lvUp then
		local lv = self.heroEntity:getLv()
		for i, v in ipairs(list) do
			if lv == v then
				self.scrollRectComp:moveToIndex(i)
				self.vfx04:setAnchoredPositionY(-34 - (i - 1) * 58)
				self.vfx04:setActive(true)
				break
			end
		end
	end
end

function HeroInfoComp:refreshUpInfo()
	local canLvUp = self.heroEntity:canLvUp()
	if not self.heroEntity:isActived() or self.heroEntity:isMaxLv() then
		self.btnUp:setActive(false)
		self.btnUp5:setActive(false)
		self.costCell:setActive(false)
		self.upgrade:setActive(false)
		local rect = self.baseObject:getRectSize()
		self.bg7:setAnchoredPositionY(150)
		self.bg7:setSizeDeltaY(rect.height - 488)
	else
		self.upgrade:setActive(not self.onlyLook)
		self.btnUp:setActive(true)
		self.btnUp5:setActive(true)
		local rect = self.baseObject:getRectSize()
		self.bg7:setAnchoredPositionY(303)
		self.bg7:setSizeDeltaY(rect.height - 640)

		local cost = {id = self.heroEntity:getLvUpCostId(), num = self.heroEntity:getLvUpMaterialNum() or 0, type = 1}
		local hadNum = DataManager.BagData.ItemData:getItemNumById(cost.id)
		self.costCell:setActive(true)
		self.costCell:refreshByConfig(cost)
		if hadNum >= cost.num then
			self.costCell:showRewardNum(hadNum .. "/" .. cost.num)
		else
			self.costCell:showRewardNum("<color=#D82F2D>" .. hadNum .. "</color>" .. "/" .. cost.num)
		end

		if canLvUp then
			self.btnUp:setTouchEnable(true)
			self.btnUp5:setTouchEnable(true)
			self.btnUp:addRedPoint(100, 36, 1)
			self.btnUp5:addRedPoint(100, 36, 1)
			self.btnUp:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
			self.btnUp5:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[2])
		else
			self.btnUp:setTouchEnable(false)
			self.btnUp5:setTouchEnable(false)
			self.btnUp:removeRedPoint()
			self.btnUp5:removeRedPoint()
			self.btnUp:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[3])
			self.btnUp5:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[3])
		end
	end
end

return HeroInfoComp