import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/config/shared_pref_kay.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/models/Authentication/select_device_imei_res.dart';
import 'package:hmg_patient_app/routes.dart';
import 'package:hmg_patient_app/services/authentication/auth_provider.dart';
import 'package:hmg_patient_app/uitl/app_shared_preferences.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class BottomNavPharmacyItem extends StatelessWidget {
  final String? title;
  final IconData? icon;

  final ValueChanged<int> changeIndex;
  final int? index;
  final int? currentIndex;
  final VoidCallbackAction? onTap;
  final bool? isHome;

  final IconData? activeIcon;

  BottomNavPharmacyItem({required this.icon, required this.changeIndex, this.index, this.currentIndex, this.activeIcon, this.title, this.onTap, this.isHome = false});

  AppSharedPreferences sharedPref = AppSharedPreferences();

  @override
  Widget build(BuildContext context) {
    OrderPreviewViewModel orderPreviewViewModel = Provider.of(context);
    ProjectViewModel projectViewModel = Provider.of(context);

    return Expanded(
      child: SizedBox(
        height: 66.0,
        child: Stack(
          clipBehavior: Clip.none,
          children: [
            Material(
              type: MaterialType.transparency,
              child: InkWell(
                highlightColor: Colors.transparent,
                splashColor: Colors.transparent,
                onTap: () {
                  if (!Provider.of<ProjectViewModel>(context, listen: false).isLogin && (currentIndex == 2 || currentIndex == 3))
                    login(context);
                  else
                    changeIndex(currentIndex!);
                },
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    currentIndex == index
                        ? Divider(
                            color: Color(0xff5AB133),
                            thickness: 3.5,
                          )
                        : Divider(
                            thickness: 0,
                          ),

                    Container(
                      child: Icon(currentIndex == index ? activeIcon : icon,
                          color: isHome!
                              ? Color(0xff5AB145)
                              : currentIndex == index
                                  ? Colors.grey
                                  : Colors.grey,
                          size: 20.0),
                    ),
                    SizedBox(
                      height: 11,
                    ),

                    // Added TextAlign Property
                    Texts(
                      title!,
                      textAlign: TextAlign.center,
                      color: currentIndex == index ? Colors.grey : Colors.grey,
                      fontWeight: currentIndex == index ? FontWeight.normal : FontWeight.w400,
                      fontSize: currentIndex == index ? 11 : 9,
                    ),
                  ],
                ),
              ),
            ),
            if (currentIndex == 3 && Provider.of<OrderPreviewViewModel>(context, listen: false).cartResponse.quantityCount!=null && Provider.of<OrderPreviewViewModel>(context, listen: false).cartResponse.quantityCount!=0)
              Positioned(
                top: 11.5,
                right: -3.5,
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.red,
                    borderRadius: BorderRadius.circular(15),
                  ),
                  padding: EdgeInsets.only(left: 6.5, right: 6.5),
                  height: 18,
                  child: Center(
                      child: Texts(
                    orderPreviewViewModel.cartResponse.quantityCount.toString(),
                    style: "caption",
                    medium: true,
                    color: Colors.white,
                  )),
                ),
              )
          ],
        ),
      ),
    );
  }

  void setUserValues(value) async {
    if (value != null) sharedPref.setObject(IMEI_USER_DATA, value);
  }

  login(BuildContext context) async {
    final authService = new AuthProvider();
    var data = await sharedPref.getObject(IMEI_USER_DATA);
    sharedPref.remove(REGISTER_DATA_FOR_LOGIIN);
    if (data != null) {
      Navigator.of(context).pushNamed(CONFIRM_LOGIN);
    } else {
      GifLoaderDialogUtils.showMyDialog(context);
      authService.selectDeviceImei(DEVICE_TOKEN).then((SelectDeviceIMEIRES? value) {
        GifLoaderDialogUtils.hideDialog(context);
        if (value != null) {
          setUserValues(value);
          Navigator.of(context).pushNamed(CONFIRM_LOGIN);
        } else {
          Navigator.of(context).pushNamed(
            WELCOME_LOGIN,
          );
        }
      }).catchError((err) {
        GifLoaderDialogUtils.hideDialog(context);
        Navigator.of(context).pushNamed(
          WELCOME_LOGIN,
        );
      });
    }
  }
}
