local ActivityBountyComp = class("ActivityBountyComp", LuaComponent)

function ActivityBountyComp:init()
    local uiMap = self:getUIMap()
    self.txScore = uiMap["bounty_panel.bounty.tx_score"]
    self.imgProg = uiMap["bounty_panel.bounty.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.txProg = uiMap["bounty_panel.bounty.prog.tx_prog"]
    self.txLv = uiMap["bounty_panel.bounty.tx_lv"]
    self.txLvNum = uiMap["bounty_panel.bounty.tx_lv_num"]
    self.txFree = uiMap["bounty_panel.title.free.tx_free"]
    -- 档位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.progLevel = uiMap["bounty_panel.list_bounty.viewport.content.prog"]
    self.imgProgLevel = uiMap["bounty_panel.list_bounty.viewport.content.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    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.txScore:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_1))
    self.txLv:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_12))
    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.bountyList = DataManager.ActivityData:getBountyCfg()
	self.scrollrectComp:addInitCallback(function()
        self.maskLevel:getTransform():SetAsLastSibling()
		self.lineLevel:getTransform():SetAsLastSibling()
		return "app/ui/activity/cell/activity_bounty_cell"
	end)
	self.scrollrectComp:addRefreshCallback(function(index, cell)
		cell:refresh(index, self.bountyList[index])
	end)
    self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(#self.bountyList)

    self.btnBuy1:addClickListener(function()
        UIManager:showUI("app/ui/activity/activity_bounty_buy_ui", GConst.ActivityConst.BOUNTY_GRADE_TYPE.PAY1)
    end)
    self.btnBuy2:addClickListener(function()
        UIManager:showUI("app/ui/activity/activity_bounty_buy_ui", GConst.ActivityConst.BOUNTY_GRADE_TYPE.PAY2)
    end)
    self.btnBuyLevel:addClickListener(function()
        ModuleManager.ActivityManager:reqBuyBountyLevel()
    end)
end

function ActivityBountyComp:refresh()
    self.btnBuyLevel:setActive(DataManager.ActivityData:canBuyBountyLevel())
    -- 战令积分显示
    local total = DataManager.ActivityData:getBountyUpgradeScore()
    local cur = DataManager.ActivityData:getBountyLevelScore()
    if total and total > 0 then
        self.imgProg.value = cur / total
        self.txProg:setText(cur .. "/" .. total)
    else
        self.imgProg.value = 1
        self.txProg:setText(I18N:getGlobalText(I18N.GlobalConst.STR_MAX))
    end
    self.txLvNum:setText(DataManager.ActivityData:getBountyLevel())

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

    local gift1 = DataManager.ActivityData:getBountyGradeGiftCfg(GConst.ActivityConst.BOUNTY_GRADE_TYPE.PAY1)
    local gift2 = DataManager.ActivityData:getBountyGradeGiftCfg(GConst.ActivityConst.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.ActivityData:getBuyBountyLevelCost()))
    GFunc.centerImgAndTx(self.imgBuyLevelIcon, self.txBuyLevelNum, 5)

    -- 等级列表
	self.scrollrectComp:updateAllCell()

    local maxLevel = DataManager.ActivityData:getBountyMaxLevel()
    local curLevel = DataManager.ActivityData:getBountyLevel()
    local topRecoveryOffset = self.scrollrectComp:getTopRecoveryOffset()
	local downRecoveryOffset = self.scrollrectComp:getDownRecoveryOffset()
	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.progLevel:setAnchoredPositionY((topRecoveryOffset + downRecoveryOffset) / 2 - cellHeight / 2 - 10)
	self.progLevel:setSizeDeltaY(cellHeight * (maxLevel - 1))
	local percent = (curLevel - 1) / (maxLevel - 1)
	if percent < 0 then
		percent = 0
	end
	self.imgProgLevel.value = percent
	self.maskLevel:setAnchoredPositionY(-topRecoveryOffset - curLevel * cellHeight)
	self.maskLevel:setSizeDeltaY(cellHeight * maxLevel + GConst.UI_SCREEN_HEIGHT)
end

return ActivityBountyComp