import 'package:diplomaticquarterapp/config/config.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/models/Authentication/select_device_imei_res.dart';
import 'package:diplomaticquarterapp/routes.dart';
import 'package:diplomaticquarterapp/services/authentication/auth_provider.dart';
import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/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 Function onTap;
  final bool isHome;

  final IconData activeIcon;

  BottomNavPharmacyItem({this.icon, 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 != 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,
        );
      });
    }
  }
}
