import 'package:ambito/src/entity/measure/category/measure_category.dart';
import 'package:ambito/src/entity/measure/measure_repository.dart';
import 'package:ambito/src/extensions/extensions.dart';
import 'package:ambito/src/packages/ambito_notifier/notifier/filter_notifier.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 '../../../packages/ambito_theme/ambito_theme.dart';

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

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

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

class MeasureCategoriesPageState extends State<MeasureCategoriesPage> {
  Set<Widget> cards = {};
  List<MeasureCategory>? categories;

  @override
  void initState() {
    ambitoFilterNotifier.removeAllFilters();
    cards = {};
    categories = MeasureRepository().getCategories();
    super.initState();
  }

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

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

    for (MeasureCategory category in categories!) {
      setState(() {
        cards.add(CardMeasure(category: category));
      });
    }

    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.verticalSpacerMax,
                  Text(
                    context.translate('page.general.links.massnahmen.title'),
                    textAlign: TextAlign.start,
                    style: theme.headlineMedium.copyWith(
                      color: theme.currentColorScheme.onSurface,
                    ),
                  ),
                  theme.verticalSpacerMax,
                  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ßnahmentypen anzeigen',
                          style: theme.titleMedium.copyWith(
                            color: theme.currentColorScheme.outline,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }),
    );
  }
}
