import 'package:betting/commom/utils/app_utils.dart';
import 'package:betting/data/base_service/api_response.dart';
import 'package:betting/data/repository/competitions/competitions_repo.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import '../../../../data/model/res/categories/categories.dart';
import '../../../../data/model/res/competitions/competitions.dart';
import '../../../../data/repository/categories/categories_repo.dart';
class AddCompetitionsController extends GetxController {
  var listTitle = <Categories>[].obs;
  var indexClick = 0.obs;
  FocusNode focusSearch = FocusNode();
  var textEditingControllerSearch = TextEditingController();
  var listCompetitionsToCategories = <ItemsCompetitions>[].obs;
  var listCompetitionsToCategoriesLoadMore = <ItemsCompetitions>[].obs;
  var listIdCompetitionsFavourites = <String>[].obs;
  var listCompetitions = <ItemsCompetitions>[].obs;
  var listCompetitionsLoadMore = <ItemsCompetitions>[].obs;
  var listIsFollow = <bool>[].obs;
  var listCompetitionsFollow = <ItemsCompetitions>[];
  final CategoriesRepo categoriesRepo = CategoriesRepo();
  final CompetitionsRepo competitionsRepo = CompetitionsRepo();
  FToast? fToast;
  var controllerEx = ScrollController();
  var itemLoadMoreEx = <ItemsCompetitions>[].obs;
  var indexPage = 1.obs;
  var competition= Competitions().obs;


  @override
  void onInit() async {
    await getAllCategories();
    await getListCompetitions('');
    controllerEx = ScrollController()..addListener(_scrollCompetitions);

    super.onInit();
  }


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

  }


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

  addCompetitionsFavorites() async {
    await competitionsRepo.addCompetitionsFavorites(listIdCompetitionsFavourites).then((value) {
      if (value.state == Status.SUCCESS) {
      }
    },);
  }


  getListCompetitions(String nameCompetitions)async{
    await competitionsRepo.getListCompetitions(0, 20,nameCompetitions).then((value) {
      if (value.state == Status.SUCCESS) {
        listCompetitions.value = value.object!.items!;
        listCompetitionsToCategories.value = [];
        listCompetitionsToCategories.value = listCompetitions.where((p0) => p0.category == listTitle[0].id).toList();
        listCompetitionsToCategories.refresh();
        listIsFollow.value = [];
        for(int i =0 ;i<listCompetitions.length;i++){
          if(listIdCompetitionsFavourites.contains(listCompetitions[i].id)){
            listIsFollow.add(true);
          }else{
            listIsFollow.add(false);
          }
        }
      }
    });
  }


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

  void _scrollCompetitions(){
    if(controllerEx.position.pixels == controllerEx.position.maxScrollExtent){
      if(listCompetitionsToCategoriesLoadMore.isNotEmpty){
        indexPage++;
      }
      competitionsRepo.getListCompetitions(indexPage.value, 20,textEditingControllerSearch.value.text).then((value) {
        if (value.state == Status.SUCCESS) {
          listCompetitionsToCategoriesLoadMore.value = [];
          competition.value = value.object!;
          listCompetitionsLoadMore.value =competition.value.items!;
          listCompetitionsToCategoriesLoadMore.value = listCompetitionsLoadMore.where((p0) => p0.category == listTitle[0].id).toList();
          listCompetitionsToCategories.addAll(listCompetitionsToCategoriesLoadMore);
          listCompetitions.addAll(listCompetitionsLoadMore);
          for(int i =0 ;i<listCompetitionsLoadMore.length;i++){
            listIsFollow.add(false);
          }
          listCompetitionsToCategories.refresh();
          AppUtils.shared.hideLoading();
          Future.delayed(const Duration(seconds: 1), () {
          });
        } else {
        }

      });

    }
  }



}



