import 'dart:math';

import 'package:sport/bean/post_user.dart';
import 'package:sport/services/Converter.dart';

class Comment {
  String id;
  String subjectId;
  String userId;
  String toCommentId;
  String parentCommentId;
  String toUserId;
  String content;
  int likeCount;
  int commentCount;
  String createdAt;
  PostUser socialInfo;
  String time;
  int createTime;
  List<Comment> subList;
  bool isLiked = false;

  String userName;
  String userAvatar;
  String toUserName;
  String toUserAvatar;

  bool isExpansion;

  Comment(
      {this.id,
      this.subjectId,
      this.userId,
      this.toCommentId,
      this.parentCommentId,
      this.toUserId,
      this.content,
      this.likeCount,
      this.commentCount,
      this.createdAt,
      this.socialInfo,
      this.time,
      this.createTime,
      this.subList,
      this.isLiked = false,
      this.userName,
        this.userAvatar,
        this.toUserName,
        this.toUserAvatar,
      });

  Comment.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    subjectId = json['subject_id'];
    userId = json['user_id'];
    toCommentId = json['to_comment_id'];
    parentCommentId = json['parent_comment_id'];
    toUserId = json['to_user_id'];
    content = json['content'];
    likeCount = Converter.toInt(json['like_count']);
    commentCount = Converter.toInt(json['comment_count']);
    createdAt = json['created_at'];
    socialInfo = json['socialInfo'] != null ? new PostUser.fromJson(json['socialInfo']) : null;
    time = json['time'];
    createTime = json['create_time'];
    if (json['subList'] != null) {
      subList = new List<Comment>();
      json['subList'].forEach((v) {
        subList.add(new Comment.fromJson(v));
      });
    }
    isLiked = json['is_liked'];

    userName = json['user_name'];
    userAvatar = json['user_avatar'];
    toUserName = json['to_user_name'];
    toUserAvatar = json['to_user_avatar'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['subject_id'] = this.subjectId;
    data['user_id'] = this.userId;
    data['to_comment_id'] = this.toCommentId;
    data['parent_comment_id'] = this.parentCommentId;
    data['to_user_id'] = this.toUserId;
    data['content'] = this.content;
    data['like_count'] = this.likeCount;
    data['comment_count'] = this.commentCount;
    data['created_at'] = this.createdAt;
    if (this.socialInfo != null) {
      data['socialInfo'] = this.socialInfo.toJson();
    }
    data['time'] = this.time;
    data['create_time'] = this.createTime;
    if (this.subList != null) {
      data['subList'] = this.subList.map((v) => v.toJson()).toList();
    }

    data['user_name'] = this.userName;
    data['user_avatar'] = this.userAvatar;
    data['to_user_name'] = this.toUserName;
    data['to_user_avatar'] = this.toUserAvatar;

    return data;
  }

  void toggleLike() {
    isLiked = !(isLiked ?? false);
    likeCount = incLikeCount();
  }

  int incLikeCount() {
    return max(0, likeCount + (isLiked ? 1 : -1));
  }
}
