local StarInfoComp = class("StarInfoComp", LuaComponent)

local STAR_CELL = "app/ui/hero/cell/star_cell"

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

	self.upBtn = uiMap["star_info.upgrade.up_btn"]
	self.upBtnTx = uiMap["star_info.upgrade.up_btn.text"]
	self.lvBtn = uiMap["star_info.upgrade.lv_btn"]
	self.lvBtnTx = uiMap["star_info.upgrade.lv_btn.text"]
	self.descTx = uiMap["star_info.upgrade.desc_tx"]
	self.costCells = {}
	for i = 1, 2 do
		self.costCells[i] = uiMap["star_info.upgrade.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
	end

	self.currStarImgs = {}
	self.nextStarImgs = {}
	for i = 1, 5 do
		self.currStarImgs[i] = uiMap["star_info.info_node.curr_star_bg.star_img_" .. i]
		self.nextStarImgs[i] = uiMap["star_info.info_node.next_star_bg.star_img_" .. i]
	end
	self.currStarBg = uiMap["star_info.info_node.curr_star_bg"]
	self.nextStarBg = uiMap["star_info.info_node.next_star_bg"]
	self.currLvTx = uiMap["star_info.info_node.curr_lv_tx"]
	self.nextLvTx = uiMap["star_info.info_node.next_lv_tx"]

	-- self.upBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4))
	-- self.descTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4))
	self.upBtnTx:setText("升星")
	self.lvBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_4))
	self.upBtn:addClickListener(function()
		ModuleManager.HeroManager:upgradeHeroStar(self.heroEntity:getCfgId())
	end)

	self.lvBtn:addClickListener(function()
		self.parentUI:changePage(GConst.HeroConst.PANEL_TYPE.HERO)
	end)

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

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

function StarInfoComp:setHeroData(heroEntity, onlyLook)
	self.heroEntity = heroEntity
	self.onlyLook = onlyLook
	self:bind(self.heroEntity, "isDirty", function()
		self:refresh(true)
	end)
end

function StarInfoComp:refresh(starUp)
	self:refreshInfo()
	self:refreshStarBtn()
	self:refreshStarInfo()
end

function StarInfoComp:refreshInfo()
	if self.heroEntity:getIsStarMax() then
		self.currStarBg:setActive(true)
		self.nextStarBg:setActive(false)
		self.currLvTx:setActive(true)
		self.nextLvTx:setActive(false)
		local currStar = self.heroEntity:getStar()
		local starType = math.ceil(currStar / 5)
		for i = 1, 5 do
			local star = (currStar - 1)%5 + 1
			if i <= star then
				self.currStarImgs[i]:setActive(true)
				self.currStarImgs[i]:setSprite(GFunc.getStarImg(starType))
			else
				self.currStarImgs[i]:setActive(false)
			end
		end
		local lv = self.heroEntity:getMaxLv()
		self.currLvTx:setText(lv .. "/" .. lv)
	else
		self.currStarBg:setActive(true)
		self.nextStarBg:setActive(true)
		self.currLvTx:setActive(true)
		self.nextLvTx:setActive(true)
		local currStar = self.heroEntity:getStar()
		local nextStar = currStar + 1
		local starType = math.ceil(currStar / 5)
		local starTypeNext = math.ceil(nextStar / 5)
		for i = 1, 5 do
			local star = (currStar - 1)%5 + 1
			if i <= star then
				self.currStarImgs[i]:setActive(true)
				self.currStarImgs[i]:setSprite(GFunc.getStarImg(starType))
			else
				self.currStarImgs[i]:setActive(false)
			end
			local star2 = (nextStar - 1)%5 + 1
			if i <= star2 then
				self.nextStarImgs[i]:setActive(true)
				self.nextStarImgs[i]:setSprite(GFunc.getStarImg(starTypeNext))
			else
				self.nextStarImgs[i]:setActive(false)
			end
		end
		local lv = self.heroEntity:getLv()
		local maxLv = self.heroEntity:getCurrMaxLv()
		local nextMaxLv = self.heroEntity:getNextMaxLv()
		self.currLvTx:setText(lv .. "/" .. maxLv)
		self.nextLvTx:setText(lv .. "/" .. nextMaxLv)
	end
end

function StarInfoComp:refreshStarBtn()
	if self.heroEntity:getIsStarMax() then
		self.upBtn:setActive(false)
		self.lvBtn:setActive(false)
		self.descTx:setActive(false)
		for i,v in ipairs(self.costCells) do
			v:setActive(false)
		end
	elseif not self.heroEntity:getIsCurLvMax() then
		self.upBtn:setActive(false)
		self.lvBtn:setActive(true)
		self.descTx:setActive(true)
		for i,v in ipairs(self.costCells) do
			v:setActive(false)
		end
		local nextLv = self.heroEntity:getNextLv()
		self.descTx:setText("等级达到" .. nextLv .. "可升星")
	else
		self.upBtn:setActive(true)
		self.lvBtn:setActive(false)
		self.descTx:setActive(false)
		local materials = self.heroEntity:getStarUpMaterials()
		local costs = {}
		costs[1] = {id = self.heroEntity:getFragmentId(), num = materials[1], type = 1}
		costs[2] = {id = self.heroEntity:getStarUpCostId(), num = materials[2], type = 1}
		for i,v in ipairs(self.costCells) do
			v:setActive(true)
			v:refreshByConfig(costs[i])
		end
	end
end

function StarInfoComp:refreshStarInfo()
	local maxStarLv = self.heroEntity:getMaxStar()
	self.scrollRectComp:refillCells(maxStarLv)
end

return StarInfoComp