import 'package:boilerplate/core/i18n/translations.g.dart';
import 'package:boilerplate/core/styles/my_dimensions.dart';
import 'package:boilerplate/core/utils/authentication_util.dart';
import 'package:boilerplate/core/utils/navigator_util.dart';
import 'package:boilerplate/core/utils/shared_preferences_util.dart';
import 'package:boilerplate/presentation/bloc/notification/notification_count/notification_count_state.dart';
import 'package:boilerplate/presentation/screen/notification/notifications_screen.dart';
import 'package:boilerplate/presentation/screen/products/favorite/favorite_list_screen.dart';
import 'package:boilerplate/presentation/screen/products/list/product_list_screen.dart';
import 'package:boilerplate/presentation/widgets/ipm_search_field.dart';
import 'package:boilerplate/presentation/widgets/notification_count.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_badger/flutter_app_badger.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../../core/di/injection_init.dart';
import '../../../../core/styles/my_font.dart';
import '../../../../domain/entities/flavor/flavor_data.dart';
import '../../../bloc/notification/notification_count/notification_count_cubit.dart';
import '../../web_view/web_view_screen.dart';

class HomeSearch extends StatelessWidget {
  final TextEditingController controller;

  final String? myLang;
  final int? notiCount;

  HomeSearch({Key? key, this.myLang, this.notiCount, required this.controller})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: MediaQuery.of(context).padding.top + MyDimens.dimen16,
        left: MyDimens.dimen16,
        right: MyDimens.dimen16,
        bottom: MyDimens.dimen18,
      ),
      child: Column(
        children: [
          Image.asset(
            'assets/images/img_ipm_logo.png',
            width: MyDimens.dimen100,
          ),
          SizedBox(height: MyDimens.dimen16),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              IPMSearchField(
                checkHome: "YES",
                controller: this.controller,
                hintText: AppLocalizations.home.search_product,
                onFieldSubmitted: (v) {
                  NavigatorUtil.pushToPage(ProductListScreen(searchTxt: v));
                },
              ),
              Column(
                children: [
                  Row(
                    children: [
                      GestureDetector(
                        onTap: () {
                          NavigatorUtil.pushToPage(WebViewScreen(
                            title: AppLocalizations.menu.cart,
                            url:
                                'https://ipmart.ipthailand.go.th/me?tabs=tabs-4#tabs-4',
                          ));
                        },
                        child: Container(
                          margin:
                              EdgeInsets.symmetric(horizontal: MyDimens.dimen8),
                          child: Icon(
                            Icons.shopping_cart_outlined,
                            color: Colors.white,
                            size: 27,
                          ),
                        ),
                      ),
                      GestureDetector(
                        onTap: () {
                          AuthenticationUtil.checkHasUser(() {
                            NavigatorUtil.pushToPage(FavoritesScreen());
                          });
                        },
                        child: Container(
                          margin:
                              EdgeInsets.symmetric(horizontal: MyDimens.dimen8),
                          child: SvgPicture.asset(
                            'assets/icons/ic_favorite.svg',
                            fit: BoxFit.fitWidth,
                          ),
                        ),
                      ),
                      GestureDetector(
                        onTap: () async {
                          BlocProvider.of<NotificationCountCubit>(context)
                              .clearNotificationCount();

                          final pref = getIt.get<SharedPreferencesUtil>();

                          pref.putInt(
                              SharedPreferencesUtil.keyNotificationCount, 0);

                          FlutterAppBadger.removeBadge();
                          FlutterAppBadger.updateBadgeCount(0);
                          if (NavigatorUtil.navigate.currentState != null) {
                            BlocProvider.of<NotificationCountCubit>(
                                    NavigatorUtil
                                        .navigate.currentState!.context)
                                .clearNotificationCount();
                          }
                          FlutterAppBadger.removeBadge();

                          await AuthenticationUtil.checkHasUser(() {
                            NavigatorUtil.pushToPage(NotificationsScreen());
                          });
                        },
                        child: Stack(
                          clipBehavior: Clip.none,
                          children: [
                            Container(
                              margin: EdgeInsets.symmetric(
                                  horizontal: MyDimens.dimen8),
                              child: SvgPicture.asset(
                                'assets/icons/ic_notification.svg',
                                fit: BoxFit.fitWidth,
                              ),
                            ),
                            BlocBuilder<NotificationCountCubit,
                                NotificationCountState>(
                              builder: (context, state) {
                                if (state is HaveNotificaitonCountState) {
                                  return Positioned(
                                    top: -(MyDimens.dimen2),
                                    right: MyDimens.dimen8,
                                    child: NotificationCount(count: 1),
                                  );
                                } else {
                                  return Container();
                                }
                              },
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  GestureDetector(
                    onTap: () async {
                      SharedPreferences prefs =
                          await SharedPreferences.getInstance();

                      if (prefs.getString('lang') == "" ||
                          prefs.getString('lang') == null ||
                          prefs.getString('lang') == "EN") {
                        prefs.setString('lang', 'TH');
                      } else {
                        prefs.setString('lang', 'TH');
                      }
                      // setState(() {});
                    },
                    child: Container(
                      width: 140,
                      margin: EdgeInsets.only(left: 8, top: 8, right: 8),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Spacer(),
                          Image.asset(
                            'assets/images/icons8-globe-48.png',
                            width: 30,
                            height: 30,
                          ),
                          // SizedBox(
                          //   width: 8,
                          // ),
                          // Spacer(),
                          SizedBox(
                            width: 8,
                          ),
                          Text(
                            'ไทย',
                            style:
                                MyFont.medium14.copyWith(color: Colors.white),
                          ),
                          SizedBox(
                            width: 8,
                          ),
                          Text(
                            '|',
                            style:
                                MyFont.medium14.copyWith(color: Colors.white),
                          ),
                          SizedBox(
                            width: 8,
                          ),
                          Text(
                            'EN',
                            style:
                                MyFont.medium14.copyWith(color: Colors.white),
                          ),
                          SizedBox(
                            width: 8,
                          ),
                          Spacer(),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }
}
