local MopUpUI = class("MopUpUI", BaseUI)
local MAX_SCROLL_SHOW_COUNT = 10

function MopUpUI:isFullScreen()
    return false
end

function MopUpUI:getPrefabPath()
    return "assets/prefabs/ui/common/mop_up_ui.prefab"
end

function MopUpUI:onClose()
    if self.animRewards then
        for idx, anim in pairs(self.animRewards) do
            if anim then
                anim:Kill()
            end
        end
    end
end

function MopUpUI:ctor(params)
    self.actType = params.actType
    self.customtitleTx = params.customtitleTx
    self.rewards = params.rewards
    self.remainCount = params.remainCount
    self.callback = params.callback

    ---- 有目标的扫荡
    self.target = params.target
end

function MopUpUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()

    -- 检查礼包弹出
    if self.actType == ModuleManager.MODULE_KEY.DUNGEON_WEAPON then
        DataManager.ShopData:checkPopGift(PayManager.PURCHARSE_ACT_TYPE.WEAPON_GIFT)
    elseif self.actType == ModuleManager.MODULE_KEY.DUNGEON_ARMOR then
        DataManager.ShopData:checkPopGift(PayManager.PURCHARSE_ACT_TYPE.ARMOR_GIFT)
    end
end

function MopUpUI:_display()
    local uiMap = self.root:genAllChildren()
    self.btnOk = uiMap["mop_up_ui.bg.ok_btn"]
    self.btnClose = uiMap["mop_up_ui.bg.close_btn"]

    uiMap["mop_up_ui.bg.title_text"]:setText(self.customtitleTx or I18N:getGlobalText(I18N.GlobalConst.MOP_UP_DESC_1))
    uiMap["mop_up_ui.bg.tx_none"]:setText(I18N:getGlobalText(I18N.GlobalConst.GET_REWARDS_DESC))
    uiMap["mop_up_ui.bg.ok_btn.text"]:setText(I18N:getGlobalText(I18N.GlobalConst.MOP_UP_DESC_2))

    local remianTxObj = uiMap["mop_up_ui.bg.remain_tx"]
    if self.remainCount then
        remianTxObj:setVisible(true)
        remianTxObj:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_21, self.remainCount))
    else
        remianTxObj:setVisible(false)
    end

    self:refreshTarget()
    self:refreshScrollrect()
end

function MopUpUI:_addListeners()
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)

    self.btnOk:addClickListener(function()
        self:closeUI()
        if self.callback then
            self.callback()
        end
    end)
end

function MopUpUI:refreshScrollrect()
    self.animRewards = {}
    if not self.scrollRect then
        local uiMap = self.root:genAllChildren()
        self.scrollRect = uiMap["mop_up_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)
            if index <= MAX_SCROLL_SHOW_COUNT and self.animRewards[index] == nil then
                self.animRewards[index] = self:showRewardAppearAnim(index, cell)
            end
            cell:refresh(self.rewards[index])
        end)
    end
    self.scrollRect:refillCells(#self.rewards)
end

function MopUpUI:refreshTarget()
    local uiMap = self.root:genAllChildren()
    local bg = uiMap["mop_up_ui.bg"]
    local node = uiMap["mop_up_ui.bg.item_node"]
    if not self.target then
        node:setVisible(false)
        bg:setSizeDeltaY(518)
        return
    end
    bg:setSizeDeltaY(632)
    node:setVisible(true)

    if not self.rewardCell then
        self.rewardCell = CellManager:addCellComp(uiMap["mop_up_ui.bg.item_node.reward_cell"], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    end

    self.rewardCell:refreshItemById(self.target.id)
    self.rewardCell:hideCountTx()

    local curProgress = DataManager.BagData.ItemData:getItemNumById(self.target.id) or 0
    local itemNameTx = GFunc.getRewardName(GConst.REWARD_TYPE.ITEM, self.target.id)
    local progressTx
    if curProgress >= self.target.value then
        progressTx = string.format("<color=#49FF49>%s</color>/%s", curProgress, self.target.value)
    else
        itemNameTx = itemNameTx .. "<color=#FF4949>(" .. I18N:getGlobalText(I18N.GlobalConst.MOP_UP_DESC_3) .. ")</color>"
        progressTx = string.format("<color=#FF4949>%s</color>/%s", curProgress, self.target.value)
    end
    uiMap["mop_up_ui.bg.item_node.desc_1"]:setText(itemNameTx)
    uiMap["mop_up_ui.bg.item_node.desc_2"]:setText(progressTx)
    uiMap["mop_up_ui.bg.item_node.progress_slider"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER).value = curProgress / self.target.value
end


-- 展示结算奖励的出现动画
function MopUpUI:showRewardAppearAnim(idx, cell)
    local canvasGroup = cell.baseObject:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)
	local selfObj = cell.baseObject
	local delay = (idx - 1) * 0.05
	local scaleX = selfObj:fastGetLocalScale()

    cell:setTouchEnable(false)
    self.btnOk:setTouchEnable(false)
    self.btnClose:setTouchEnable(false)
	local animRewardAppear = selfObj:createBindTweenSequence()
    animRewardAppear:Insert(0, canvasGroup:DOFade(0, 0))
    animRewardAppear:Insert(0, selfObj:getTransform():DOScale(scaleX * 0.6, 0))
    animRewardAppear:Insert(0.3 + delay, selfObj:getTransform():DOScale(scaleX * 1.1, 0.1))
    animRewardAppear:Insert(0.3 + delay, canvasGroup:DOFade(1, 0.1))
    animRewardAppear:Insert(0.4 + delay, selfObj:getTransform():DOScale(scaleX * 1, 0.13))
    animRewardAppear:OnComplete(function()
        animRewardAppear = nil
        cell:setTouchEnable(true)
        self:performWithDelayGlobal(function()
            self.btnOk:setTouchEnable(true)
            self.btnClose:setTouchEnable(true)
        end, 0.5)
    end)
    return animRewardAppear
end


return MopUpUI