import 'package:betting/commom/utils/color_utils.dart';
import 'package:betting/view/mobile/favourites_view/teams/team_info_view/squad_view/squad_item_widget/squad_item_widget.dart';
import 'package:betting/view/mobile/favourites_view/teams/team_info_view/squad_view/squad_view_controller.dart';
import 'package:betting/view/mobile/find/detail_player/detail_player_page.dart';
import 'package:betting/view/mobile/home_page/home_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SquadView extends StatelessWidget {
  SquadView({Key? key}) : super(key: key);
  final squadController = Get.put(SquadViewController());

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      onRefresh: () async {
        Get.find<HomeController>().onRefreshPressed();

      },
      color: ColorUtils.PRIMARY_COLOR,
      child: ListView.builder(
        itemCount: squadController.items.length,
        itemBuilder: (context, index) {
          var item = squadController.items[index];
          var key = item.keys.first;
          var value = item[key];

          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: Text(
                  key,
                  style: const TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      color: Colors.white),
                ),
              ),
              SizedBox(
                width: Get.width,
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  itemCount: value.length,
                  itemBuilder: (context, index) {
                    return GestureDetector(
                      onTap: () {
                        Navigator.push(context, MaterialPageRoute(builder: (context) => DetailPlayerPage(),) );
                      },
                      child: Container(
                          padding: const EdgeInsets.all(8),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.white70, width: 0.5),
                            borderRadius: (value.length == 1)? BorderRadius.circular(8):  (index == 0)
                                ? const BorderRadius.only(
                              topLeft: Radius.circular(8),
                              topRight: Radius.circular(8),
                            )
                                : (index == value.length - 1)
                                ? const BorderRadius.only(
                              bottomLeft: Radius.circular(8),
                              bottomRight: Radius.circular(8),
                            )
                                : BorderRadius.circular(0),
                          ),
                          margin: const EdgeInsets.symmetric(horizontal: 10),
                          child: SquadItemWidget(
                              shirtNumber: (key != 'COACH')
                                  ? value[index]['shirtNumber']
                                  : null,
                              namePlayers: value[index]['name'],
                              nation: value[index]['nation'],
                              flag: value[index]['flag'])),
                    );
                  },
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
