class MessageInstance {
  int fromId;
  int toId;
  String type;
  MessageData data;
  int read;
  bool selfSend;
  String createdAt;
  MessageUser toUser;
  MessageUser fromUser;
  String relate;

  MessageInstance(
      {this.fromId,
      this.toId,
      this.type,
      this.data,
      this.read,
      this.selfSend,
      this.createdAt,
      this.toUser,
      this.fromUser,
      this.relate});

  MessageInstance.fromJson(Map<String, dynamic> json) {
    fromId = json['from_id'];
    toId = json['to_id'];
    type = json['type'];
    data = json['data'] != null ? new MessageData.fromJson(json['data']) : null;
    read = json['read'];
    selfSend = json['self_send'];
    createdAt = json['created_at'];
    toUser = json['to_user'] != null
        ? new MessageUser.fromJson(json['to_user'])
        : null;
    fromUser = json['from_user'] != null
        ? new MessageUser.fromJson(json['from_user'])
        : null;
    relate = json['relate'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['from_id'] = this.fromId;
    data['to_id'] = this.toId;
    data['type'] = this.type;
    if (this.data != null) {
      data['data'] = this.data.toJson();
    }
    data['read'] = this.read;
    data['self_send'] = this.selfSend;
    data['created_at'] = this.createdAt;
    if (this.toUser != null) {
      data['to_user'] = this.toUser.toJson();
    }
    if (this.fromUser != null) {
      data['from_user'] = this.fromUser.toJson();
    }
    data['relate'] = this.relate;
    return data;
  }
}

class MessageData {
  String text;
  String url;
  String logo;
  MessagePostUser user;
  MessageForum forum;
  MessageSubject subject;

  MessageData({
    this.text,
    this.url,
    this.logo,
    this.user,
    this.forum,
    this.subject,
  });

  MessageData.fromJson(Map<String, dynamic> json) {
    text = json['text'];
    url = json['url'];
    logo = json['logo'];
    user = json['user'] != null ? MessagePostUser.fromJson(json['user']) : null;
    forum = json['forum'] != null ? MessageForum.fromJson(json['forum']) : null;
    subject = json['subject'] != null
        ? MessageSubject.fromJson(json["subject"])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> map = new Map<String, dynamic>();
    map['text'] = this.text;
    map['url'] = this.url;
    map['logo'] = this.logo;
    map['user'] = this.user;
    map['forum'] = this.forum;
    map['subject'] = this.subject;
    return map;
  }
}

class MessageUser {
  int id;
  String name;
  String avatar;
  bool online;

  MessageUser({this.id, this.name, this.avatar, this.online});

  MessageUser.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    avatar = json['avatar'];
    online = json['online'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['avatar'] = this.avatar;
    data['online'] = this.online;
    return data;
  }
}

// 这里是轮询的消息结构...
class Message {
  int curId;
  List<MessageInstance> messages;

  Message({this.curId, this.messages});

  Message.fromJson(Map<String, dynamic> json) {
    curId = json['cur_id'];
    messages = new List<MessageInstance>();
    if (json['messages'] != null) {
      json['messages'].forEach((v) {
        messages.add(new MessageInstance.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.curId;
    if (this.messages != null) {
      data['messages'] = this.messages.map((v) => v.toJson()).toList();
    }

    return data;
  }
}

class ChatMessage {
  MessageUser user;
  List<MessageInstance> messages;

  ChatMessage({this.user, this.messages});

  ChatMessage.fromJson(Map<String, dynamic> json) {
    user = json['user'];
    messages = new List<MessageInstance>();
    if (json['message'] != null) {
      json['message'].forEach((v) {
        messages.add(new MessageInstance.fromJson(v));
      });
    }
  }
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['user'] = this.user;
    if (this.messages != null) {
      data['messages'] = this.messages.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

// 聊天首页
class ChatMessageInstance {
  int id;
  int fromId;
  int toId;
  String type;
  String relate;
  bool selfSend;
  int read;
  int unreadCount;
  String createdAt;
  MessageData data;
  MessageUser toUser;
  MessageUser fromUser;

  ChatMessageInstance({
    this.id,
    this.fromId,
    this.toId,
    this.type,
    this.relate,
    this.selfSend,
    this.read,
    this.unreadCount,
    this.createdAt,
    this.data,
    this.toUser,
    this.fromUser,
  });

  ChatMessageInstance.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    fromId = json['from_id'];
    toId = json['to_id'];
    type = json['type'];
    relate = json['relate'];
    selfSend = json['self_send'];
    read = json['read'];
    unreadCount = json['unread_count'];
    createdAt = json['created_at'];
    data = json['data'];
    toUser = json['to_user'];
    fromUser = json['from_user'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['from_id'] = this.fromId;
    data['to_id'] = this.toId;
    data['type'] = this.type;
    data['relate'] = this.relate;
    data['self_send'] = this.selfSend;
    data['read'] = this.read;
    data['unread_count'] = this.unreadCount;
    data['created_at'] = this.createdAt;
    data['data'] = this.data;
    data['to_user'] = this.toUser;
    data['from_user'] = this.fromUser;
    return data;
  }
}

class ChatOnlineInfo {
  bool online;
  String relate;
  int userId;

  ChatOnlineInfo.fromJson(Map<String, dynamic> json) {
    online = json['online'];
    relate = json['relate'];
    userId = json['id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['online'] = this.online;
    data['relate'] = this.relate;
    data['id'] = this.userId;
    return data;
  }
}

class MessageForum {
  String cover;
  String name;
  int id;

  MessageForum.fromJson(Map<String, dynamic> json) {
    cover = json['cover'];
    name = json['name'];
    id = json['id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['cover'] = this.cover;
    data['name'] = this.name;
    data['id'] = this.id;
    return data;
  }
}

class MessageSubject {
  String title;
  String content;
  int id;
  List<String> images;
  String cover;

  MessageSubject.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    content = json['content'];
    id = json['id'];
    images = new List<String>();
    cover = json['cover'];
    images = [];
    if (json['images'] != null && json['images'].length > 0) {
      json['images'].forEach((v) {
        images.add(v);
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['title'] = this.title;
    data['content'] = this.content;
    data['images'] = this.images;
    data['id'] = this.id;
    data['cover'] = this.cover;
    return data;
  }
}

class MessagePostUser {
  int id;
  String name;
  MessagePostUser({this.id, this.name});
  MessagePostUser.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['id'] = this.id;
    return data;
  }
}
