import 'package:betting/commom/utils/color_utils.dart';
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/widgets/tournaments_widget.dart';
import 'package:betting/view/mobile/favourites_view/matches_view/list_results/results_screen_controller.dart';
import 'package:betting/view/mobile/favourites_view/teams/team_info_view/matches_view/result_team_info/widgets/item_result_team_info.dart';
import 'package:betting/view/mobile/home_page/home_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class ResultScreen extends StatelessWidget {
  ResultScreen({Key? key}) : super(key: key);
  final resultsScreenController = Get.put(ResultsScreenController());

  @override
  Widget build(BuildContext context) {
    return
     GetBuilder<ResultsScreenController>(builder: (controller) {
       return  RefreshIndicator(
         onRefresh: () async{
           resultsScreenController.getListMatchesOnTeamFavouriteResult();
           Get.find<HomeController>().onRefreshPressed();

         },
         child:
        resultsScreenController.isLoading.value ?
        Obx(() => ListView.builder(
             itemCount: resultsScreenController.listMatchesFavourite.length,
             itemBuilder: (context, index) {
               return Column(
                 children: [
                   SizedBox(height: 10.h,),
                   GestureDetector(
                     onTap: () {
                       Get.toNamed(Routes.fixturesTournament);
                     },
                     child: TournamentsWidget(
                         logo: resultsScreenController.listMatchesFavourite[index].logo!=null ? resultsScreenController.listMatchesFavourite[index].logo!: 'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                         nameTournaments: resultsScreenController.listMatchesFavourite[index].name!,
                         typeTournaments: resultsScreenController.listMatchesFavourite[index].name!),
                   ),
                   SizedBox(height: 10.h,),
                   Container(
                       padding: const EdgeInsets.symmetric(horizontal: 8,),
                       child: ListView.builder(
                         physics: const NeverScrollableScrollPhysics(),
                         shrinkWrap: true,
                         itemCount: resultsScreenController.listMatchesFavourite[index].matches?.length,
                         itemBuilder: (context, indexMatches) {
                           return GestureDetector(
                             onTap: () {
                               Get.toNamed(Routes.detailMatchesRegions);
                             },
                             child: Obx(() {
                               int homeScore = int.parse(resultsScreenController.listMatchesFavourite[index].matches![indexMatches].score!.substring(0, 1));
                               int awayScore = int.parse(resultsScreenController.listMatchesFavourite[index].matches![indexMatches].score![resultsScreenController.listMatchesFavourite[index].matches![indexMatches].score!.length - 1]);

                               return Container(
                                 margin: const EdgeInsets.only(bottom: 8),
                                 decoration: BoxDecoration(
                                   color: Colors.grey.withOpacity(0.08),
                                   borderRadius: const BorderRadius.all(Radius.circular(8),),),
                                 child: Row(
                                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                   crossAxisAlignment: CrossAxisAlignment.center,
                                   children: [
                                     Expanded(
                                       child: ItemResultMatch(
                                         time: resultsScreenController.listMatchesFavourite[index].matches![indexMatches].matchTime!,
                                         logoHomeTeam: resultsScreenController.listMatchesFavourite[index].matches![indexMatches].home!.logo ??
                                             'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                                         teamHomeName: resultsScreenController.listMatchesFavourite[index].matches![indexMatches].home!.name!,
                                         logoAwayTeam: resultsScreenController.listMatchesFavourite[index].matches![indexMatches].away!.logo ??
                                             'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                                         teamAwayName: resultsScreenController.listMatchesFavourite[index].matches![indexMatches].away!.name!,
                                         scoreHomeTeam: homeScore,
                                         scoreAwayTeam: awayScore,
                                         result: '',
                                         isHomeFavourite: resultsScreenController.listMatchesFavourite[index].matches![indexMatches].home!.favourite!,
                                         isAwayFavourite: resultsScreenController.listMatchesFavourite[index].matches![indexMatches].away!.favourite!,),
                                     ),

                                     GestureDetector(
                                       onTap: () {
                                         bool canDelete = resultsScreenController.listMatchesFavourite[index].matches![indexMatches].home!.favourite! == false &&
                                             resultsScreenController.listMatchesFavourite[index].matches![indexMatches].away!.favourite! == false;

                                         if(canDelete == true) {
                                           resultsScreenController.delMatchFavourite(resultsScreenController.listMatchesFavourite[index].matches![indexMatches].sId!);
                                         }
                                       },
                                       child: (resultsScreenController.listMatchesFavourite[index].matches![indexMatches].favourite!) ?
                                       Container(
                                         margin: const EdgeInsets.only(right: 8),
                                         child: Icon(Icons.star_rounded, color: ColorUtils.PRIMARY_COLOR, size: 22.sp,),)
                                           : Container(margin: const EdgeInsets.only(right: 8), child: Icon(Icons.star_outline_rounded, size: 22.sp, color: Colors.white70,),),
                                     ),
                                   ],
                                 ),
                               );
                             },
                             ),
                           );
                         },)
                   ),
                 ],
               );
             },
           ),)
            : const LoadingCustom(),
       );
     },);
  }
}
