local ActPvpBountyUI = class("ActPvpBountyUI", BaseUI)

local BOUNTY_CELL = "app/ui/activity/act_pvp/cell/bounty_cell"
local BTN_ICON = {"common_btn_green_3", "act_common_board_6"}

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

function ActPvpBountyUI:isFullScreen()
    return false
end

function ActPvpBountyUI:ctor()
    self.rewardList = DataManager.ActPvpData:getBountyList()
end

function ActPvpBountyUI:getPrefabPath()
    return "assets/prefabs/ui/activity/act_pvp/act_pvp_bounty_ui.prefab"
end

function ActPvpBountyUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
    self:_bind()

    self:refreshTime()
    if self.actCountdownSid then
        self:unscheduleGlobal(self.actCountdownSid)
    end
    self.actCountdownSid = self:scheduleGlobal(function()
        self:refreshTime()
	end, 1)
end

function ActPvpBountyUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["act_pvp_bounty_ui.bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_4))
    uiMap["act_pvp_bounty_ui.bg.scrollrect.viewport.content.mask_img.line.btn_buy_level.tx_num"]:setText(GFunc.getRewardNum(DataManager.ActPvpData:getBuyBountyLevelCost()))
    self.downDesc = uiMap["act_pvp_bounty_ui.bg.bg.desc"]
    self.downDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_16))

    self:refreshBtns()
    self:_refreshScrollrect()
    self:refreshLevelInfo()
end

function ActPvpBountyUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["act_pvp_bounty_ui.bg.close_btn"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["act_pvp_bounty_ui.bg.scrollrect.viewport.content.mask_img.line.btn_buy_level"]:addClickListener(function()
        ModuleManager.ActPvpManager:reqBuyBountyLevel()
    end)
end

function ActPvpBountyUI:_bind()
    self:bind(DataManager.ActPvpData, "isDirty", function()
        self:_display()
    end)

    self:bind(DataManager.ShopData, "isDirty", function()
        self:_display()
    end)
end

function ActPvpBountyUI:refreshBtns()
    local uiMap = self.root:genAllChildren()
    uiMap["act_pvp_bounty_ui.bg.free.tx_free"]:setText(I18N:getGlobalText(I18N.GlobalConst.STR_FREE))

    local probtn = uiMap["act_pvp_bounty_ui.bg.buy1"]
    local proBtntx = uiMap["act_pvp_bounty_ui.bg.buy1.tx_buyed"]
    local proBuy = uiMap["act_pvp_bounty_ui.bg.buy1.tx_buy"]
    proBtntx:setText(I18N:getGlobalText(I18N.GlobalConst.ALREADY_ACTIVE))
    proBuy:setText(DataManager.ActPvpData:getProPrice())

    if DataManager.ActPvpData:getProBought() then
        probtn:setSprite(GConst.ATLAS_PATH.ACT_COMMON, BTN_ICON[2])
        probtn:setTouchEnable(false)
        proBtntx:setVisible(true)
        proBuy:setVisible(false)
    else
        probtn:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
        probtn:setTouchEnable(true)
        probtn:addClickListener(function()
            if not DataManager.ActPvpData:getIsOpen() then
                GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_OVER_EDSC))
                return
            end
        
            PayManager:purchasePackage(DataManager.ActPvpData:getProActId(), PayManager.PURCHARSE_TYPE.ACT_GIFT)
        end)
        proBtntx:setVisible(false)
        proBuy:setVisible(true)
    end

    local utralbtn = uiMap["act_pvp_bounty_ui.bg.buy2"]
    local utralBtntx = uiMap["act_pvp_bounty_ui.bg.buy2.tx_buyed"]
    local utralBuy = uiMap["act_pvp_bounty_ui.bg.buy2.tx_buy"]
    utralBtntx:setText(I18N:getGlobalText(I18N.GlobalConst.ALREADY_ACTIVE))
    utralBuy:setText(DataManager.ActPvpData:gettUtralPrice())

    if DataManager.ActPvpData:getUtralBought() then
        utralbtn:setSprite(GConst.ATLAS_PATH.ACT_COMMON, BTN_ICON[2])
        utralbtn:setTouchEnable(false)
        utralBtntx:setVisible(true)
        utralBuy:setVisible(false)
    else
        utralbtn:setSprite(GConst.ATLAS_PATH.COMMON, BTN_ICON[1])
        utralbtn:setTouchEnable(true)
        utralbtn:addClickListener(function()
            if not DataManager.ActPvpData:getIsOpen() then
                GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_OVER_EDSC))
                return
            end
        
            PayManager:purchasePackage(DataManager.ActPvpData:getUtralActId(), PayManager.PURCHARSE_TYPE.ACT_GIFT)
        end)
        utralBtntx:setVisible(false)
        utralBuy:setVisible(true)
    end
