import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../../../core/i18n/translations.g.dart';
import '../../../../../core/styles/my_dimensions.dart';
import '../../../../../core/styles/my_font.dart';
import '../../../../bloc/products_list/products_bloc.dart';
import 'product_list_filter_chip.dart';

class ProductListIntellectualFilter extends StatefulWidget {
  ProductListIntellectualFilter({Key? key}) : super(key: key);

  @override
  State<ProductListIntellectualFilter> createState() =>
      _ProductListIntellectualFilterState();
}

class _ProductListIntellectualFilterState
    extends State<ProductListIntellectualFilter> {
  bool isShowAll = false;
  late String lang = "";
  String selectedValue = "USA";
  List<DropdownMenuItem<String>> get dropdownItems {
    List<DropdownMenuItem<String>> menuItems = [
      DropdownMenuItem(child: Text("USA"), value: "USA"),
      DropdownMenuItem(child: Text("Canada"), value: "Canada"),
      DropdownMenuItem(child: Text("Brazil"), value: "Brazil"),
      DropdownMenuItem(child: Text("England"), value: "England"),
    ];
    return menuItems;
  }

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      setState(() {
        final bloc = BlocProvider.of<ProductListBloc>(context);

        isShowAll =
            bloc.state.data.filterData?.intellectualSelected.isNotEmpty ??
                false;
      });
    });
    getLang();
  }

  getLang() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    setState(() {
      lang = prefs.getString('lang').toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ProductListBloc, ProductListState>(
      builder: (context, state) {
        return Padding(
          padding: EdgeInsets.only(top: MyDimens.dimen12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                lang == 'th' || lang == ""
                    ? AppLocalizations.products.intellectual_filter_title
                    : AppLocalizations.products.intellectual_filter_title_en,
                style: MyFont.medium16,
              ),
              SizedBox(height: MyDimens.dimen8),

              RichText(
                maxLines: 99,
                text: TextSpan(
                  children: [
                    ...List.generate(
                      state.data.filterData?.intellectualType.length ?? 0,
                      (index) {
                        final item =
                            state.data.filterData!.intellectualType[index];

                        return WidgetSpan(
                          child: Padding(
                            padding: EdgeInsets.only(
                                right: MyDimens.dimen8,
                                top: MyDimens.dimen8,
                                bottom: MyDimens.dimen8),
                            child: ProductListFilterChip(
                              initialValue: state
                                  .data.filterData!.intellectualSelected
                                  .where((element) => element == item.id)
                                  .isNotEmpty,
                              id: item.id,
                              text: item.name ?? '',
                              onSelect: (id) {
                                BlocProvider.of<ProductListBloc>(context).add(
                                  SelectIntellectualFilterEvent(id),
                                );
                              },
                            ),
                          ),
                        );
                      },
                    ),
                  ],
                ),
              ),
              // if (!isShowAll)
              //   Container(
              //     alignment: Alignment.center,
              //     padding: EdgeInsets.only(top: MyDimens.dimen12),
              //     child: GestureDetector(
              //       onTap: () {
              //         setState(() {
              //           isShowAll = true;
              //         });
              //       },
              //       child: Text(
              //         AppLocalizations.products.show_more,
              //         style: MyFont.medium12.copyWith(
              //           color: Color(0xFF494949),
              //         ),
              //       ),
              //     ),
              //   ),
            ],
          ),
        );
      },
    );
  }
}
