import 'package:betting/data/base_service/api_response.dart';
import 'package:betting/data/model/res/infor_team/infor_team.dart';
import 'package:betting/data/model/res/matches/matches.dart';
import 'package:betting/data/repository/matches/matches_repo.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class InforMatchesRegionsController extends GetxController {
  var itemMatches = Matches().obs;
  MatchesRepo matchesRepo = MatchesRepo();
  var infoTeam = InforTeam().obs;
  var h2hTeam = H2h().obs;
  RxList formTeam1 = [].obs;
  RxList formTeam2 = [].obs;
  RxList h2hFormTeam1 = <String>[].obs;
  RxList h2hFormTeam2 = <String>[].obs;
  var outputDateToDateFormat = DateFormat('dd/MM/yyyy');
  var outputTimeFormat = DateFormat('HH:mm');
  var isReady = false.obs;

  @override
  void onInit() {

    itemMatches.value = Get.arguments;

    getInfoMatches(itemMatches.value.sId!);
    super.onInit();
  }

  getInfoMatches(String matchesId) async {
    await matchesRepo.inforMatches(matchesId).then(
      (value) {
        if (value.state == Status.SUCCESS) {
          infoTeam.value = value.object!;

          if (infoTeam.value.h2h != null) {
            h2hTeam.value = infoTeam.value.h2h!;

          }
          if (h2hTeam.value.team1Last6!.length > 5) {
            formTeam1.value = h2hTeam.value.team1Last6!.take(5).toList();
          } else {
            formTeam1.value = h2hTeam.value.team1Last6!;
          }

          if (h2hTeam.value.team2Last6!.length > 5) {
            formTeam2.value = h2hTeam.value.team2Last6!.take(5).toList();
          } else {
            formTeam2.value = h2hTeam.value.team2Last6!;
          }


          if(h2hTeam.value.team1Last6!.isNotEmpty){
            formTeam1.add(Team1Last6(ftScore: 'VIEW ALL'));

          }
          if(h2hTeam.value.team2Last6!.isNotEmpty){
            formTeam2.add(Team1Last6(ftScore: 'VIEW ALL'));

          }
          h2hFormTeam1.value = h2hTeam.value.h2hFormTeam1!;
          h2hFormTeam2.value = h2hTeam.value.h2hFormTeam2!;




        }
      },
    );
    isReady.value = true;
    update();
  }
}
