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

class User {
  int id;
  int userId;
  int gameId;
  num score;
  int provinceId;
  int cityId;
  int districtId;
  String finishAt;
  int duration;
  int consume;
  int up;
  int upNew;
  String rateBegin;
  String rateEnd;
  String userName;
  String userAvatar;
  int userLevel;
  int userGender;
  int userAge;
  int position;
  int sportDay;
  List<Achievement> achievements;

  User(
      {this.id,
        this.userId,
        this.gameId,
        this.score,
        this.provinceId,
        this.cityId,
        this.districtId,
        this.finishAt,
        this.duration,
        this.consume,
        this.up,
        this.upNew,
        this.rateBegin,
        this.rateEnd,
        this.userName,
        this.userAvatar,
        this.userLevel,
        this.userGender,
        this.userAge,
        this.position,
        this.achievements});

  User.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['user_id'];
    gameId = json['game_id'];
    score = json['score'];
    provinceId = json['province_id'];
    cityId = json['city_id'];
    districtId = json['district_id'];
    finishAt = json['finish_at'];
    duration = json['duration'];
    consume = json['consume'];
    up = json['up'];
    upNew = Converter.toInt(json['up_new']);
    rateBegin = json['rate_begin'];
    rateEnd = json['rate_end'];
    userName = json['user_name'];
    userAvatar = json['user_avatar'];
    userLevel = json['user_level'];
    userGender = json['user_gender'];
    userAge = json['user_age'];
    position = json['position'];
    sportDay = json['sport_day'] ?? 0;
    if (json['achievements'] != null) {
      achievements = new List<Achievement>();
      json['achievements'].forEach((v) {
        achievements.add(new Achievement.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['user_id'] = this.userId;
    data['game_id'] = this.gameId;
    data['score'] = this.score;
    data['province_id'] = this.provinceId;
    data['city_id'] = this.cityId;
    data['district_id'] = this.districtId;
    data['finish_at'] = this.finishAt;
    data['duration'] = this.duration;
    data['consume'] = this.consume;
    data['up'] = this.up;
    data['up_new'] = this.upNew;
    data['rate_begin'] = this.rateBegin;
    data['rate_end'] = this.rateEnd;
    data['user_name'] = this.userName;
    data['user_avatar'] = this.userAvatar;
    data['user_level'] = this.userLevel;
    data['user_gender'] = this.userGender;
    data['user_age'] = this.userAge;
    data['position'] = this.position;
    data['sport_day'] = this.sportDay;
    if (this.achievements != null) {
      data['achievements'] = this.achievements.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Achievement {
  int id;
  int seriesId;
  String logo;
  String name;
  int rewardExp;
  int rewardScore;
  int conditionCount;
  int conditionProgress;
  int userId;
  String createdAt;
  String conditionDetail;
  String conditionMeasure;
  int userCount;
  int seriesCount;
  String seriesName;
//  int conditionCount;
//  int conditionProgress;

  Achievement(
      {this.id,
        this.seriesId,
        this.logo,
        this.name,
        this.rewardExp,
        this.rewardScore,
        this.conditionCount,
        this.conditionProgress,
        this.createdAt,
        this.conditionDetail,
        this.conditionMeasure,
        this.userCount,
        this.seriesCount,
        this.seriesName,
//        this.conditionCount,
//        this.conditionProgress,
      });

  Achievement.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['user_id'];
    seriesId = json['series_id'];
    logo = json['logo'];
    name = json['name'];
    rewardExp = json['reward_exp'];
    rewardScore = json['reward_score'];
    conditionCount = json['condition_count'];
    conditionProgress = json['condition_progress'];
    createdAt = json['created_at'] == null ? "" : json['created_at'] ;
    conditionDetail = json['condition_detail'];
    conditionMeasure = json['condition_measure'];
    userCount = json['user_count'];
    seriesCount = json['series_count'];
    seriesName = json['series_name'];
//    conditionProgress = json['condition_progress'];
//    conditionCount = json['condition_count'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['user_id'] = this.userId;
    data['series_id'] = this.seriesId;
    data['logo'] = this.logo;
    data['name'] = this.name;
    data['reward_exp'] = this.rewardExp;
    data['reward_score'] = this.rewardScore;
    data['condition_count'] = this.conditionCount;
    data['condition_progress'] = this.conditionProgress;
    data['created_at'] = this.createdAt;
    data['condition_detail'] = this.conditionDetail;
    data['condition_measure'] = this.conditionMeasure;
    data['user_count'] = this.userCount;
    data['series_count'] = this.seriesCount;
    data['series_name'] = this.seriesName;
//    data['condition_progress'] = this.conditionProgress;
//    data['condition_count'] = this.conditionCount;
    return data;
  }
}
//class Achievement {
//  int id;
//  String name;
//  String logo;
//  int conditionSportDuration;
//  int sort;
//  int seriesId;
//  int rewardScore;
//
//  Achievement(
//      {this.id,
//        this.name,
//        this.logo,
//        this.conditionSportDuration,
//        this.sort,
//        this.seriesId,
//        this.rewardScore});
//
//  Achievement.fromJson(Map<String, dynamic> json) {
//    id = json['id'];
//    name = json['name'];
//    logo = json['logo'];
//    conditionSportDuration = json['condition_sport_duration'] == null ? json['condition_duration'] : json['condition_sport_duration'];
//    sort = json['sort'];
//    seriesId = json['series_id'];
//    rewardScore = json['reward_score'];
//  }
//
//  Map<String, dynamic> toJson() {
//    final Map<String, dynamic> data = new Map<String, dynamic>();
//    data['id'] = this.id;
//    data['name'] = this.name;
//    data['logo'] = this.logo;
//    data['condition_sport_duration'] = this.conditionSportDuration;
//    data['sort'] = this.sort;
//    data['series_id'] = this.seriesId;
//    data['reward_score'] = this.rewardScore;
//    return data;
//  }
//}