import 'package:betting/data/base_service/api_response.dart';
import 'package:betting/data/model/res/goal_score/goal_score_model.dart';
import 'package:betting/data/model/res/infor_team/infor_team.dart';
import 'package:betting/data/model/res/teams/teams.dart';
import 'package:betting/data/repository/goal_scorers/goal_scorers_repo.dart';
import 'package:betting/data/repository/teams/team_repo.dart';
import 'package:betting/view/mobile/favourites_view/teams/team_info_view/team_info_controller.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class OverviewController extends GetxController {
  final _topScoreRepo = GoalScorerRepo();
  var itemTeam = ItemTeams().obs;
  TeamsRepo teamsRepo = TeamsRepo();
  var infoTeam = InforTeam().obs;
  var h2hTeam = H2h().obs;
  var isReady = false.obs;
  RxList last5Team1 = [].obs;
  RxList last5Team2 = [].obs;
  RxList h2hFormTeam1 = <String>[].obs;
  RxList h2hFormTeam2 = <String>[].obs;
  RxList topScores = <GoalScore>[].obs;
  var outputDateToDateFormat = DateFormat('dd/MM/yyyy');
  var outputTimeFormat = DateFormat('HH:mm');

  @override
  void onInit() {
    if (Get.isRegistered<TeamInfoController>()) {
      getOverViewInfoTeam();
    }

    itemTeam.value = Get.arguments;

    getListTopScore(itemTeam.value.id!);
    super.onInit();
  }

  getOverViewInfoTeam() async {
    await teamsRepo
        .inforTeam('${Get.find<TeamInfoController>().itemTeam.value.id}')
        .then(
      (value) {
        if (value.state == Status.SUCCESS) {
          infoTeam.value = value.object!;
          if (infoTeam.value.h2h != null) {
            h2hTeam.value = infoTeam.value.h2h!;
          }

          last5Team1.value = h2hTeam.value.team1Last6!.isNotEmpty ? h2hTeam.value.team1Last6!.take(5).toList() : [];
          last5Team1.add(Team1Last6(ftScore: 'VIEW ALL'));
          last5Team2.value = h2hTeam.value.team2Last6!.isNotEmpty ? h2hTeam.value.team2Last6!.take(5).toList() : [];

          h2hFormTeam1.value = h2hTeam.value.h2hFormTeam1 ?? [];
          h2hFormTeam2.value = h2hTeam.value.h2hFormTeam2 ?? [];
        }
      },
    );
    isReady.value = true;
    update();
  }

  /// todo: Method get top score by competition id
  void getListTopScore(String teamId) async {
    await _topScoreRepo.getListTopScore(0, 20, '', '', teamId).then((value) {
      if (value.state == Status.SUCCESS) {
        topScores.value = value.object!;
      }
    });
  }
}
