local IdleQuickDropUI = class("IdleQuickDropUI", BaseUI)

function IdleQuickDropUI:isFullScreen()
    return false
end

function IdleQuickDropUI:getPrefabPath()
    return "assets/prefabs/ui/idle/idle_quick_drop_ui.prefab"
end

function IdleQuickDropUI:ctor()
    self.rewardList = {}
    local rewards = DataManager.IdleData:getIdleQuickDropPreviewRewards()
    for _, item in pairs(rewards) do
        if item.num > 0 then
            table.insert(self.rewardList, item)
        end
    end
    if #self.rewardList > 1 then
        local cfg = ConfigManager:getConfig("item")
        local infoA
        local infoB
        table.sort(self.rewardList, function(a, b)
            infoA = cfg[a.id]
            infoB = cfg[b.id]
            if infoA.type == infoB.type then
                if infoA.qlt == infoB.qlt then
                    return infoA.qlt > infoB.qlt
                else
                    return a.id < b.id
                end
            else
                return infoA.type < infoB.type
            end
        end)
    end
end

function IdleQuickDropUI:onReshow()
    -- 检查是否升级
    if DataManager.PlayerData:getIfCanLevelUp() then
        ModuleManager.PlayerManager:levelUp()
    end
end

function IdleQuickDropUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.uiMap = uiMap
    uiMap["idle_quick_drop_ui.bg.close_btn"]:addClickListener(function()
        self:closeUI()
    end)
    uiMap["idle_quick_drop_ui.bg.title_text"]:setText(I18N:getGlobalText(I18N.GlobalConst.IDLE_QUICK))
    local quickIdleTime = DataManager.IdleData:getQuickIdleTime()
    uiMap["idle_quick_drop_ui.bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.IDLE_DROP_DESC_3, quickIdleTime // 3600))

    self:initRewards()
    self:initBtns()
    self:bindData()
end

function IdleQuickDropUI:initRewards()
    self.scrollRect = self.uiMap["idle_quick_drop_ui.bg.scroll_rect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
		return GConst.TYPEOF_LUA_CLASS.REWARD_CELL
	end)
	self.scrollRect:addRefreshCallback(function(index, cell)
		cell:refreshByConfig(self.rewardList[index])
	end)
    self.scrollRect:clearCells()
    self.scrollRect:refillCells(#self.rewardList)
end

function IdleQuickDropUI:initBtns()
    self.adBtn = self.uiMap["idle_quick_drop_ui.bg.ad_btn"]
    self.adBtn:addClickListener(function()
        ModuleManager.IdleManager:getIdleQuickRewrad(true)
    end)
    self.uiMap["idle_quick_drop_ui.bg.ad_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.STR_FREE))
    self.itemBtn = self.uiMap["idle_quick_drop_ui.bg.item_btn"]
    self.itemBtn:addClickListener(function()
        ModuleManager.IdleManager:getIdleQuickRewrad(false)
    end)

    local cost = DataManager.IdleData:getQuickIdleCost()
    local itemBtnIcon = self.uiMap["idle_quick_drop_ui.bg.item_btn.icon"]
    local itemBtnTx = self.uiMap["idle_quick_drop_ui.bg.item_btn.text"]
    local iconName = ConfigManager:getConfig("item")[cost.id].icon
    itemBtnIcon:setSprite(GConst.ATLAS_PATH.ICON_ITEM, iconName)
    itemBtnTx:setText(tostring(cost.num))
    GFunc.centerImgAndTx(itemBtnIcon, itemBtnTx)

    self.descTx2 = self.uiMap["idle_quick_drop_ui.bg.desc_tx_2"]
    self.descTx3 = self.uiMap["idle_quick_drop_ui.bg.desc_tx_3"]
end

function IdleQuickDropUI:bindData()
    self:bind(DataManager.IdleData, "adCount", function()
        self:onRefresh()
    end)
    self:bind(DataManager.IdleData, "energyCount", function()
        self:onRefresh()
    end)

    local cost = DataManager.IdleData:getQuickIdleCost()
    local costId = GFunc.getRewardId(cost)
    local itemEntity = DataManager.BagData.ItemData:getItemById(costId)
    if itemEntity then
        self:bind(itemEntity, "num", function()
            self:onRefresh()
        end)
    end
end

function IdleQuickDropUI:onRefresh()
    local adRemainTimes = DataManager.IdleData:getQuickIdleAdRemainTimes()
    if adRemainTimes <= 0 then
        adRemainTimes = 0
        self.adBtn:setTouchEnable(false)
        self.adBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_2")
        self.adBtn:removeRedPoint()
    else
        self.adBtn:setTouchEnable(true)
        self.adBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_blue_2")
        self.adBtn:addRedPoint(121, 42, 0.8)
    end
    self.descTx2:setText(I18N:getGlobalText(I18N.GlobalConst.TODAY_REMAIN_TIMES, adRemainTimes))
    local vitRemainTimes = DataManager.IdleData:getQuickIdleVitRemainTimes()
    if vitRemainTimes <= 0 then
        vitRemainTimes = 0
        self.itemBtn:setTouchEnable(false)
        self.itemBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_2")
        self.itemBtn:removeRedPoint()
    else
        self.itemBtn:setTouchEnable(true)
        self.itemBtn:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_2")
        local cost = DataManager.IdleData:getQuickIdleCost()
        local costId = GFunc.getRewardId(cost)
        local costNum = GFunc.getRewardNum(cost)
        if GFunc.checkCost(costId, costNum) then
            self.itemBtn:addRedPoint(121, 42, 0.8)
        else
            self.itemBtn:removeRedPoint()
        end
    end
    self.descTx3:setText(I18N:getGlobalText(I18N.GlobalConst.TODAY_REMAIN_TIMES, vitRemainTimes))
end

return IdleQuickDropUI
