local SummonManager = class("SummonManager", BaseModule)

-- 商城宝箱
function SummonManager:summon(summonType, costType)
    if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.STORE_BOX_OPEN, true) then
        return
    end

    if summonType == GConst.SummonConst.SUMMON_TYPE.LV_3 and not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.STORE_BOX_3_OPEN, true) then
        return
    end

    local costItem, cost  = DataManager.SummonData:getSummonCost(summonType)
    if costType == 1 then
        if not GFunc.checkCost(costItem.id, costItem.num, false) then
            return
        end
    else
        if not GFunc.checkCost(cost.id, cost.num, false) then
            return
        end
    end

    local params = {type = summonType, consume_type = costType}
    local responseData = {}
    self:sendMessage(ProtoMsgType.FromMsgEnum.SummonReq, params, responseData, self.summonFinish, BIReport.ITEM_GET_TYPE.SUMMON)
end

function SummonManager:summonFinish(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        DataManager.SummonData:init(result.summon_info)
        if result.reqData then
            local summonType = result.reqData.type
            ModuleManager.ShopManager:showBoxOpenUI({type = GConst.ShopConst.BOX_REWARD_TYPE.SUMMON, params = summonType, rewards = result.rewards})
            -- 任务
            if summonType == GConst.SummonConst.SUMMON_TYPE.LV_1 then
                ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_OPEN_SHOP_BOX_LEVEL_1, 1)
            elseif summonType == GConst.SummonConst.SUMMON_TYPE.LV_2 then
                ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_OPEN_SHOP_BOX_LEVEL_2, 1)
            elseif summonType == GConst.SummonConst.SUMMON_TYPE.LV_3 then
                ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_OPEN_SHOP_BOX_LEVEL_3, 1)
            end
            if result.rewards then
                local openType = BIReport.BOX_OPEN_OPEN_TYPE.USE_KEY
                if result.reqData.consume_type ~= 1 then -- 钻石
                    openType = BIReport.BOX_OPEN_OPEN_TYPE.USE_GEM
                end
                local count = 0
                if result.summon_info and result.summon_info.count[summonType] then
                    count = result.summon_info.count[summonType]
                end
                BIReport:postBoxOpen(summonType, BIReport.BOX_OPEN_BOX_TYPE.SHOP, openType, DataManager.SummonData:getSummonLevel(), result.rewards, count)
            end
        end
        if result.summon_info.count then
            local totalCount = 0
            for t, count in pairs(result.summon_info.count) do
                totalCount = totalCount + count
            end
            BIReport:postFirstDayOpenBox(totalCount)
        end
    end
end

return SummonManager