local FourteenDayBountyUI = class("FourteenDayBountyUI", BaseUI)

function FourteenDayBountyUI:isFullScreen()
    return false
end

function FourteenDayBountyUI:getPrefabPath()
    return "assets/prefabs/ui/activity/fourteen_day/fourteen_day_bounty_ui.prefab"
end

function FourteenDayBountyUI:onPressBackspace()
	self:closeUI()
end

function FourteenDayBountyUI:onClose()
    if self.countdownSid then
		self:unscheduleGlobal(self.countdownSid)
	end
end

function FourteenDayBountyUI:ctor()
end

function FourteenDayBountyUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.txTitle = uiMap["fourteen_day_bounty_ui.content.title.tx_title"]
    self.btnClose = uiMap["fourteen_day_bounty_ui.content.btn_close"]
    self.scrollrectComp = uiMap["fourteen_day_bounty_ui.content.list_bounty"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.prog = uiMap["fourteen_day_bounty_ui.content.list_bounty.viewport.content.prog"]
    self.imgProg = uiMap["fourteen_day_bounty_ui.content.list_bounty.viewport.content.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    self.imgMask = uiMap["fourteen_day_bounty_ui.content.list_bounty.viewport.content.mask_img"]
    self.imgLine = uiMap["fourteen_day_bounty_ui.content.list_bounty.viewport.content.line"]
    -- 购买
    self.btnBuy = uiMap["fourteen_day_bounty_ui.content.btn_buy"]
    self.txBuy = uiMap["fourteen_day_bounty_ui.content.btn_buy.tx_buy"]
    self.txTips = uiMap["fourteen_day_bounty_ui.content.tx_tips"]
    self.txCountdown = uiMap["fourteen_day_bounty_ui.content.countdown.tx_countdown"]
    self.bought = uiMap["fourteen_day_bounty_ui.content.bought"]
    self.txBought = uiMap["fourteen_day_bounty_ui.content.bought.tx_bought"]

    self.bountyList = DataManager.FourteenDayData:getBountyCfg()

    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_TITLE_1))
    self.txTips:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_DESC_5))
    self.txBought:setText(I18N:getGlobalText(I18N.GlobalConst.ALREADY_ACTIVE))
    self.scrollrectComp:addInitCallback(function()
        self.imgMask:getTransform():SetAsLastSibling()
		self.imgLine:getTransform():SetAsLastSibling()
		return "app/ui/activity/fourteen_day/cell/fourteen_day_bounty_cell"
	end)
	self.scrollrectComp:addRefreshCallback(function(index, cell)
		cell:refresh(index)
	end)
    self.scrollrectComp:clearCells()
	self.scrollrectComp:refillCells(#self.bountyList)

    self.btnBuy:addClickListener(function()
        ModuleManager.FourteenDayManager:buyBounty()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:bind(DataManager.FourteenDayData, "isDirty", function()
        self:onRefresh()
	end)
    self:bind(DataManager.ShopData, "isDirty", function()
        self:refreshBuy()
        self:refreshBountyList()
	end)
end

function FourteenDayBountyUI:onRefresh()
    self:refreshBuy()
    self:refreshBountyList()
    self:refreshCountdown()
end

-- 刷新倒计时
function FourteenDayBountyUI:refreshCountdown()
    if self.countdownSid then
		self:unscheduleGlobal(self.countdownSid)
	end
	self.countdownSid = self:scheduleGlobal(function()
        local time = DataManager.FourteenDayData:getEndRemainTime()
        if time < 0 then
            self:closeUI()
            return
        end
        self.txCountdown:setText(Time:formatNumTimeStr(time))
	end, 1)
    self.txCountdown:setText(Time:formatNumTimeStr(DataManager.FourteenDayData:getEndRemainTime()))
end

-- 刷新购买
function FourteenDayBountyUI:refreshBuy()
    if not DataManager.FourteenDayData:isBountyGradeUnlock(GConst.FourteenDayConst.BOUNTY_GRADE_TYPE.PAY) then
        self.bought:setActive(false)
        self.btnBuy:setActive(true)
        local gift = DataManager.ShopData:getActGiftConfig()[GConst.FourteenDayConst.BOUNTY_GIFT_ID]
        self.txBuy:setText(GFunc.getFormatPrice(gift.recharge_id))
    else
        self.bought:setActive(true)
        self.btnBuy:setActive(false)
    end
end

-- 刷新战令列表
function FourteenDayBountyUI:refreshBountyList()
    self.scrollrectComp:updateAllCell()
    local maxLevel = #self.bountyList
    local curLevel = DataManager.FourteenDayData:getActiveDays()
    local topRecoveryOffset = self.scrollrectComp:getTopRecoveryOffset()
	local downRecoveryOffset = self.scrollrectComp:getDownRecoveryOffset()
	local cellHeight = self.scrollrectComp:getCellHeight()
	if curLevel >= maxLevel then
		self.imgLine:setVisible(false)
	else
		self.imgLine:setVisible(true)
		self.imgLine:setAnchoredPositionY(-topRecoveryOffset - curLevel * cellHeight)
	end
	self.prog:setAnchoredPositionY((topRecoveryOffset + downRecoveryOffset) / 2 - cellHeight / 2 - 10)
	self.prog:setSizeDeltaY(cellHeight * (maxLevel - 1))
	local percent = (curLevel - 1) / (maxLevel - 1)
	if percent < 0 then
		percent = 0
	end
	self.imgProg.value = percent
	self.imgMask:setAnchoredPositionY(-topRecoveryOffset - curLevel * cellHeight)
	self.imgMask:setSizeDeltaY(cellHeight * maxLevel + GConst.UI_SCREEN_HEIGHT)
end

return FourteenDayBountyUI