local TournArenaBountyComp = class("TournArenaBountyComp", LuaComponent)

function TournArenaBountyComp:init()
    local uiMap = self:getUIMap()
    self.txDesc = uiMap["bounty_panel.tx_desc"]
    self.txFree = uiMap["bounty_panel.title.free.tx_free"]
    self.txCurScore = uiMap["bounty_panel.title.cur_level.tx_level"]
    -- 档位1
    self.txBuyed1 = uiMap["bounty_panel.title.buy1.tx_buyed"]
    self.btnBuy1 = uiMap["bounty_panel.title.buy1.btn_buy"]
    self.txBuy1 = uiMap["bounty_panel.title.buy1.btn_buy.tx_buy"]
    -- 档位2
    self.txBuyed2 = uiMap["bounty_panel.title.buy2.tx_buyed"]
    self.btnBuy2 = uiMap["bounty_panel.title.buy2.btn_buy"]
    self.txBuy2 = uiMap["bounty_panel.title.buy2.btn_buy.tx_buy"]

    self.scrollrectComp = uiMap["bounty_panel.list_bounty"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.maskLevel = uiMap["bounty_panel.list_bounty.viewport.content.mask_img"]
    self.lineLevel = uiMap["bounty_panel.list_bounty.viewport.content.line"]
    self.btnBuyLevel = uiMap["bounty_panel.list_bounty.viewport.content.line.btn_buy_level"]
    self.txBuyLevelNum = uiMap["bounty_panel.list_bounty.viewport.content.line.btn_buy_level.tx_num"]
    self.imgBuyLevelIcon = uiMap["bounty_panel.list_bounty.viewport.content.line.btn_buy_level.icon"]

    self.txDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_TOURN_ARENEA_DESC_1))
    self.txFree:setText(I18N:getGlobalText(I18N.GlobalConst.STR_FREE))
    self.txBuyed1:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20))
    self.txBuyed2:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20))

    self.bountyIdList = DataManager.TournArenaData:getBountyIdList()
	self.scrollrectComp:addInitCallback(function()
        self.maskLevel:getTransform():SetAsLastSibling()
		self.lineLevel:getTransform():SetAsLastSibling()
		return "app/ui/activity/tourn_arena/cell/tourn_arena_bounty_cell"
	end)
	self.scrollrectComp:addRefreshCallback(function(index, cell)
		cell:refresh(index, self.bountyIdList[index])
	end)
    self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(DataManager.TournArenaData:getBountyMaxLevel())

    self.btnBuy1:addClickListener(function()
        ModuleManager.TournArenaManager:buyBounty(GConst.TournArenaConst.BOUNTY_GIFT_ID_1)
    end)
    self.btnBuy2:addClickListener(function()
        ModuleManager.TournArenaManager:buyBounty(GConst.TournArenaConst.BOUNTY_GIFT_ID_2)
    end)
    self.btnBuyLevel:addClickListener(function()
        ModuleManager.TournArenaManager:reqBuyBountyLevel()
    end)
    self:bind(DataManager.TournArenaData, "isDirty", function()
        self:refresh(true)
	end)
    self:bind(DataManager.ShopData, "isDirty", function()
        self:refresh(true)
	end)
end

function TournArenaBountyComp:refresh(notUpdatePos)
    self.btnBuyLevel:setActive(DataManager.TournArenaData:canBuyBountyLevel())
    self.txCurScore:setText(DataManager.TournArenaData:getBountyTotalScore())

    -- 档位展示
    self.btnBuy1:setActive(not DataManager.TournArenaData:isBountyGradeUnlock(GConst.TournArenaConst.BOUNTY_GRADE_TYPE.PAY1))
    self.btnBuy2:setActive(not DataManager.TournArenaData:isBountyGradeUnlock(GConst.TournArenaConst.BOUNTY_GRADE_TYPE.PAY2))

    local gift1 = DataManager.TournArenaData:getBountyGradeGiftCfg(GConst.TournArenaConst.BOUNTY_GRADE_TYPE.PAY1)
    local gift2 = DataManager.TournArenaData:getBountyGradeGiftCfg(GConst.TournArenaConst.BOUNTY_GRADE_TYPE.PAY2)
    self.txBuy1:setText(GFunc.getFormatPrice(gift1.recharge_id))
    self.txBuy2:setText(GFunc.getFormatPrice(gift2.recharge_id))
    self.txBuyLevelNum:setText(GFunc.getRewardNum(DataManager.TournArenaData:getBuyBountyLevelCost()))
    GFunc.centerImgAndTx(self.imgBuyLevelIcon, self.txBuyLevelNum, 5)

    -- 等级列表
    local maxLevel = DataManager.TournArenaData:getBountyMaxLevel()
    local curLevel = DataManager.TournArenaData:getBountyLevel()
    local topRecoveryOffset = self.scrollrectComp:getTopRecoveryOffset()
	local cellHeight = self.scrollrectComp:getCellHeight()
	if curLevel >= maxLevel then
		self.lineLevel:setVisible(false)
	else
		self.lineLevel:setVisible(true)
		self.lineLevel:setAnchoredPositionY(-topRecoveryOffset - curLevel * cellHeight)
	end
	self.maskLevel:setAnchoredPositionY(-topRecoveryOffset - curLevel * cellHeight)
	self.maskLevel:setSizeDeltaY(cellHeight * maxLevel + GConst.UI_SCREEN_HEIGHT)
    self.scrollrectComp:updateAllCell()
    if not notUpdatePos then
        local rewardLevel = DataManager.TournArenaData:getFirstCanGetBountyRewardLevel()
        self.scrollrectComp:moveToIndex(rewardLevel or curLevel)
    end
end

return TournArenaBountyComp