//
//  CustomRedPacketMessage.m
//

#import "CustomRedPacketMessage.h"


@implementation CustomRedPacketMessage

///初始化
+ (instancetype)messageWithContent:(NSString *)content {
    CustomRedPacketMessage *msg = [[CustomRedPacketMessage alloc] init];
    if (msg) {
        msg.content = content;
    }
    return msg;
}

///消息是否存储，是否计入未读数
+ (RCMessagePersistent)persistentFlag {
    return MessagePersistent_ISCOUNTED;
}

/// NSCoding
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        self.content = [aDecoder decodeObjectForKey:@"content"];
        self.extra = [aDecoder decodeObjectForKey:@"extra"];
    }
    return self;
}

/// NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.content forKey:@"content"];
    [aCoder encodeObject:self.extra forKey:@"extra"];
}

///将消息内容编码成json
- (NSData *)encode {
    NSMutableDictionary *dataDict = [self encodeBaseData];
    [dataDict setObject:self.content forKey:@"content"];
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:kNilOptions error:nil];
    return data;
}


///将json解码生成消息内容
- (void)decodeWithData:(NSData *)data {
    NSDictionary *jsonDic = [[self class] dictionaryFromJsonData:data];
    if (!jsonDic) {
        // 解析失败保存原数据
        self.rawJSONData = data;
        return;
    }
    // 基类负责解析基类属性
    [self decodeBaseData:jsonDic];
    self.content = jsonDic[@"content"];
}

/// 会话列表中显示的摘要
- (NSString *)conversationDigest {
    return @"红包";
}

///消息的类型名
+ (NSString *)getObjectName {
    return @"app:red_packet";
}

@end
