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

class UserInfo {
  int id;
  String name;
  String avatar;
  int sportTargetId;
  int topSubjectId;
  int level;
  double score;
  int isBan;
  int provinceId;
  int cityId;
  int districtId;
  String createdAt;
  int gender;
  int age;
  String province;
  String city;
  String district;
  SportRecordSum sportRecordSum;
  List<Achievement> achievements;
  String followStatus;

  UserInfo(
      {this.id,
        this.name,
        this.avatar,
        this.sportTargetId,
        this.topSubjectId,
        this.level,
        this.score,
        this.isBan,
        this.provinceId,
        this.cityId,
        this.districtId,
        this.createdAt,
        this.gender,
        this.age,
        this.province,
        this.city,
        this.district,
        this.sportRecordSum,
        this.achievements});

  UserInfo.fromJson(Map<String, dynamic> json) {
    id = Converter.toInt(json['id']);
    if(json.containsKey("uid")){
      id = Converter.toInt(json['uid']);
    }
    name = json['name'] ?? json['user_name'];
    avatar = json['avatar'] ?? json['user_avatar'];
    sportTargetId = json['sport_target_id'];
    topSubjectId = json['top_subject_id'];
    level = Converter.toInt(json['level']);
    score =  Converter.toDouble(json['score']);
    isBan =  Converter.toInt(json['is_ban']);
    provinceId =  Converter.toInt(json['province_id']);
    cityId =  Converter.toInt(json['city_id']);
    districtId =  Converter.toInt(json['district_id']);
    createdAt = json['created_at'];
    gender =  Converter.toInt(json['gender']);
    age =  Converter.toInt(json['age']);
    province = json['province'];
    city = json['city'];
    district = json['district'];
    sportRecordSum = json['sport_record_sum'] != null
        ? new SportRecordSum.fromJson(json['sport_record_sum'])
        : null;
    if (json['achievements_show'] != null) {
      achievements = new List<Achievement>();
      json['achievements_show'].forEach((v) {
        achievements.add(new Achievement.fromJson(v));
      });
    }
    followStatus= json['follow_status'];
  }

  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['sport_target_id'] = this.sportTargetId;
    data['top_subject_id'] = this.topSubjectId;
    data['level'] = this.level;
    data['score'] = this.score;
    data['is_ban'] = this.isBan;
    data['province_id'] = this.provinceId;
    data['city_id'] = this.cityId;
    data['district_id'] = this.districtId;
    data['created_at'] = this.createdAt;
    data['gender'] = this.gender;
    data['age'] = this.age;
    data['province'] = this.province;
    data['city'] = this.city;
    data['district'] = this.district;
    if (this.sportRecordSum != null) {
      data['sport_record_sum'] = this.sportRecordSum.toJson();
    }
    if (this.achievements != null) {
      data['achievements_show'] = this.achievements.map((v) => v.toJson()).toList();
    }
    data['follow_status'] = this.followStatus;
    return data;
  }

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

class SportRecordSum {
  int id;
  int userId;
  int consume;
  int duration;
  int jumpCount;
  int crouchCount;

  SportRecordSum(
      {this.id,
        this.userId,
        this.consume,
        this.duration,
        this.jumpCount,
        this.crouchCount});

  SportRecordSum.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['user_id'];
    consume = json['consume'];
    duration = json['duration'];
    jumpCount = json['jump_count'];
    crouchCount = json['crouch_count'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['user_id'] = this.userId;
    data['consume'] = this.consume;
    data['duration'] = this.duration;
    data['jump_count'] = this.jumpCount;
    data['crouch_count'] = this.crouchCount;
    return data;
  }
}