end

function ActPvpBountyUI:_refreshScrollrect()
    local uiMap = self.root:genAllChildren()
    local mask = uiMap["act_pvp_bounty_ui.bg.scrollrect.viewport.content.mask_img"]
    local level = DataManager.ActPvpData:getBountyLevel()
    local totalCount = #self.rewardList
    local maskCount = math.max(0, totalCount - level)
    mask:setAnchoredPositionY(-10 - level * 120)
    mask:setSizeDeltaY(maskCount * 120 + 20)
    local progSlider = uiMap["act_pvp_bounty_ui.bg.scrollrect.viewport.content.prog.img_prog"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    local y = (totalCount - 1) * 120
    if (level - 1) >= 0 then
        progSlider.value = (level - 1) / (totalCount - 1)
    else
        progSlider.value = 0
    end
    mask:setVisible(level < totalCount)

    if self.scrollRectComp then
        self.scrollRectComp:updateAllCell()
        return
    end

    local prog = uiMap["act_pvp_bounty_ui.bg.scrollrect.viewport.content.prog"]
    prog:setSizeDeltaY(y)
    self.scrollRectComp = uiMap["act_pvp_bounty_ui.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRectComp:addInitCallback(function()
		return BOUNTY_CELL
	end)
	self.scrollRectComp:addRefreshCallback(function(index, cell)
		cell:refresh(self.rewardList[index])
        mask:getTransform():SetAsLastSibling()
	end)
    self.scrollRectComp:clearCells()
    self.scrollRectComp:setTotalCount(0)
    if level < 1 then
        level = 1
    end
    self.scrollRectComp:refillCells(totalCount, nil, level)
end

function ActPvpBountyUI:refreshTime()
    local uiMap = self.root:genAllChildren()
    local remainTime = DataManager.ActPvpData:getRemainTime()
    local str
    if remainTime <= 0 then
        str = I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_OVER_EDSC)
    else
        str = Time:formatNumTimeStr(remainTime)
    end
    uiMap["act_pvp_bounty_ui.bg.countdown.tx_countdown"]:setText(str)
    uiMap["act_pvp_bounty_ui.bg.scrollrect.viewport.content.mask_img.line.btn_buy_level"]:setActive(remainTime < 86400)
end

function ActPvpBountyUI:refreshLevelInfo()
    local uiMap = self.root:genAllChildren()
    local lv = DataManager.ActPvpData:getBountyLevel()
    uiMap["act_pvp_bounty_ui.bg.bg.point_bg.desc_lv_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_12))
    uiMap["act_pvp_bounty_ui.bg.bg.point_bg.desc_lv"]:setText(lv)
    uiMap["act_pvp_bounty_ui.bg.bg.point_bg.desc_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_15))
    local needExp = DataManager.ActPvpData:getBountyLevelExp(lv)
    local txSlider = uiMap["act_pvp_bounty_ui.bg.bg.point_bg.desc_slider"]
    local slider = uiMap["act_pvp_bounty_ui.bg.bg.point_bg.slider_bg.slider"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    if needExp <= 0 then -- 满级了
        txSlider:setText(I18N:getGlobalText(I18N.GlobalConst.STR_MAX))
        slider.value = 1
    else
        local exp = DataManager.ActPvpData:getBountyExp()
        txSlider:setText(exp .. "/" .. needExp)
        slider.value = exp / needExp
    end
end

return ActPvpBountyUI