import 'package:boilerplate/core/i18n/translations.g.dart';
import 'package:boilerplate/core/styles/my_colors.dart';
import 'package:boilerplate/core/styles/my_dimensions.dart';
import 'package:boilerplate/presentation/bloc/products_list/products_bloc.dart';
import 'package:boilerplate/presentation/widgets/base_scaffold.dart';
import 'package:boilerplate/presentation/widgets/ipm_button.dart';
import 'package:boilerplate/presentation/widgets/ipm_title_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../../core/styles/my_font.dart';
import '../../../../domain/entities/product/product_list_filter_data.dart';
import 'widgets/product_list_categories_filter.dart';
import 'widgets/product_list_intellectual_filter.dart';
import 'widgets/product_list_price_filter.dart';

class ProductListFilterScreen extends StatefulWidget {
  @override
  State<ProductListFilterScreen> createState() =>
      _ProductListFilterScreenState();
}

class _ProductListFilterScreenState extends State<ProductListFilterScreen> {
  final GlobalKey<FormState> priceFormkey = GlobalKey<FormState>();
  late String lang = '';
  String selectedValue = "filter1";
  List<DropdownMenuItem<String>> get dropdownItems {
    List<DropdownMenuItem<String>> menuItems = [
      DropdownMenuItem(child: Text(" ล่าสุด"), value: "filter1"),
      DropdownMenuItem(child: Text(" เก่าที่สุด"), value: "filter2"),
      DropdownMenuItem(child: Text(" ก > ฮ"), value: "filter3"),
      DropdownMenuItem(child: Text(" ฮ > ก"), value: "filter4"),
    ];
    return menuItems;
  }

  List<DropdownMenuItem<String>> get dropdownItems_en {
    List<DropdownMenuItem<String>> menuItems_en = [
      DropdownMenuItem(child: Text(" Lasted"), value: "filter1"),
      DropdownMenuItem(child: Text(" Old"), value: "filter2"),
      DropdownMenuItem(child: Text(" A > Z"), value: "filter3"),
      DropdownMenuItem(child: Text(" Z > A"), value: "filter4"),
    ];
    return menuItems_en;
  }

  @override
  void dispose() {
    super.dispose();
  }

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

    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 WillPopScope(
        onWillPop: () {
          BlocProvider.of<ProductListBloc>(context).add(
            CloseFilterEvent(),
          );
          return Future.value(true);
        },
        child: Container(
          color: Colors.black.withOpacity(0.4),
          padding: EdgeInsets.only(left: MyDimens.dimen35),
          child: BaseScaffold(
            hideAppBar: true,
            body: Column(
              children: [
                Expanded(
                  child: Padding(
                    padding: EdgeInsets.only(
                      top:
                          MediaQuery.of(context).padding.top + MyDimens.dimen16,
                      left: MyDimens.dimen16,
                      right: MyDimens.dimen16,
                    ),
                    child: SingleChildScrollView(
                      padding: EdgeInsets.only(
                        bottom: MediaQuery.of(context).padding.bottom +
                            MyDimens.dimen16,
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Expanded(
                                child: IPMTitleText(
                                  title1: lang == "th" || lang == ""
                                      ? AppLocalizations.products.filter_title_1
                                      : AppLocalizations
                                          .products.filter_title_1_en,
                                  title2: lang == "th" || lang == ""
                                      ? AppLocalizations.products.filter_title_2
                                      : AppLocalizations
                                          .products.filter_title_2_en,
                                ),
                              ),
                              IconButton(
                                onPressed: () {
                                  BlocProvider.of<ProductListBloc>(context).add(
                                    CloseFilterEvent(),
                                  );
                                  Navigator.pop(context);
                                },
                                icon: Icon(
                                  Icons.close,
                                  size: MyDimens.dimen24,
                                ),
                              )
                            ],
                          ),
                          Row(
                            children: [
                              // SizedBox(
                              //   width: MyDimens.dimen16,
                              // ),
                              Text(
                                lang == 'th' || lang == ""
                                    ? "แสดงผลงานตาม"
                                    : "Show",
                                style: MyFont.medium16,
                              ),
                              SizedBox(
                                width: MyDimens.dimen16,
                              ),
                              Container(
                                width: MyDimens.dimen100,
                                height: MyDimens.dimen52,
                                child: DropdownButtonFormField(
                                    isExpanded: true,
                                    decoration: InputDecoration(
                                      enabledBorder: OutlineInputBorder(
                                        borderSide: BorderSide(
                                            color: MyColor.orange, width: 1),
                                        borderRadius: BorderRadius.circular(20),
                                      ),
                                      border: OutlineInputBorder(
                                        borderSide: BorderSide(
                                            color: MyColor.orange, width: 1),
                                        borderRadius: BorderRadius.circular(20),
                                      ),
                                      focusedBorder: OutlineInputBorder(
                                        borderSide: BorderSide(
                                            color: MyColor.orange, width: 1),
                                        borderRadius: BorderRadius.circular(20),
                                      ),
                                      filled: true,
                                      fillColor: Colors.white,
                                    ),
                                    dropdownColor: Colors.white,
                                    value: selectedValue,
                                    onChanged: (String? newValue) {
                                      setState(() {
                                        selectedValue = newValue!;
                                        print(selectedValue);
                                        if (selectedValue == "filter1") {
                                          BlocProvider.of<ProductListBloc>(
                                                  context)
                                              .add(
                                            SelectSortEndEvent("desc", "id"),
                                          );
                                        } else if (selectedValue == "filter2") {
                                          BlocProvider.of<ProductListBloc>(
                                                  context)
                                              .add(
                                            SelectSortEndEvent("asc", "id"),
                                          );
                                        } else if (selectedValue == "filter3") {
                                          BlocProvider.of<ProductListBloc>(
                                                  context)
                                              .add(
                                            SelectSortEndEvent("asc", "name"),
                                          );
                                        } else {
                                          BlocProvider.of<ProductListBloc>(
                                                  context)
                                              .add(
                                            SelectSortEndEvent("desc", "name"),
                                          );
                                        }
                                      });
                                    },
                                    style: MyFont.medium14,
                                    items: lang == 'th' || lang == ''
                                        ? dropdownItems
                                        : dropdownItems_en),
                              ),
                            ],
                          ),
                          ProductListIntellectualFilter(),
                          ProductListCategoriesFilter(),
                          ProductListPriceFilter(formkey: priceFormkey),
                        ],
                      ),
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(
                    right: MyDimens.dimen16,
                    left: MyDimens.dimen16,
                    bottom: MediaQuery.of(context).padding.bottom +
                        MyDimens.dimen12,
                  ),
                  child: Row(
                    children: [
                      Expanded(
                        child: IPMButton(
                          text: lang == 'th' || lang == ""
                              ? AppLocalizations.products.clean
                              : AppLocalizations.products.clean_en,
                          textColor: MyColor.headerColor,
                          backgroundColor: Colors.white,
                          borderColor: MyColor.headerColor,
                          onPressed: () {
                            BlocProvider.of<ProductListBloc>(context).add(
                              CleanFilterEvent(),
                            );
                          },
                        ),
                      ),
                      SizedBox(width: MyDimens.dimen14),
                      Expanded(
                        child: IPMButton(
                          text: lang == 'th' || lang == ""
                              ? AppLocalizations.products.saerch
                              : AppLocalizations.products.saerch_en,
                          onPressed: () {
                            if (priceFormkey.currentState?.validate() ??
                                false) {
                              BlocProvider.of<ProductListBloc>(context).add(
                                SubmitFilterEvent(),
                              );
                              Navigator.pop(context);
                            }
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    });
  }
}
