import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:remever/common/functions.dart';
import 'package:remever/common/resources.dart';
import 'package:remever/common/widgets/typography.dart';
import 'package:remever/common/widgets/wspace.dart';
import 'package:remever/components/extensions/context.dart';
import 'package:remever/database/database.dart';
import 'package:remever/gen/assets.gen.dart';
import 'package:remever/inject.dart';
import 'package:remever/router.gr.dart';
import 'package:remever/screens/dialogs/filters_dialog.dart';
import 'package:remever/services/collection/collections_interface.dart';
import 'package:remever/widgets/debug/app_debug.dart';

class CollectionsAppBar extends StatelessWidget implements PreferredSizeWidget {
  const CollectionsAppBar({super.key});

  @override
  Size get preferredSize => Size.fromHeight(66.h);

  @override
  Widget build(BuildContext context) {
    return AppBar(
      toolbarHeight: 66.h,
      backgroundColor: AppColors.white,
      shadowColor: Colors.transparent,
      // leading: SizedBox(),
      leadingWidth: 0,
      title: Row(
        children: <Widget>[
          GestureDetector(
            onLongPress: () => context.pushRoute(const SandboxRoute()),
            child: AppTypography(
              'Коллекции',
              type: SemiBold28px(),
              color: AppColors.body_text,
            ),
          ),
          const WSpace(2),
          Container(
            height: 22.h,
            width: 38.w,
            decoration: BoxDecoration(
              color: AppColors.secondary,
              borderRadius: BorderRadius.circular(40).r,
            ),
            child: Center(
              child: StreamBuilder<List<Collection>>(
                stream: getIt<CollectionsInterface>().watchCollectionsList(),
                builder: (context, snapshot) {
                  return _buildCount(snapshot.data?.length ?? 0);
                },
              ),
            ),
          ),
        ],
      ),
      actions: <Widget>[
        AppDebug(
          builder: (context, isDebug) {
            if (!isDebug) return SizedBox();
            return Row(
              children: [
                AppBarIconButton(icon: Assets.icons.typeSearch, onTap: () {}),
                AppBarIconButton(icon: Assets.icons.typeDownload, onTap: () {}),
                AppBarIconButton(
                  icon: Assets.icons.typeSort,
                  onTap: () {
                    showCuperModalBottomSheet(
                      context: context,
                      height: 424.h,
                      builder: (BuildContext context) => const FiltersDialog(),
                    );
                  },
                ),
              ],
            );
          },
        ),
      ],
    );
  }

  Widget _buildCount(int count) {
    return AppTypography(
      '$count',
      type: Regular12px(),
      color: AppColors.body_text,
    );
  }
}

class AppBarIconButton extends StatelessWidget {
  const AppBarIconButton({required this.icon, required this.onTap, super.key});

  final AssetGenImage icon;
  final void Function()? onTap;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: SizedBox(
        width: 48.h,
        child: Center(child: icon.image(height: 24.h, width: 24.w)),
      ),
    );
  }
}
