import 'package:boilerplate/presentation/screen/products/detail/product_detail_screen.dart';
import 'package:boilerplate/presentation/screen/products/list/widgets/product_list_search_panel.dart';
import 'package:boilerplate/presentation/widgets/base_container.dart';
import 'package:easy_search_bar/easy_search_bar.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../../core/di/injection_init.dart';
import '../../../../core/styles/my_colors.dart';
import '../../../../core/styles/my_dimensions.dart';
import '../../../../core/styles/my_font.dart';
import '../../../../core/utils/shared_preferences_util.dart';
import '../../../bloc/products_list/products_bloc.dart';
import '../../../widgets/ipm_data_not_found.dart';
import '../../../widgets/ipm_image.dart';
import 'widgets/product_list_result.dart';

class ProductListScreen extends StatefulWidget {
  final String? searchTxt;
  final int? inteId;
  final int? typeId;
  final String? myTitle;

  ProductListScreen(
      {Key? key, this.searchTxt, this.inteId, this.typeId, this.myTitle})
      : super(key: key);

  @override
  State<ProductListScreen> createState() => _ProductListScreenState();
}

class _ProductListScreenState extends State<ProductListScreen>
    with AutomaticKeepAliveClientMixin {
  late final ProductListBloc bloc;
  final List<String> _suggestions = [
    'Afeganistan',
    'Albania',
    'Algeria',
    'Australia',
    'Brazil',
    'German',
    'Madagascar',
    'Mozambique',
    'Portugal',
    'Zambia'
  ];
  String searchValue = '';
  bool _isLoadMore = true;
  late String lang = '';
  getLang() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    setState(() {
      lang = prefs.getString('lang').toString();
    });
    await FirebaseAnalytics.instance
        .logScreenView(screenName: 'ProductListPage');
  }

  final GlobalKey<RefreshIndicatorState> refreshKey =
      GlobalKey<RefreshIndicatorState>();
  @override
  void initState() {
    super.initState();

    bloc = getIt.get<ProductListBloc>();

    if (widget.inteId != null) {
      bloc.add(SelectIntellectualFilterEvent(widget.inteId!));
    } else if (widget.typeId != null) {
      bloc.add(SelectCategoriesFilterEvent(widget.typeId!));
    }

    bloc.add(GetProductFilterEvent());
    getLang();
  }

  void blocListener(BuildContext context, ProductListState state) {
    if (state is ProductSelectedState) {
      Navigator.push(
        context,
        CupertinoPageRoute(
          builder: (_) => BlocProvider.value(
            value: bloc,
            child: ProductDetailScreen(
              prodictId: state.data.selected!.id!,
            ),
          ),
        ),
      );
    }
  }

  @override
  void dispose() {
    bloc.close();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BlocProvider.value(
      value: bloc,
      child: BlocListener<ProductListBloc, ProductListState>(
        listener: blocListener,
        child: BaseContainer(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ProductListSearchPanel(
                searchTxt: widget.searchTxt,
              ),
              ProductListResult(
                showRefresh: widget.searchTxt == null,
                myTitle: widget.myTitle,
                searchTxt: widget.searchTxt,
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
