import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.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;
    return Container(
      // height: 84.toScreenHeight,
      decoration: BoxDecoration(
        color: AppColor.background(context),
        boxShadow: [boxShadowR14],
      ),
      child: BottomNavigationBar(
        backgroundColor: Colors.white,
        items: <BottomNavigationBarItem>[
          navBarItem(context, index: 0, iconName: "overview", label: context.translation.overview),
          navBarItem(context, index: 1, iconName: "request_icon", label: context.translation.workOrder),
          if (!isEngineer) navBarItem(context, index: 2, iconName: "add_icon", label: context.translation.calendar, showLabel: false),
          navBarItem(context, index: !isEngineer ? 3 : 2, iconName: "assets", label: context.translation.assets),
          navBarItem(context, index: !isEngineer ? 4 : 3, iconName: "contact", label: context.translation.contact),
        ],
        currentIndex: selectedIndex,
        onTap: onPressed,
        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 : '',
    );
  }
}
