import 'package:boilerplate/core/i18n/translations.g.dart';
import 'package:boilerplate/core/styles/my_dimensions.dart';
import 'package:boilerplate/core/utils/navigator_util.dart';
import 'package:boilerplate/presentation/bloc/products_list/products_bloc.dart';
import 'package:boilerplate/presentation/screen/home/home_screen.dart';
import 'package:boilerplate/presentation/screen/products/list/product_list_filter_screen.dart';
import 'package:boilerplate/presentation/widgets/custom_transition_route.dart';
import 'package:boilerplate/presentation/widgets/ipm_search_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../landing/landing_screen.dart';

class ProductListSearchPanel extends StatefulWidget {
  final String? searchTxt;

  const ProductListSearchPanel({Key? key, this.searchTxt}) : super(key: key);

  @override
  State<ProductListSearchPanel> createState() => _ProductListSearchPanelState();
}

class _ProductListSearchPanelState extends State<ProductListSearchPanel> {
  late final TextEditingController searchCtrl;
  late String lang = '';

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

    searchCtrl = TextEditingController(text: widget.searchTxt ?? '');

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (searchCtrl.text.isNotEmpty) {
        BlocProvider.of<ProductListBloc>(context).add(
          GetProductsEvent(searchTxt: searchCtrl.text),
        );
      }
    });
    getLang();
  }

  @override
  void dispose() {
    searchCtrl.dispose();

    super.dispose();
  }

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

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

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: MediaQuery.of(context).padding.top + MyDimens.dimen16,
        bottom: MyDimens.dimen24,
        left: MyDimens.dimen16,
        right: MyDimens.dimen16,
      ),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(
            'assets/images/img_list_search_bg.png',
          ),
          fit: BoxFit.cover,
        ),
      ),
      child: Row(
        children: [
          if (ModalRoute.of(context)?.canPop ?? false)
            GestureDetector(
              onTap: () => NavigatorUtil.pushReplaceToPage(LandingScreem()),
              child: Padding(
                padding: EdgeInsets.only(right: MyDimens.dimen16),
                child: SvgPicture.asset(
                  'assets/icons/ic_back_button.svg',
                  color: Colors.white,
                  width: MyDimens.dimen24,
                  height: MyDimens.dimen24,
                ),
              ),
            ),
          Expanded(
            child: IPMSearchField(
              controller: searchCtrl,checkHome: "null",
              hintText: lang == 'th' || lang == ""
                  ? AppLocalizations.products.search_product
                  : AppLocalizations.products.search_product_en,
              onFieldSubmitted: (value) {
                BlocProvider.of<ProductListBloc>(context).add(
                  ShowRefreshEvent(textSearch: value),
                );
              },
            ),
          ),
          GestureDetector(
            onTap: () {
              BlocProvider.of<ProductListBloc>(context).add(
                OpenFilterEvent(),
              );
              Navigator.push(
                context,
                CustomTransitionRoute(
                  builder: (_) => BlocProvider.value(
                    value: BlocProvider.of<ProductListBloc>(context),
                    child: ProductListFilterScreen(),
                  ),
                  begin: Offset(1, 0),
                  end: Offset(0, 0),
                ),
              );
            },
            child: Container(
              color: Colors.transparent,
              padding: EdgeInsets.only(
                right: MyDimens.dimen10,
                left: MyDimens.dimen20,
              ),
              child: SvgPicture.asset(
                'assets/icons/ic_filter.svg',
                width: MyDimens.dimen24,
                height: MyDimens.dimen24,
              ),
            ),
          )
        ],
      ),
    );
  }
}
