import 'package:ambito/src/entity/measure/group/measure_group.dart';
import 'package:ambito/src/pages/ambito_page.dart';
import 'package:ambito/src/widgets/cards/card_measure.dart';
import 'package:ambito/src/widgets/page/base_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:screen_breakpoints/screen_breakpoints.dart';

import '../../../consts/consts.dart';
import '../../../entity/measure/measure_repository.dart';
import '../../../packages/ambito_theme/ambito_theme.dart';

class MeasureGroupsPage extends AmbitoPage {
  const MeasureGroupsPage({super.key});

  @override
  final String path = 'massnahmen';
  @override
  final String title = 'Maßnamen';

  @override
  State<StatefulWidget> createState() => MeasureGroupsPageState();
}

class MeasureGroupsPageState extends State<MeasureGroupsPage> {
  Set<Widget> cards = {};
  String currentCategory = '';
  List<MeasureGroup>? groups;
  Map<String, int> counter = {};

  @override
  void initState() {
    cards = {};
    currentCategory = Get.parameters['kategorie'] ?? '';
    groups = MeasureRepository().getGroupsByCategory(currentCategory);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final AmbitoTheme theme = getTheme(context);

    setState(() {
      cards = {};
    });

    for (MeasureGroup group in groups!) {
      setState(() {
        cards.add(CardMeasure(
          group: group,
        ));
      });
    }

    return BasePage().getPage(
      context,
      BreakpointBuilder(builder: (
        context,
        breakpoint,
        configuration,
      ) {
        return SingleChildScrollView(
          child: Center(
            child: SizedBox(
              width: Breakpoint.fromContext(context).width,
              child: Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(breakpoint.toString()),
                  theme.verticalSpacer,
                  Text(
                    'Maßnahmengruppen',
                    textAlign: TextAlign.start,
                    style: theme.headlineLarge.copyWith(
                      color: theme.currentColorScheme.onSurface,
                    ),
                  ),
                  theme.verticalSpacerMax,
                  Align(
                    alignment: Alignment.center,
                    child: Wrap(
                      alignment: WrapAlignment.start,
                      spacing: 40,
                      runSpacing: 40,
                      children: cards.toList(),
                    ),
                  ),
                  theme.verticalSpacerMax,
                  InkWell(
                    onHover: (hovered) {},
                    highlightColor: Colors.transparent,
                    splashColor: Colors.transparent,
                    focusColor: Colors.transparent,
                    hoverColor: Colors.transparent,
                    onTap: () async {
                      await prefs.setString('selected_areaType', '');
                      await Get.toNamed('/massnahmendatenbank');
                    },
                    child: Container(
                      decoration: BoxDecoration(
                        borderRadius:
                            const BorderRadius.all(Radius.circular(8)),
                        color:
                            theme.currentColorScheme.onSurface..withValues(alpha:.1),
                      ),
                      width: Breakpoint.fromContext(context).width,
                      child: Padding(
                        padding: const EdgeInsets.all(20),
                        child: Text(
                          'Alle Maßnahmen anzeigen',
                          style: theme.headlineMedium.copyWith(
                            color: theme.currentColorScheme.outline,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }),
    );
  }
}
