import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/items_for_sale/get_sale_categories_list.dart';

class SelectCategoryFragment extends StatelessWidget {
  final Function changePageViewIndex;
  final List<GetSaleCategoriesList> getSaleCategoriesList;
  static late GetSaleCategoriesList selectedSaleCategory;

  SelectCategoryFragment({Key? key, required this.changePageViewIndex, required this.getSaleCategoriesList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          LocaleKeys.whatAreYouOffering.tr().toText20().paddingOnly(top: 0, left: 21, right: 21),
          getSaleCategoriesList.isNotEmpty
              ? GridView(
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, childAspectRatio: 105 / 105, crossAxisSpacing: 12, mainAxisSpacing: 12),
                  padding: const EdgeInsets.only(top: 15, bottom: 21, left: 21, right: 21),
                  shrinkWrap: true,
                  primary: false,
                  physics: const ScrollPhysics(),
                  children: getItemsForSaleWidgets(),
                )
              : Container(),
        ],
      ),
    );
  }

  List<Widget> getItemsForSaleWidgets() {
    List<Widget> itemsList = [];

    getSaleCategoriesList.forEach(
      (element) {
        itemsList.add(
          InkWell(
            onTap: () {
              selectedSaleCategory = element;
              changePageViewIndex(1);
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SvgPicture.string(element.content!, fit: BoxFit.contain, width: 32, height: 32),
                  ],
                ).expanded,
                element.title!.toText12()
              ],
            ).paddingOnly(left: 10, right: 10, bottom: 10, top: 16).expanded.objectContainerView(disablePadding: true),
          ),
        );
      },
    );

    return itemsList;
  }
}
