import 'dart:math';

import 'package:sport/bean/sport_detail.dart';
import 'package:sport/bean/sport_target.dart';
import 'package:sport/bean/user.dart';
//import 'package:sport/bean/user.dart';

import 'game.dart' as game;

class SportIndex {
  int duration;
  int durationTarget;
  int consume;
  int beyond;
  String inspire;
  game.GameInfoData lastGame;
  List<game.GameInfoData> games;
  RankInfo rank;
  List<Achievement> achievement;

  Today today;
  SportTarget target;
  List<RecordsToday> records;

  SportIndex({this.duration, this.durationTarget, this.consume, this.beyond, this.inspire, this.lastGame, this.achievement});

  SportIndex.fromJson(Map<String, dynamic> json) {
    duration = json['duration'];
    durationTarget = json['duration_target'];
    consume = json['consume'];
    beyond = json['beyond'];
    inspire = json['inspire'];
    lastGame = json['last_game'] != null ? new game.GameInfoData.fromJson(json['last_game']) : null;
    rank = json['rank'] != null ? new RankInfo.fromJson(json['rank']) : null;
    if (json['games'] != null) {
      games = new List<game.GameInfoData>();
      json['games'].forEach((v) {
        games.add(new game.GameInfoData.fromJson(v));
      });
    }

    if (json['new_achievements'] != null) {
      achievement = new List<Achievement>();
      json['new_achievements'].forEach((v) {
        achievement.add(new Achievement.fromJson(v));
      });
    }

    today = json['today'] != null ? new Today.fromJson(json['today']) : null;

    target = json['target'] != null ? new SportTarget.fromJson(json['target']) : null;

    if (json['records'] != null) {
      records = new List<RecordsToday>();
      json['records'].forEach((v) {
        records.add(new RecordsToday.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['duration'] = this.duration;
    data['duration_target'] = this.durationTarget;
    data['consume'] = this.consume;
    data['beyond'] = this.beyond;
    data['inspire'] = this.inspire;
    if (this.lastGame != null) {
      data['last_game'] = this.lastGame.toJson();
    }
    data["new_achievements"] = this.achievement;
    return data;
  }
}

class RankInfo {
  Rank rank;
  User user;
  List<User> records;

  RankInfo({this.rank, this.user, this.records});

  RankInfo.fromJson(Map<String, dynamic> json) {
    rank = json['rank'] != null ? new Rank.fromJson(json['rank']) : null;
    user = json['user'] != null ? new User.fromJson(json['user']) : null;
    if (json['records'] != null) {
      records = new List<User>();
      json['records'].forEach((v) {
        records.add(new User.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.rank != null) {
      data['rank'] = this.rank.toJson();
    }
    if (this.user != null) {
      data['user'] = this.user.toJson();
    }
    if (this.records != null) {
      data['records'] = this.records.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Rank {
  int id;
  String name;
  int isGame;
  int isSport;
  int gameId;
  String introduce;
  int userCountMax;
  String rateBegin;
  String rateEnd;

  Rank({this.id, this.name, this.isGame, this.isSport, this.gameId, this.introduce, this.userCountMax, this.rateBegin, this.rateEnd});

  Rank.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    isGame = json['is_game'];
    isSport = json['is_sport'];
    gameId = json['game_id'];
    introduce = json['introduce'];
    userCountMax = json['user_count_max'];
    rateBegin = json['rate_begin'];
    rateEnd = json['rate_end'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['is_game'] = this.isGame;
    data['is_sport'] = this.isSport;
    data['game_id'] = this.gameId;
    data['introduce'] = this.introduce;
    data['user_count_max'] = this.userCountMax;
    data['rate_begin'] = this.rateBegin;
    data['rate_end'] = this.rateEnd;
    return data;
  }
}

class Today {
  int step, consume, duration;

  Today({this.step, this.consume, this.duration});

  Today.fromJson(Map<String, dynamic> json) {
    step = json['step'];
    consume = json['consume'];
    duration = json['duration_minute'];
  }

  double get strength => strengthToValue(consume);

  String strengthLabel() {
      return strengthToLabel(consume);
  }


  int value(String type) => type == "consume" ? consume : duration;
}

const strengthArr = ["低强度" , "强度适中" ,"高强度"];

double strengthToValue(int consume) => (consume ?? 0) / 60.0;

String strengthToLabel(int consume) {
  if(consume == 0)
    return "无";

  double v = (consume ?? 0) / 60.0;
  if (v < 5.0) {
    return strengthArr[0];
  } else if (v < 7.5) {
    return strengthArr[1];
  } else {
    return strengthArr[2];
  }
}