import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sport/bean/sport_index.dart';
import 'package:sport/bean/user.dart';
import 'package:sport/provider/lib/view_state_refresh_list_model.dart';
import 'package:sport/provider/user_model.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/services/api/resp.dart';

class SportIndexModel extends ViewStateRefreshListModel with InjectApi {
  SportIndex data;
  int gameId;
  int provinceId, cityId, districtId;
  BuildContext context;


  ValueNotifier< List<Achievement>> valueNotifier = ValueNotifier([]);

  SportIndexModel(this.context);

  @override
  Future<List> loadData({int pageNum}) async {
    data = (await api.getSportIndex()).data;
    if (data != null) {
      gameId = data.games?.first?.id;
      Provider.of<UserModel>(context, listen: false).durationTarget.value = data.durationTarget;
//      achievementList.add(data.achievement);
      if(data.achievement != null){
        valueNotifier.value = data.achievement;
      }
      return [data];
    } else {
      return [];
    }
  }

  void game(int id) {
    this.gameId = id;
    setIdle();
    switchArea();
  }

  Future area(int provinceId, int cityId, int districtId) async {
    this.provinceId = provinceId;
    this.cityId = cityId;
    this.districtId = districtId;
    setIdle();
    await switchArea();
  }

  Future switchArea() async {
    RespData<RankInfo> resp = await api.getRankInfo(gameId.toString(), provinceId: provinceId, cityId: cityId);
    if (resp != null && resp.data != null) {
      data.rank = resp.data;
      setIdle();
    }
  }
}
