// ignore_for_file: must_be_immutable

import 'package:byod/bloc/menu_selected_bar.dart';
import 'package:byod/models/fav_group.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../helper/helper.dart';
import '../../helper/widget/style.dart';
import '../../models/category_list.dart';

class CategoryListHome extends StatefulWidget {
  List<CategoryList> categoryList;
  List<FavoriteGroup> favList;
  ScrollController scrollController;
  // ScrollController scrollControllerMenu;
  CategoryListHome({
    Key? key,
    required this.categoryList,
    required this.scrollController,
    required this.favList,
    // required this.scrollControllerMenu,
  }) : super(key: key);

  @override
  State<CategoryListHome> createState() => _CategoryListHomeState();
}

class _CategoryListHomeState extends State<CategoryListHome> {
  int selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<MenuSelectedBar, List<dynamic>>(
      builder: (contextMenuSelectedBar, selectedBarIndex) {
        selectedIndex = selectedBarIndex[0];
        return ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: widget.categoryList.length,
          itemBuilder: (context, i) {
            return GestureDetector(
              onTap: () {
                FocusScopeNode currentFocus = FocusScope.of(context);

                if (!currentFocus.hasPrimaryFocus) {
                  currentFocus.unfocus();
                }
                context
                    .read<MenuSelectedBar>()
                    .selectedBarMenu(i, widget.categoryList[i].id);
                widget.scrollController.animateTo(
                  (widget.favList.isNotEmpty) ? 371 : 0,
                  duration: Duration(milliseconds: animatedTime),
                  curve: Curves.fastOutSlowIn,
                );
              },
              child: Container(
                color: backgroundColor,
                margin: EdgeInsets.only(
                    left: (i == 0) ? 0 : 28.335,
                    right: (i == widget.categoryList.length - 1) ? 0 : 28.335),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    defaultText(
                      context,
                      widget.categoryList[i].name,
                      style: catNameStyle(),
                    ),
                    AnimatedSwitcher(
                      duration: Duration(milliseconds: animatedTime),
                      transitionBuilder:
                          (Widget child, Animation<double> animation) {
                        return ScaleTransition(scale: animation, child: child);
                      },
                      child: (selectedIndex == i)
                          ? Container(
                              key: const Key('underline'),
                              margin: const EdgeInsets.only(top: 27),
                              width: 30,
                              height: 2,
                              color: buttonColor)
                          : const SizedBox(
                              key: Key('underline'),
                            ),
                    )
                  ],
                ),
              ),
            );
          },
        );
      },
    );
  }
}
