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/teams/team_info_view/matches_view/result_team_info/result_team_info_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/favourites_view/teams/team_info_view/team_info_controller.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 ResultTeamInfo extends StatelessWidget {
  ResultTeamInfo({Key? key}) : super(key: key);
  final resultTeamInfoController = Get.put(ResultTeamInfoController());

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

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

                            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(
                                      date: 'FT',
                                      time: '${resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches?[indexMatches].date}',
                                      logoHomeTeam: resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].home!.logo ??
                                          'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                                      teamHomeName: resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].home!.name!,
                                      logoAwayTeam: resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].away!.logo ??
                                          'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                                      teamAwayName: resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].away!.name!,
                                      scoreHomeTeam: homeScore,
                                      scoreAwayTeam: awayScore,
                                      result: '',
                                      isHomeFavourite: resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].home!.favourite!,
                                      isAwayFavourite: resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].away!.favourite!,),
                                  ),

                                  GestureDetector(
                                    onTap: () {
                                    },
                                    child: resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].home!.sId! == Get.find<TeamInfoController>().itemTeam.value.id?
                                    int.parse( resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].ftScore!.substring(0,1)) > int.parse( resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].ftScore!.substring(4,5))?
                                    Container(
                                      padding: const EdgeInsets.all(10),
                                      alignment: Alignment.center,
                                      decoration: const BoxDecoration(
                                        shape: BoxShape.circle,
                                        color: Colors.green
                                      ),
                                      margin: const EdgeInsets.only(right: 8),
                                      child: const Text('W')) : int.parse(resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].ftScore!.substring(0,1)) == int.parse( resultTeamInfoController.listMatChesFavouriteOfTeamResult[index].matches![indexMatches].ftScore!.substring(4,5)) ?
                                    Container(
                                        padding: const EdgeInsets.all(10),
                                        alignment: Alignment.center,
                                        decoration: const BoxDecoration(
                                            shape: BoxShape.circle,
                                            color: Colors.grey
                                        ),
                                        margin: const EdgeInsets.only(right: 8),
                                        child: const Text('D')):
                                    Container(
                                        padding: const EdgeInsets.all(10),
                                        alignment: Alignment.center,
                                        decoration: const BoxDecoration(
                                            shape: BoxShape.circle,
                                            color: Colors.red
                                        ),
                                        margin: const EdgeInsets.only(right: 8),
                                        child: const Text('L')) : Container()
                                  ),
                                ],
                              ),
                            );
                          },
                          ),
                        );
                      },)
                ),
              ],
            );
          },
        ),)
      );
    },);
  }
}
