import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';

class AppBottomNavigationBar extends StatelessWidget {
  final Function(int index) onPressed;
  final int selectedIndex;

  const AppBottomNavigationBar({Key? key, required this.onPressed, required this.selectedIndex}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool isEngineer = (Provider.of<UserProvider>(context, listen: false).user?.type) == UsersTypes.engineer;
    bool isUserFMS = Provider.of<SettingProvider>(context, listen: false).isUserFMS;

    List<NavItemModel> navItems = [
      NavItemModel(0, "overview", context.translation.overview),
      NavItemModel(1, "request_icon", context.translation.workOrder),
      if ((isEngineer && isUserFMS) || !isEngineer) NavItemModel(2, "add_icon", context.translation.calendar, showLabel: false),
      NavItemModel(3, "assets", context.translation.assets),
      NavItemModel(4, "contact", context.translation.contact),
    ];

    return Container(
      decoration: BoxDecoration(
        color: AppColor.background(context),
        boxShadow: [boxShadowR14],
      ),
      child: BottomNavigationBar(
        // backgroundColor: Colors.white,
        backgroundColor: AppColor.background(context),
        items: <BottomNavigationBarItem>[
          for (int i = 0; i < navItems.length; i++) navBarItem(context, index: navItems[i].index, iconName: navItems[i].iconName, label: navItems[i].label, showLabel: navItems[i].showLabel),
        ],
        currentIndex: selectedIndex,
        onTap: (index) {
          onPressed(navItems[index].index);
        },
        selectedLabelStyle: AppTextStyles.tinyFont2,
        unselectedLabelStyle: AppTextStyles.tinyFont2,
        selectedItemColor: Theme.of(context).bottomNavigationBarTheme.selectedItemColor,
        unselectedItemColor: Theme.of(context).bottomNavigationBarTheme.unselectedItemColor,
      ),
    );
  }

  BottomNavigationBarItem navBarItem(BuildContext context, {required int index, required String iconName, required String label, bool showLabel = true}) {
    return BottomNavigationBarItem(
      icon: iconName
          .toSvgAsset(
            width: showLabel ? 26 : 38,
            height: showLabel ? 26 : 38,
            color: showLabel || (showLabel && selectedIndex != 2)
                ? selectedIndex == index
                    ? Theme.of(context).bottomNavigationBarTheme.selectedItemColor
                    : Theme.of(context).bottomNavigationBarTheme.unselectedItemColor
                : null,
          )
          .paddingOnly(top: showLabel ? 0 : 12, bottom: showLabel ? 6 : 0),
      label: showLabel ? label : '',
    );
  }
}

class NavItemModel {
  int index;
  String label;
  String iconName;
  bool showLabel;

  NavItemModel(this.index, this.iconName, this.label, {this.showLabel = true});
}
