import 'package:betting/commom/utils/app_utils.dart';
import 'package:betting/data/model/res/categories/categories.dart';
import 'package:betting/data/model/res/teams/teams.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

import '../../../../data/base_service/api_response.dart';
import '../../../../data/repository/categories/categories_repo.dart';
import '../../../../data/repository/teams/team_repo.dart';

class FollowTeamsController extends GetxController {
  var listTitle = <Categories>[].obs;
  var indexClick = 0.obs;
  FocusNode focusSearch = FocusNode();
  var textEditingControllerSearch = TextEditingController();
  var listTeamFollow = <ItemTeams>[];
  var listTeamToCategories = <ItemTeams>[].obs;
  var listTeamToCategoriesLoadMore = <ItemTeams>[].obs;

  var listIdTeamFavourites = <String>[].obs;
  var listTeam = <ItemTeams>[].obs;
  var listIsFollow = <bool>[].obs;
  final TeamsRepo teamsRepo = TeamsRepo();
  final CategoriesRepo categoriesRepo = CategoriesRepo();
  FToast? fToast;
  var controllerEx = ScrollController();
  var teamList = Teams().obs;
  var listTeamLoadmore = <ItemTeams>[].obs;
  var indexPage = 1.obs;

  @override
  void onInit() async {
    await getAllCategories();
    await getListTeam('');
    controllerEx = ScrollController()..addListener(_scrollTeams);
    super.onInit();
  }


  onPageViewChange(int page) {
    indexClick.value = page;
    if(page != 0) {
      indexClick.value-1;
    } else {
      indexClick.value = 0;
    }

  }


  addTeamFollow()async{
    await teamsRepo.addTeamFollow([], [], []).then((value) {
      if (value.state == Status.SUCCESS) {
      }
    });
  }


  getAllCategories()async{
    await categoriesRepo.getAllCategories().then((value) {
      if (value.state == Status.SUCCESS) {
        listTitle.value = value.object!;
      }
    });
  }

  getListTeam(String nameTeam )async{
    await teamsRepo.getListTeam(0, 20, nameTeam).then((value) {
      if (value.state == Status.SUCCESS) {
        listTeam.value = value.object!.items!;
        listTeamToCategories.value = [];
        listTeamToCategories.value = listTeam.where((p0) => p0.category == listTitle[indexClick.value].id).toList();
        listTeamToCategories.refresh();
        listIsFollow.value = [];
        for(int i =0 ;i<listTeam.length;i++){
          if(listIdTeamFavourites.contains(listTeam[i].id)){
            listIsFollow.add(true);
          }else{
            listIsFollow.add(false);
          }
        }
      }
    });
  }



  changeSport(){
    listTeamToCategories.value = [];
    listTeamToCategories.value = listTeam.where((p0) => p0.category == listTitle[indexClick.value].id).toList();
    listTeamToCategories.refresh();
    listIsFollow.value = [];
    for(int i =0 ;i<listTeam.length;i++){
      if(listIdTeamFavourites.contains(listTeam[i].id)){
        listIsFollow.add(true);
      }else{
        listIsFollow.add(false);
      }
    }
  }

  void _scrollTeams(){
    if(controllerEx.position.pixels == controllerEx.position.maxScrollExtent){
      if(listTeamToCategoriesLoadMore.isNotEmpty){
        indexPage++;
      }
      teamsRepo.getListTeam(indexPage.value, 20,textEditingControllerSearch.value.text).then((value) {
        if (value.state == Status.SUCCESS) {
          listTeamToCategoriesLoadMore.value = [];
          teamList.value = value.object!;
          listTeamLoadmore.value =teamList.value.items!;
          listTeamToCategoriesLoadMore.value = listTeamLoadmore.where((p0) => p0.category == listTitle[0].id).toList();
          listTeamToCategories.addAll(listTeamToCategoriesLoadMore);
          listTeam.addAll(listTeamLoadmore);
          for(int i =0 ;i<listTeamLoadmore.length;i++){
            listIsFollow.add(false);
          }
          listTeamToCategories.refresh();
          AppUtils.shared.hideLoading();
          Future.delayed(const Duration(seconds: 1), () {
          });
        } else {
        }

      });

    }
  }


}



