import 'package:betting/commom/widget/loading_custom.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';
import 'package:intl/intl.dart';

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FixturesController>(builder: (controller) {
      return RefreshIndicator(
        onRefresh: () async {
          fixtureController.getListMatchesOnTeamFavourite();
          Get.find<HomeController>().onRefreshPressed();
          },
        child: fixtureController.isLoading.value ?
        Obx(() => ListView.builder(
          shrinkWrap: true,
          itemCount: fixtureController.listMatChesFavourite.length,
          itemBuilder: (context, index) {
            return Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                GestureDetector(
                  onTap: () {
                    Get.toNamed(Routes.detailTournamentPage, arguments: fixtureController.listMatChesFavourite[index]);
                    },
                  child: TournamentsWidget(
                    logo: fixtureController.listMatChesFavourite[index].logo ??
                        'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                    nameTournaments: fixtureController.listMatChesFavourite[index].name!,
                    typeTournaments: fixtureController.listMatChesFavourite[index].name!,
                    isFavourite: fixtureController.listMatChesFavourite[index].favourite,
                  ),
                ),
                SizedBox(
                  height: 10.h,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 8,
                  ),
                  child: ListView.builder(
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: fixtureController.listMatChesFavourite[index].matches?.length,
                    itemBuilder: (context, indexMatches) {
                      return GestureDetector(
                        onTap: () {
                          Get.toNamed(Routes.detailMatchesRegions, arguments: fixtureController.listMatChesFavourite[index].matches![indexMatches]);
                          },
                        child: Obx(
                              () => ItemFixtureWidget(
                                date: '${fixtureController.listMatChesFavourite[index].matches?[indexMatches].date}',
                                time: DateFormat.Hm().format(DateTime.parse('${fixtureController.listMatChesFavourite[index].matches?[indexMatches].date} ${fixtureController.listMatChesFavourite[index].matches![indexMatches].time!}')),
                                logoTeamHome: fixtureController.listMatChesFavourite[index].matches![indexMatches].home!.logo ?? 'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                                teamHomeName: fixtureController.listMatChesFavourite[index].matches![indexMatches].home!.name!,
                                logoTeamAway: fixtureController.listMatChesFavourite[index].matches![indexMatches].away!.logo ?? 'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                                teamNameAway: fixtureController.listMatChesFavourite[index].matches![indexMatches].away!.name!,
                                isHomeFavourite: fixtureController.listMatChesFavourite[index].matches![indexMatches].home!.favourite!,
                                isAwayFavourite: fixtureController.listMatChesFavourite[index].matches![indexMatches].away!.favourite!,
                                funcFavouriteMatch: () {
                                  Get.find<FootballController>().deleteCompetitionsFavorites(fixtureController.listMatChesFavourite[index].sId!);
                                  fixtureController.listMatChesFavourite.refresh();
                                }, isFavouriteMatch: true,
                              ),
                        ),
                      );
                      },
                  ),
                ),
              ],
            );
            },
        ),): const LoadingCustom(),
      );
   },);
  }
}
