import 'package:betting/commom/widget/loading_custom.dart';
import 'package:betting/data/model/res/matches/matches.dart';
import 'package:betting/routes/app_pages.dart';
import 'package:betting/view/mobile/favourites_view/matches_view/list_fixtures/fixtures_screen_controller.dart';
import 'package:betting/view/mobile/favourites_view/matches_view/list_fixtures/widgets/item_fixture_widget.dart';
import 'package:betting/view/mobile/favourites_view/matches_view/list_fixtures/widgets/tournaments_widget.dart';
import 'package:betting/view/mobile/home_page/home_controller.dart';
import 'package:betting/view/mobile/scores/football/football_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class FixturesScreen extends StatelessWidget {
  FixturesScreen({Key? key}) : super(key: key);
  final controller = Get.put(FixturesController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FixturesController>(builder: (controller) {
      return RefreshIndicator(
        onRefresh: () async {
          controller.getListMatchesOnTeamFavourite();
          Get.find<HomeController>().onRefreshPressed();
          },
        child: controller.isLoading.value ?
        Obx(() => ListView.builder(
          shrinkWrap: true,
          itemCount: controller.listMatChesFavourite.length,
          itemBuilder: (context, index) {
            return Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                GestureDetector(
                  onTap: () {
                    Get.toNamed(Routes.detailTournamentPage, arguments: controller.listMatChesFavourite[index]);
                    },
                  child: TournamentsWidget(
                    logo: controller.listMatChesFavourite[index].logo ??
                        'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                    nameTournaments: controller.listMatChesFavourite[index].name!,
                    typeTournaments: controller.listMatChesFavourite[index].name!,
                    isFavourite: controller.listMatChesFavourite[index].favourite,
                  ),
                ),
                SizedBox(
                  height: 10.h,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 8,
                  ),
                  child: ListView.builder(
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: controller.listMatChesFavourite[index].matches?.length,
                    itemBuilder: (context, indexMatches) {
                      Matches? match = controller.listMatChesFavourite[index].matches?[indexMatches];

                      return GestureDetector(
                        onTap: () {
                          Get.toNamed(Routes.detailMatchesRegions, arguments: controller.listMatChesFavourite[index].matches![indexMatches]);
                          },
                        child: 
                        Obx(() => ItemFixtureWidget(
                          isNotification: Get.find<HomeController>().isNotification.value,
                          match: match,
                          funcFavouriteMatch: () {
                            Get.find<HomeController>().isNotification.value ==true ?
                            controller.listMatChesFavourite[index].matches![indexMatches].favourite =! controller.listMatChesFavourite[index].matches![indexMatches].favourite!:
                            Get.find<FootballController>().deleteCompetitionsFavorites(controller.listMatChesFavourite[index].sId!);
                            controller.listMatChesFavourite.refresh();
                          },
                          isFavouriteMatch:
                          Get.find<HomeController>().isNotification.value ==true ?
                            controller.listMatChesFavourite[index].matches![indexMatches].favourite!: true,
                        )),
                      );
                      },
                  ),
                ),
              ],
            );
            },
        ),): const LoadingCustom(),
      );
   },);
  }
}
