import 'package:ambito/src/extensions/extensions.dart';
import 'package:ambito/src/packages/ambito_notifier/notifier/cart_notifier.dart';
import 'package:ambito/src/packages/ambito_theme/ambito_theme.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:screen_breakpoints/screen_breakpoints.dart';

import '../../config/config.dart';

class AmbitoAppbar extends AppBar {
  AmbitoAppbar(
      {super.key,
      List<String>? links,
      required Breakpoint breakpoint,
      required AmbitoTheme theme})
      : super(
          automaticallyImplyLeading: false,
          leading: Padding(
            padding: const EdgeInsets.all(8),
            child: Hero(
              tag: 'logo',
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  onHover: (hovered) {},
                  highlightColor: Colors.transparent,
                  splashColor: Colors.transparent,
                  focusColor: Colors.transparent,
                  hoverColor: Colors.transparent,
                  onTap: () {
                    Get.toNamed('/');
                  },
                  child: Image.asset(
                    'assets/images/logo_trans.png',
                  ),
                ),
              ),
            ),
          ),
          leadingWidth: 80,
          scrolledUnderElevation: 0,
          centerTitle: true,
          title: Builder(
            builder: (context) => _links(context, links),
          ),
          toolbarHeight: 80,
          backgroundColor: theme.currentColorScheme.primaryContainer,
          actions: [
            Text(breakpoint.breakpoint.toString()),
            IconButton(
              onPressed: () {
                Get.toNamed('/');
              },
              icon: Icon(
                Icons.notifications,
                color: theme.currentColorScheme.primary,
              ),
            ),
            if (breakpoint >= myBreakpoints.md)
              IconButton(
                onPressed: () {
                  Get.toNamed('/warenkorb');
                },
                icon: ambitoCartNotifier.carts == 0
                    ? Icon(
                        Icons.shopping_cart_outlined,
                        color: theme.currentColorScheme.primary,
                      )
                    : Badge.count(
                        count: ambitoCartNotifier.carts,
                        child: Icon(
                          Icons.shopping_cart_outlined,
                          color: theme.currentColorScheme.primary,
                        ),
                      ),
              ),
            if (breakpoint >= myBreakpoints.md)
              IconButton(
                onPressed: () {
                  Get.toNamed('/kalender/jahr');
                },
                icon: Icon(
                  Icons.calendar_view_month,
                  color: theme.currentColorScheme.primary,
                ),
              ),
            if (breakpoint >= myBreakpoints.md)
              IconButton(
                onPressed: () {
                  Get.toNamed('/kalender');
                },
                icon: Icon(
                  Icons.calendar_month,
                  color: theme.currentColorScheme.primary,
                ),
              ),
            if (breakpoint >= myBreakpoints.md)
              PopupMenuButton(
                onSelected: (item) {},
                icon: Icon(
                  Icons.person,
                  color: theme.currentColorScheme.primary,
                ),
                itemBuilder: (
                  BuildContext context,
                ) {
                  return [
                    PopupMenuItem(
                      onTap: () async {
                        Get.toNamed('/massnahme/neu');
                      },
                      child: Text(
                        'Neue Maßnahme',
                        style: getTheme(context).bodyMedium,
                      ),
                    ),
                  ];
                },
              ),
            const SizedBox(
              width: 30,
            )
          ],
        );

  static Widget _links(BuildContext context, List<String>? links) {
    List<Widget> linkButtons = [];
    if (context.breakpoint.breakpoint >= myBreakpoints.md!.breakpoint &&
        links != null &&
        links.isNotEmpty) {
      for (String link in links) {
        linkButtons.add(_linkButton(context, link));
      }
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: linkButtons,
    );
  }

  static Widget _linkButton(BuildContext context, String link) {
    double fontSize = 20;
    return TextButton(
      style: ButtonStyle(
        overlayColor:
            WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {
          return Colors.transparent;
        }),
      ),
      onPressed: () {
        Get.offAndToNamed('/$link');
      },
      child: Text(
        context.translate('page.start.links.$link.title'),
        style: TextStyle(
          fontSize: fontSize,
          fontWeight: FontWeight.bold,
          //color: (activeLink == link) ? Colors.grey.shade800 : Colors.black,
        ),
      ),
    );
  }
}
