local MailManager = class("MailManager", BaseModule)

function MailManager:showMailUI()
    UIManager:showUI("app/ui/mail/mail_ui")
    self:getMailList()
end

function MailManager:getMailList(force)
    if not force and not DataManager.MailData:getNeedGetNewMail() then
        return
    end

    if self.needUpdateMailSid then
        self:unscheduleGlobal(self.needUpdateMailSid)
        self.needUpdateMailSid = nil
    end

    self:sendMessage(ProtoMsgType.FromMsgEnum.MailListReq, {}, {}, self.getMailListFinish)
end

function MailManager:getMailListFinish(result)
    if self.needUpdateMailSid then
        self:unscheduleGlobal(self.needUpdateMailSid)
        self.needUpdateMailSid = nil
    end

    if result.status == 0 then
        DataManager.MailData:init(result)
    end
end

-- 触发服务器时间相关邮件
function MailManager:getTriggeredTimeMail()
    self:sendMessage(ProtoMsgType.FromMsgEnum.MailCycleReq, {}, {}, self.getTriggeredTimeMailFinish)
end

function MailManager:getTriggeredTimeMailFinish(result)
    if result.status == 0 then
    end
end

function MailManager:claimMail(ids)
    if not ids then
        for mailId, entity in pairs(DataManager.MailData:getMails()) do
            if entity:getIsNormalMail() and entity:canClaim() then
                if not ids then
                    ids = {}
                end
                table.insert(ids, mailId)
            end
        end
    else
        for _, id in ipairs(ids) do
            local entity = DataManager.MailData:getMails()[id]
            if not entity or not entity:canClaim() then
                return
            end
        end
    end
    if not ids then
        return
    end

    local reqData = {ids = ids}
    self:sendMessage(ProtoMsgType.FromMsgEnum.MailExtractReq, reqData, {}, self.claimMailFinish, BIReport.ITEM_GET_TYPE.MAIL)
end

function MailManager:claimMailFinish(result)
    if result.status == 0 then
        GFunc.showRewardBox(result.rewards)

        local needDeleteIds = {}
        for _, id in ipairs(result.ids) do
            local entity = DataManager.MailData:getMails()[id]
            entity:receivedThis()
            if entity:getIsAdMail() or entity:getIsNormalMail() then
                table.insert(needDeleteIds, id)
            end
            BIReport:postMailClaim(id)
        end

        if needDeleteIds[1] then
            DataManager.MailData:deleteMail(needDeleteIds) -- 提前删除邮件，提升体验
            self:deleteMail(needDeleteIds)
        end

        DataManager.MailData:refreshRedPoint()
        DataManager.MailData:setDirty()
    end
end

function MailManager:deleteMail(ids)
    if not ids then
        for mailId, entity in pairs(DataManager.MailData:getMails()) do
            if entity:canDelete() then
                if not ids then
                    ids = {}
                end
                table.insert(ids, mailId)
            end
        end
    end
    if not ids or not ids[1] then
        return
    end
    local reqData = {ids = ids}
    local rspData = {ids = ids}
    self:sendMessage(ProtoMsgType.FromMsgEnum.MailDeleteReq, reqData, rspData, self.deleteMailFinish)
end

function MailManager:deleteMailFinish(result)
    if result.status then
        DataManager.MailData:deleteMail(result.ids)
    end
end

function MailManager:readMail(id)
    local entity = DataManager.MailData:getMails()[id]
    if not entity:notRead() then
        UIManager:showUI("app/ui/mail/mail_detail_ui", {id = id})
        return
    end

    local reqData = {ids = {id}}
    local rspData = {ids = {id}}
    self:sendMessage(ProtoMsgType.FromMsgEnum.MailReadReq, reqData, rspData, self.readMailFinish)
end

function MailManager:readMailFinish(result)
    if result.status == 0 then
        for _, id in ipairs(result.ids) do
            DataManager.MailData:setMailRead(id)
            BIReport:postMailOpen(id)
        end

        UIManager:showUI("app/ui/mail/mail_detail_ui", {id = result.ids[1]})
    end
end

function MailManager:needUpdateMail()
    if DataManager.MailData:getNeedGetNewMail() then
        return
    end

    -- 收到推送后不一定是有新邮件，要去拉一下, 延迟是为了减轻服务器压力
    if self.needUpdateMailSid then
        self:unscheduleGlobal(self.needUpdateMailSid)
        self.needUpdateMailSid = nil
    end
    self.needUpdateMailSid = self:performWithDelayGlobal(function()
        self:getMailList(true)
    end, math.random(1, 60))
end

return MailManager