import 'dart:math';

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

import 'image.dart';

class Post {
  String id;
  String userId;
  String forumId;
  String title;
  String content;
  List<Image> images;
  String createdAt;
  int shareCount;
  int likeCount;
  int commentCount;
  String isGood;
  String isTop;
  String isUserTop;
  String isOfficial;
  String nickname;
  String avatar;
  int createTime;
  bool isLiked;
  bool isExpansion;
  bool isDelete;
  String quoteSubjectId, quoteCommentId;
  String forumName, forumCover;
  Post quoteSubject;
  List<String> tags;
  String followStatus;
  String quoteData;
  String gameName;
  Post({
    this.id,
    this.userId,
    this.forumId,
    this.title,
    this.content,
    this.images,
    this.createdAt,
    this.shareCount,
    this.likeCount,
    this.commentCount,
    this.isGood,
    this.nickname,
    this.avatar,
    this.createTime,
    this.isLiked,
    this.quoteData,
    this.gameName,
  });

  Post.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['user_id'];
    forumId = json['forum_id'];
    title = json['title'];
    content = json['content'];
    if (json['images'] != null) {
      images = new List();
      if (json['images'] is List) {
        json['images'].forEach((v) {
          if (v is String) {
            images.add(Image(src: v));
          } else {
            images.add(new Image.fromJson(v));
          }
        });
      }
      if (json['images'] is String) {
        if (json['images'] != "") images.add(new Image(src: json['images']));
      }
    }
    createdAt = json['created_at'];
    shareCount = Converter.toInt(json['share_count']);
    likeCount = Converter.toInt(json['like_count']);
    commentCount = Converter.toInt(json['comment_count']);
    isGood = json['is_good'];
    isTop = json['is_top'];
    isOfficial = json['is_official'];
    nickname = json['nickname'];
    avatar = json['avatar'];
    createTime = json['create_time'];
    isLiked = json['is_liked'];
    content = content ?? content.trim();
    quoteSubjectId = json['quote_subject_id'];
    quoteCommentId = json['quote_comment_id'];
    forumName = json['forum_name'];
    forumCover = json['forum_cover'];
    quoteSubject = json['quoteSubject'] != null
        ? new Post.fromJson(json['quoteSubject'])
        : null;
    tags = [];
    if (json['tags'] != null) {
      if (json['tags'] is List) {
        json['tags'].forEach((v) {
          if (v is String) {
            tags.add(v);
          }
        });
      } else {
        tags.add(json['tags']);
      }
    }
    followStatus = json['follow_status'];
    quoteData = json['quote_data'];
    gameName = json['game_name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['user_id'] = this.userId;
    data['forum_id'] = this.forumId;
    data['title'] = this.title;
    data['content'] = this.content;
    if (this.images != null) {
      data['images'] = this.images.map((v) => v.toJson()).toList();
    }
    data['created_at'] = this.createdAt;
    data['share_count'] = this.shareCount;
    data['like_count'] = this.likeCount;
    data['comment_count'] = this.commentCount;
    data['is_good'] = this.isGood;
    data['is_top'] = this.isTop;
    data['is_official'] = this.isOfficial;
    data['nickname'] = this.nickname;
    data['avatar'] = this.avatar;
    data['create_time'] = this.createTime;
    data['is_liked'] = this.isLiked;
    data['follow_status'] = this.followStatus;
    if (this.quoteSubject != null) {
      data['quoteSubject'] = this.quoteSubject.toJson();
    }
    data['quote_data'] = this.quoteData;
    data['game_name'] = this.gameName;
    return data;
  }

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

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

  bool isFriend() {
    return followStatus == "friends" || followStatus == "followed";
  }

  bool isMe(int userId) {
    return int.parse(this.userId) == userId;
  }
}
