// Flutter imports:
import 'package:flutter/material.dart';

// Package imports:
import 'package:auto_route/auto_route.dart';
import 'package:remever/common/functions.dart';
import 'package:remever/common/resources.dart';
import 'package:remever/common/widgets/wspace.dart';
import 'package:remever/components/extensions/context.dart';
import 'package:remever/gen/assets.gen.dart';
import 'package:remever/router.gr.dart';

@RoutePage()
class HomeScreen extends StatelessWidget {
  ///
  /// Основной экран на котором расположен бар навигации
  ///
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    logger.logBuild('build home screen');

    return SafeArea(
      top: false,
      child: AutoTabsScaffold(
        routes: <PageRouteInfo>[
          SettingsRoute(),
          StatistickRoute(),
          CreateRoute(),
          CollectionRoute(),
        ],
        bottomNavigationBuilder: (_, TabsRouter tabsRouter) {
          return ColoredBox(
            color: AppColors.bg,
            child: SizedBox(
              height: 73.h,
              child: Stack(
                alignment: Alignment.bottomCenter,
                children: <Widget>[
                  _buildBackgroundBar(tabsRouter),
                  _buildCentralButton(),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  /// Построение заднего фона и кнопок нижнего бара
  Widget _buildBackgroundBar(TabsRouter tabsRouter) {
    return Container(
      height: 64.h,
      color: AppColors.white,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 32).r,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            _BottomBarIcon(
              icon: Assets.icons.typeSetting,
              isActive: tabsRouter.activeIndex == 0,
              onTap: () => tabsRouter.setActiveIndex(0),
            ),
            _BottomBarIcon(
              icon: Assets.icons.typeStat,
              isActive: tabsRouter.activeIndex == 1,
              onTap: () => tabsRouter.setActiveIndex(1),
            ),
            const WSpace(60),
            _BottomBarIcon(
              icon: Assets.icons.typeCreateCard,
              isActive: tabsRouter.activeIndex == 2,
              onTap: () => tabsRouter.setActiveIndex(2),
            ),
            _BottomBarIcon(
              icon: Assets.icons.typeCollection,
              isActive: tabsRouter.activeIndex == 3,
              onTap: () => tabsRouter.setActiveIndex(3),
            ),
          ],
        ),
      ),
    );
  }

  /// Построение центральной кнопки
  Widget _buildCentralButton() {
    return Align(
      alignment: Alignment.topCenter,
      child: GestureDetector(
        onTap: () {
          // Логика нажатия на центральную кнопку
        },
        child: SizedBox.square(
          dimension: 60.r,
          child: DecoratedBox(
            decoration: const BoxDecoration(
              shape: BoxShape.circle,
              color: AppColors.primary,
            ),
            child: Center(
              child: Assets.icons.typeLearn.image(
                color: AppColors.white,
                height: 24.h,
                width: 24.w,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class _BottomBarIcon extends StatelessWidget {
  final AssetGenImage icon;
  final bool isActive;
  final VoidCallback onTap;

  const _BottomBarIcon({
    required this.icon,
    required this.isActive,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: icon.image(
        height: 24.h,
        width: 24.w,
        color: isActive ? AppColors.primary : Colors.black,
      ),
    );
  }
}
