local HeroFundUI = class("HeroFundUI", BaseUI)

local FUND_CELL = "app/ui/activity/hero_fund/cell/fund_cell"
local BTN_ICON = {"common_btn_green_3", "act_herofund_board_2"}

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

function HeroFundUI:isFullScreen()
    return false
end

function HeroFundUI:ctor()
    self.rewardList = {}
    for id, info in pairs(DataManager.HeroFundData:getConfig()) do
        table.insert(self.rewardList, id)
    end
    table.sort(self.rewardList, function(a, b)
        return a < b
    end)
end

function HeroFundUI:getPrefabPath()
    return "assets/prefabs/ui/activity/hero_fund/activity_herofund_ui.prefab"
end

function HeroFundUI: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 HeroFundUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["activity_herofund_ui.bg.bg.title"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_FUND_DESCC_2))
    uiMap["activity_herofund_ui.bg.ui_spine_obj"]:playAnim("idle", true, false)
    uiMap["activity_herofund_ui.bg.level.tx_level"]:setText(DataManager.HeroFundData:getWaveCount())
    self.downDesc = uiMap["activity_herofund_ui.bg.bg.desc"]
    self.downDesc:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_FUND_DESCC_3))
    self.toActivityTx = uiMap["activity_herofund_ui.bg.bg.to_activity_tx"]
    self.toActivityTx:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_FUND_DESCC_4))
    self.toActivityBtn = uiMap["activity_herofund_ui.bg.bg.to_activity_btn"]
    self.toActivityBtnTx = uiMap["activity_herofund_ui.bg.bg.to_activity_btn.text"]
    self.toActivityBtnTx:setText(I18N:getGlobalText(I18N.GlobalConst.PART_IN_DESC))
    self.toActivityBtn:addClickListener(function()
        self:closeUI()
        ModuleManager.FourteenDayManager:showFourteenDayUI()
    end)

    self:refreshBtns()
    self:_refreshScrollrect()
end

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

function HeroFundUI:_bind()
    self:bind(DataManager.HeroFundData, "isDirty", function()
        self:_display()
    end)

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

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

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

    if DataManager.HeroFundData:getProBought() then
        probtn:setSprite(GConst.ATLAS_PATH.ACT_HEROFUND, 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()
            ModuleManager.HeroFundManager:purcharse(DataManager.HeroFundData:getProActId())
        end)
        proBtntx:setVisible(false)
        proBuy:setVisible(true)
    end

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

    if DataManager.HeroFundData:getUtralBought() then
        utralbtn:setSprite(GConst.ATLAS_PATH.ACT_HEROFUND, 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()
            ModuleManager.HeroFundManager:purcharse(DataManager.HeroFundData:getUtralActId())
        end)
        utralBtntx:setVisible(false)
        utralBuy:setVisible(true)
    end
end

function HeroFundUI:_refreshScrollrect()
    local uiMap = self.root:genAllChildren()
    local mask = uiMap["activity_herofund_ui.bg.scrollrect.viewport.content.mask_img"]
    local level = DataManager.HeroFundData:getWaveLevel()
    local totalCount = #self.rewardList
    local maskCount = math.max(0, totalCount - level)
    mask:setAnchoredPositionY(-10 - level * 120)
    mask:setSizeDeltaY(maskCount * 120 + 20)
    local progSlider = uiMap["activity_herofund_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["activity_herofund_ui.bg.scrollrect.viewport.content.prog"]
    prog:setSizeDeltaY(y)
    self.scrollRectComp = uiMap["activity_herofund_ui.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRectComp:addInitCallback(function()
		return FUND_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 HeroFundUI:refreshTime()
    local uiMap = self.root:genAllChildren()
    local remainTime = DataManager.HeroFundData:getRemainTime()
    local str
    if remainTime <= 0 then
        str = I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_OVER_EDSC)
    else
        str = Time:formatNumTimeStr(remainTime)
        if DataManager.FourteenDayData:getOpenStatus() then -- 14天活动开了就显示
            self.toActivityTx:setVisible(true)
            self.toActivityBtn:setActive(true)
            self.downDesc:setVisible(false)
        else
            self.toActivityTx:setVisible(false)
            self.toActivityBtn:setActive(false)
            self.downDesc:setVisible(true)
        end
    end
    uiMap["activity_herofund_ui.bg.countdown.tx_countdown"]:setText(str)
end

return HeroFundUI