import 'package:betting/commom/utils/color_utils.dart';
import 'package:betting/commom/utils/textstyle.dart';
import 'package:betting/commom/widget/cache_network_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/item_widget/item_widget.dart';
import 'package:betting/view/mobile/favourites_view/matches_view/list_fixtures/fixtures_tournaments/fixtures_tournaments_controller.dart';
import 'package:betting/view/mobile/favourites_view/widgets/toast_wiget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class FixtureTournaments extends StatelessWidget {
  FixtureTournaments({Key? key}) : super(key: key);
  final fixturesTournamentsController = Get.put(FixtureTournamentsController());

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Image.asset(
          "assets/images/background_app_betting.jpg",
          height: Get.height,
          width: Get.width,
          fit: BoxFit.cover,
        ),
        Scaffold(
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            automaticallyImplyLeading: false,
            bottom: PreferredSize(
              preferredSize: const Size.fromHeight(35),
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        IconButton(
                          icon: const Icon(
                            Icons.arrow_back_ios_new_rounded,
                            size: 18,
                            color: Colors.white54,
                          ),
                          onPressed: () {
                            Get.back();
                          },
                        ),
                        ItemWidget(
                            logo: fixturesTournamentsController.itemMatches.value?.logo ??
                                'https://upload.wikimedia.org/wikipedia/en/thumb/d/df/Bundesliga_logo_%282017%29.svg/800px-Bundesliga_logo_%282017%29.svg.png',
                            name: fixturesTournamentsController.itemMatches.value!.name!,
                            nation: fixturesTournamentsController.itemMatches.value!.name!, funcFavorite: () {
                          fixturesTournamentsController.itemMatches.value!.favourite = !fixturesTournamentsController.itemMatches.value!.favourite!;
                          CustomToast().showToast(context, 'competitions', fixturesTournamentsController.itemMatches.value!.favourite!);
                        }, isFavorite: fixturesTournamentsController.itemMatches.value!.favourite!,),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
          body: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const SizedBox(
                height: 10,
              ),
              Row(
                children: [
                  Container(
                    margin: const EdgeInsets.only(left: 8),
                    height: 35,
                    padding:
                        const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(25),
                        border: Border.all(color: Colors.transparent)),
                    alignment: Alignment.center,
                    child: Text('FIXTURES',
                        textAlign: TextAlign.center,
                        style: TextStyleUtils.sizeText13Weight600()),
                  ),
                ],
              ),
              Expanded(
                child: Obx(() => ListView.builder(
                    padding: EdgeInsets.zero,
                    itemCount: fixturesTournamentsController.matchFootballController.schedules.length,
                    shrinkWrap: true,
                    itemBuilder: (context, index) {
                      return Column(
                        children: [
                          InkWell(
                            child: Container(
                              margin: EdgeInsets.symmetric(vertical: 8.h),
                              child: Row(
                                children: [
                                  SizedBox(
                                    width: 8.w,
                                  ),
                                  Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        fixturesTournamentsController.matchFootballController.schedules[index].date,
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 14.sp,
                                            fontWeight: FontWeight.bold),
                                      ),
                                      //Text("FA Cup Final",style: TextStyle(color: Colors.white60,fontSize: 12.sp),),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.symmetric(horizontal: 8.w),
                            child: ListView.builder(
                              itemCount: fixturesTournamentsController.matchFootballController.schedules[index].matches.length,
                              shrinkWrap: true,
                              padding: EdgeInsets.zero,
                              physics: const NeverScrollableScrollPhysics(),
                              itemBuilder: (context, scheduleIndex) {
                                Matches item = fixturesTournamentsController.matchFootballController.schedules[index].matches[scheduleIndex];

                                return GestureDetector(
                                  onTap: () {
                                    Get.toNamed(Routes.detailMatchesRegions, arguments: item);
                                  },
                                  child: Container(
                                    margin: EdgeInsets.only(bottom: 4.h),
                                    padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 8.h),
                                    decoration: BoxDecoration(
                                        color: const Color.fromRGBO(255, 255, 255, 0.1),
                                        borderRadius: BorderRadius.circular(6.r)),
                                    child: Row(
                                      children: [
                                        Column(
                                          children: [
                                            Text(
                                              DateFormat('HH:mm').format(DateFormat('HH:mm:ss').parse(item.time!)),
                                              style: const TextStyle(
                                                  color: Colors.white),
                                            ),
                                          ],
                                        ),
                                        SizedBox(
                                          width: 8.w,
                                        ),
                                        Column(
                                          crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                          children: [
                                            Row(
                                              children: [
                                                SizedBox(
                                                  height: 18.h,
                                                  width: 18.h,
                                                  child: CacheNetWorkCustom(
                                                    urlImage: item.home?.logo ??
                                                        'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',
                                                  ),
                                                ),
                                                SizedBox(
                                                  width: 8.w,
                                                ),
                                                Text(
                                                  item.home!.name!,
                                                  style: const TextStyle(
                                                      color: Colors.white),
                                                ),
                                              ],
                                            ),
                                            SizedBox(
                                              height: 4.h,
                                            ),
                                            Row(
                                              children: [
                                                SizedBox(
                                                  height: 18.h,
                                                  width: 18.h,
                                                  child: CacheNetWorkCustom(urlImage: item.away?.logo ?? 'https://upload.wikimedia.org/wikipedia/vi/thumb/5/5c/Chelsea_crest.svg/1200px-Chelsea_crest.svg.png',),
                                                ),
                                                SizedBox(
                                                  width: 8.w,
                                                ),
                                                Text(
                                                  item.away!.name!,
                                                  style: const TextStyle(
                                                      color: Colors.white),
                                                ),
                                              ],
                                            )
                                          ],
                                        ),
                                        const Spacer(),
                                        GestureDetector(
                                          onTap: () {
                                            item.favourite = !item.favourite!;

                                            if (item.favourite == true) {
                                              fixturesTournamentsController.matchFootballController.addMatchesFavorites(item.sId!, index);
                                            } else {
                                              fixturesTournamentsController.matchFootballController.deleteMatchFavorites(item.sId!, index);
                                            }

                                            CustomToast().showToast(context, 'Match', item.favourite!);
                                          },
                                          child: Icon(
                                            item.favourite == false ? Icons.star_outline_rounded : Icons.star_rounded,
                                            color: ColorUtils.PRIMARY_COLOR,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                            ),
                          ),
                        ],
                      );
                    },
                  ),
                ),
              )
            ],
          ),
        ),
      ],
    );
  }
}
