import 'package:app_business/generated/base.pb.dart' as pb;
import 'package:app_business/service/api.dart';
import 'package:track_common/model.dart';
import 'package:track_common/view/home/personal_rank.dart';

class PersonalRankControllerImpl extends PersonalRankController {
  @override
  Future<List<RankActiveInfo>> getRankList(int mapId, DateTime startAt) async {
    final startAtSec = filterStartAt.value.millisecondsSinceEpoch ~/ 1000;
    final r = await Get.find<ApiService>()
        .stub
        .toGameRanking(pb.ToGameRankingRequest()
          ..mapId = mapId
          ..startSecond = Int64(startAtSec));

    final out = <RankActiveInfo>[];

    for (final actSrc in r.list) {
      final act = RankActiveInfo()
        ..id = actSrc.actId
        ..name = actSrc.actName
        ..userList = actSrc.rankList.map((e) => e.toModel()).toList();

      out.add(act);
    }
    return out;
  }
}

extension UserRankInfoExt on pb.ToOrienteerRankInfo {
  RankUserInfo toModel() {
    return RankUserInfo()
      ..id = oId
      ..name = oName
      ..routeName = courseName
      ..state = switch (state) {
        1 => GameState.finish,
        2 => GameState.processing,
        _ => GameState.unFinish
      }
      ..phone = phone
      ..startAt = startAt.toModel()
      ..duration = duration.toModel()
      ..distance = distance.meter;
  }
}
