local MailEntity = class("MailEntity", BaseData)

function MailEntity:ctor(info)
    info = info or GConst.EMPTY_TABLE
    self.id = info.id or 0
    self.createTime = GFunc.formatTimeStep(info.create_at)
    self.mailExpire = GFunc.formatTimeStep(info.expire + info.create_at)
    self.state = info.state or GConst.MailConst.MAIL_TYPE.NOT_READ
    self.mailType = GConst.MailConst.MAIL_TYPE.CUSTOM
    self.mailCfgId = nil -- 可能为空
    self.title = json.decode(info.title or "{}")
    self.body = json.decode(info.body or "{}")
    self.rewards = info.rewards or {}
    -- 处理一下格式
    for i, v in ipairs(self.rewards) do
        v.num = v.count
    end
    self.data.isDirty = false

    if self.title.key then
        local cfg = ConfigManager:getConfig("mail")[self.title.key]
        if cfg then
            self.mailCfgId = self.title.key
            self.mailType = cfg.type
        end
    end
end

function MailEntity:setDirty()
    self.data.isDirty = not self.data.isDirty
end

function MailEntity:getId()
    return self.id
end

function MailEntity:notRead()
    return self.state == GConst.MailConst.MAIL_STATE.NOT_READ
end

function MailEntity:isRead()
    return self.state == GConst.MailConst.MAIL_STATE.READED
end

function MailEntity:readThis()
    if self:isReceived() then
        return
    end
    self.state = GConst.MailConst.MAIL_STATE.READED
end

function MailEntity:isReceived()
    return self.state == GConst.MailConst.MAIL_STATE.RECEIVED
end

function MailEntity:receivedThis()
    self.state = GConst.MailConst.MAIL_STATE.RECEIVED
end

function MailEntity:getState()
    return self.state
end

function MailEntity:getMailName()
    if self.mailCfgId then
        return I18N:getConfig("mail")[self.mailCfgId].name
    else
        local language = I18N:getCurLanguage()
        if self.title.content then
            return self.title.content[language] or GConst.EMPTY_STRING
        elseif self.title.raw then
            return self.title.raw
        else
            return GConst.EMPTY_STRING
        end
    end
end

function MailEntity:getMailDesc()
    if self.mailCfgId then
        return I18N:getConfig("mail")[self.mailCfgId].desc
    else
        local language = I18N:getCurLanguage()
        if self.body.content then
            return self.body.content[language] or GConst.EMPTY_STRING
        elseif self.body.raw then
            return self.body.raw
        else
            return GConst.EMPTY_STRING
        end
    end
end

function MailEntity:getMailExpire()
    return self.mailExpire
end

function MailEntity:getRemainCd()
    if not self.mailExpire then
        return 0
    end

    local remainCd = self.mailExpire - Time:getServerTime()
    if remainCd < 0 then
        remainCd = 0
    end

    return remainCd
end

function MailEntity:getRemainCdStr()
    local hour = self:getRemainCd() // 3600
    if hour <= 0 then
        hour = 1
    end
    return I18N:getGlobalText(I18N.GlobalConst.MAIL_COUNTDOWN, hour)
end

function MailEntity:getRewards()
    return self.rewards
end

function MailEntity:haveReward()
    return self:getRewards()[1] ~= nil
end

function MailEntity:getMailType()
    return self.mailType
end

function MailEntity:getIsAdMail()
    return self.mailType == GConst.MailConst.MAIL_TYPE.AD
end

function MailEntity:getIsNormalMail()
    return self.mailType == GConst.MailConst.MAIL_TYPE.NORMAL
end

function MailEntity:canClaim()
    if self:isOver() then
        return false
    end

    if not self:getRewards()[1] then
        return false
    end

    return not self:isReceived()
end

function MailEntity:isOver()
    return self:getRemainCd() <= 0
end

function MailEntity:canDelete()
    if self:isReceived() then
        return true
    end
    if not self:haveReward() and self:isRead() then
        return true
    end
    return false
end

function MailEntity:showNew()
    if not self:notRead() then
        return false
    end

    return self:getId() > DataManager.MailData:getLastMailId()
end

return MailEntity