import 'dart:io';
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/home_page/home_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../../commom/widget/cache_network_custom.dart';
import '../../../../commom/widget/custom_native_ads.dart';
import 'football_controller.dart';



class FootballPage extends GetView<FootballController> {
  @override
  final controller = Get.put(FootballController());

  FootballPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FootballController>(builder: (controller) {
      return controller.isReady.value?
      RefreshIndicator(child:
      Obx(() => ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: controller.listMatches.length,
        shrinkWrap: true,
        itemBuilder: (context, index) {
          return Obx(() => Column(
            children: [
              InkWell(
                onTap: () {
                 Get.toNamed(Routes.detailTournamentPage,
                    arguments: controller.listMatches[index],
                  );
                },
                child: Container(
                  margin: EdgeInsets.symmetric(vertical: 8.h),
                  child: Row(
                    children: [
                      const Icon(Icons.flag,color: Colors.white,),
                      SizedBox(width: 8.w,),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text('${controller.listMatches[index].name}',style: TextStyle(color: Colors.white,fontSize: 14.sp,fontWeight: FontWeight.bold),),
                          Text("${controller.listMatches[index].name} Tournaments",style: TextStyle(color: Colors.white60,fontSize: 12.sp),),
                        ],
                      ),
                      const Spacer(),
                      const Icon(Icons.arrow_forward_ios_outlined,color: Colors.white,size: 16,)
                    ],
                  ),
                ),
              ),
              Obx(() =>   ListView.builder(
                itemCount: controller.listMatches[index].matches?.length,
                shrinkWrap: true,
                padding: EdgeInsets.zero,
                physics: const NeverScrollableScrollPhysics(),
                itemBuilder: (context, indexMatches) {
                  return
                    itemListScore(controller, index, indexMatches);
                },)),
              index == 0 ?
              NativeAdsCustom(height: 130.h,nativeAdManager:controller.nativeAds[0] ,radius: 0.r,) :const SizedBox(),
              Visibility(
                  visible: Platform.isAndroid,
                  child: index == controller.listMatches.length/2 ?
                  NativeAdsCustom(height: 120.h,nativeAdManager: controller.nativeAds[1],radius: 0.r,) :const SizedBox()),
              Visibility(
                visible: Platform.isAndroid,
                child: index == controller.listMatches.length -1?
                NativeAdsCustom(height: 100.h,nativeAdManager: controller.nativeAds[2],radius: 0.r,):const SizedBox(),)
            ],
          ));
        },)), onRefresh:() async{
        Get.find<HomeController>().onRefreshPressed();
        controller. getListMatchesOnDay('',controller.outputDateToDateFormat.format(DateTime.now()),'');
        controller.listMatches.refresh();
        },)
          :const LoadingCustom();
    },);
  }

  InkWell itemListScore(FootballController controller, int index, int indexMatches) {
    return InkWell(
                  onTap: () {
                    Get.toNamed(Routes.detailMatchesRegions, arguments: controller.listMatches[index].matches![indexMatches]);
                  },
                  child: Container(
                    margin: EdgeInsets.only(bottom: 4.h),
                    padding: EdgeInsets.symmetric(vertical: 8.h),
                    decoration: BoxDecoration(
                        color: const Color.fromRGBO(255, 255, 255, 0.1),
                        borderRadius: BorderRadius.circular(6.r)
                    ),
                    child: Row(
                      children: [
                        controller.listMatches[index].matches![indexMatches].status =='IN PLAY'?
                        SizedBox(
                          width: 40.w,
                          child:   Row(
                            children: [
                              Container(
                                decoration: const BoxDecoration(
                                  borderRadius: BorderRadius.only(topRight: Radius.circular(6), bottomRight: Radius.circular(6)),
                                  color: ColorUtils.PRIMARY_COLOR,
                                ),
                                width: 4.w,
                                height: 40.h,
                              ),
                              SizedBox(width: 6.w,),
                              Text('${controller.listMatches[index].matches![indexMatches].matchTime}\'',style: const TextStyle(color:ColorUtils.PRIMARY_COLOR, fontWeight: FontWeight.bold))
                            ],
                          ),
                        ):
                        Column(
                          children: [
                            SizedBox(
                              width: 40.w,
                              child:  Text(controller.getMatchTimeText(index, indexMatches),textAlign: TextAlign.center,style: const TextStyle(color: Colors.white),),
                            ),
                          ],
                        ),
                        SizedBox(width: 8.w,),
                        Expanded(child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                SizedBox(
                                  height: 20.h,
                                  width:18.h,
                                  child: CacheNetWorkCustom(urlImage:
                                  controller.listMatches[index].matches![indexMatches].home?.logo!= null ?
                                  '${controller.listMatches[index].matches![indexMatches].home?.logo}':
                                    "https://upload.wikimedia.org/wikipedia/vi/thumb/c/c7/Logo_Real_Madrid.svg/1432px-Logo_Real_Madrid.svg.png"
                                    ,),
                                ),
                                SizedBox(width: 8.w,),
                                RichText(text: TextSpan(
                                    children: [
                                      TextSpan(text:"${controller.listMatches[index].matches![indexMatches].home!.name}",style: const TextStyle(color: Colors.white)),
                                      WidgetSpan(child: controller.listMatches[index].matches![indexMatches].home!.favourite == true ?  Container(
                                        margin: EdgeInsets.only(left: 4.w),
                                        child: const Icon(Icons.star,color:Colors.grey, size: 16,),): Container(),
                                      )
                                    ]
                                )),
                                SizedBox(width: 8.w,),
                                const Spacer(),
                                controller.listMatches[index].matches![indexMatches].status =='FINISHED'?
                                Text( controller.listMatches[index].matches![indexMatches].ftScore!=""?controller.listMatches[index].matches![indexMatches].ftScore!.substring(0,1):'',style: const TextStyle(color: Colors.white),):
                                controller.listMatches[index].matches![indexMatches].status =='IN PLAY'?
                                controller.listMatches[index].matches![indexMatches].score!= "" ?
                                Text(controller.listMatches[index].matches![indexMatches].score!.substring(0,1),style: const TextStyle(color: Colors.white),):
                                const Text('0',style: TextStyle(color: Colors.white)):
                                const Text(''),
                              ],
                            ),
                            SizedBox(height: 4.h,),
                            Row(
                              children: [
                                SizedBox(
                                  height: 20.h,
                                  width:  18.h,
                                  child: CacheNetWorkCustom(urlImage:
                                  controller.listMatches[index].matches?[indexMatches].away?.logo!= null ?
                                  '${controller.listMatches[index].matches?[indexMatches].away?.logo}' :
                                  "https://upload.wikimedia.org/wikipedia/vi/thumb/1/1d/Manchester_City_FC_logo.svg/2048px-Manchester_City_FC_logo.svg.png",),
                                ),
                                SizedBox(width: 6.5.w,),
                                SizedBox(
                                  width: 160.w,
                                  child: Text("${controller.listMatches[index].matches![indexMatches].away!.name}",style: const TextStyle(color: Colors.white),),

                                ),
                                SizedBox(width: 6.5.w,),

                                controller.listMatches[index].matches![indexMatches].away!.favourite == true ? const Icon(Icons.star,color:Colors.grey, size: 16,): Container(),
                                const Spacer(),
                                controller.listMatches[index].matches![indexMatches].status =='FINISHED'?
                                Text(controller.listMatches[index].matches![indexMatches].ftScore!="" ? controller.listMatches[index].matches![indexMatches].ftScore!.substring(4,5):'',style: const TextStyle(color: Colors.white),):
                                controller.listMatches[index].matches![indexMatches].status =='IN PLAY'?
                                controller.listMatches[index].matches![indexMatches].score!= "" ?
                                Text(controller.listMatches[index].matches![indexMatches].score!.substring(4,5),style: const TextStyle(color: Colors.white),):
                                const Text('0',style: TextStyle(color: Colors.white)):
                                const Text(''),
                              ],
                            )
                          ],
                        ),),
                        SizedBox(width: 8.w,),
                        controller.listMatches[index].matches![indexMatches].status =='INIT' ?
                        InkWell(
                          onTap: (){
                            controller.listMatches[index].matches![indexMatches].favourite =! controller.listMatches[index].matches![indexMatches].favourite!;
                            if(controller.listMatches[index].matches![indexMatches].favourite ==true){
                              controller.addMatchesFavorites(controller.listMatches[index].matches![indexMatches].sId!, index);
                              controller.listMatches.refresh();
                            }else{
                              controller.deleteCompetitionsFavorites( controller.listMatches[index].matches![indexMatches].sId!);
                            }
                          },
                          child: controller.listMatches[index].matches![indexMatches].favourite==true ?
                          const Icon(Icons.star_rounded,color:ColorUtils.PRIMARY_COLOR,) :
                          const Icon(Icons.star_border_rounded,color: Colors.white,),
                        ) : const Icon(Icons.star_border_rounded,color: Colors.grey,),
                        SizedBox(width: 8.w,),
                      ],
                    ),
                  ),
                );
  }
}
