import 'package:ambito/src/entity/lists/list_repository.dart';
import 'package:ambito/src/entity/measure/category/measure_category.dart';
import 'package:ambito/src/extensions/extensions.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:screen_breakpoints/screen_breakpoints.dart';

import '../../config/config.dart';
import '../../consts/consts.dart';
import '../../entity/measure/group/measure_group.dart';
import '../../packages/ambito_theme/ambito_theme.dart';

class CardMeasure extends StatelessWidget {
  const CardMeasure({super.key, this.category, this.group});

  final MeasureCategory? category;
  final MeasureGroup? group;

  @override
  Widget build(BuildContext context) {
    if (category != null) {
      return _buildMeasureCard(
        context,
        title: category!.name!,
        image: category!.image,
        color: actionAreaColors[category!.name!]!,
        titleColor: actionAreaFGColors[category!.name!]!,
        description: category!.description! +
            ListRepository()
                .getMeasuresByCategory(category!.name!)
                .length
                .toString(),
        count: ListRepository().getGroupsCounterByCategory(category!.name!),
        route:
            '/massnahmendatenbank/${category!.name!.toLowerCase().replaceUmlauts()}',
      );
    }

    if (group != null) {
      final currentCategory = Get.parameters['kategorie'] ?? '';
      return _buildMeasureCard(
        context,
        title: group!.name!,
        image: group!.image,
        color: actionGroupColors[group!.name!]!..withValues(alpha: 0.1),
        titleColor: actionGroupColors[group!.name!]!,
        description: group!.description! +
            ListRepository()
                .getMeasuresByCategoryAndGroup(currentCategory, group!.name!)
                .length
                .toString(),
        count: ListRepository()
            .getTypesCounterByCategoryAndGroup(currentCategory, group!.name!),
        route:
            '/massnahmendatenbank/$currentCategory/${group!.name!.toLowerCase().replaceUmlauts()}',
      );
    }

    return const SizedBox();
  }

  Widget _buildMeasureCard(
    BuildContext context, {
    required String title,
    String? image,
    required Color color,
    required Color titleColor,
    required String description,
    required int count,
    required String route,
  }) {
    final bool isSmallScreen =
        context.breakpoint.breakpoint == myBreakpoints.sm!.breakpoint;
    final imageProvider = (image == 'images/logo_trans.png')
        ? AssetImage(image!) as ImageProvider
        : CachedNetworkImageProvider(image!);

    return InkWell(
      onTap: () => prefs
          .setString('selected_areaType', title)
          .then((_) => Get.toNamed(route)),
      highlightColor: Colors.transparent,
      splashColor: Colors.transparent,
      focusColor: Colors.transparent,
      hoverColor: Colors.transparent,
      child: isSmallScreen
          ? _buildSmallCard(
              context, color, titleColor, imageProvider, title, count)
          : _buildNormalCard(context, color, imageProvider, title, titleColor,
              description, count),
    );
  }

  Widget _buildSmallCard(
    BuildContext context,
    Color containerColor,
    Color titleColor,
    ImageProvider imageProvider,
    String title,
    int count,
  ) {
    final theme = getTheme(context);

    return Padding(
      padding: const EdgeInsets.only(bottom: 10),
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: containerColor,
        ),
        child: Column(
          children: [
            Container(
              height: 128,
              width: double.infinity,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                image: DecorationImage(image: imageProvider, fit: BoxFit.cover),
              ),
            ),
            theme.verticalSpacerSmall,
            Text(
              title,
              style: theme.titleMedium
                  .copyWith(color: titleColor, fontWeight: FontWeight.bold),
            ),
            Text(
              'Anzahl: $count',
              style: theme.labelMedium.copyWith(color: titleColor),
            ),
            theme.verticalSpacerSmall,
          ],
        ),
      ),
    );
  }

  Widget _buildNormalCard(
    BuildContext context,
    Color bgColor,
    ImageProvider imageProvider,
    String title,
    Color titleColor,
    String description,
    int count,
  ) {
    final theme = getTheme(context);

    return SizedBox(
      width: 357,
      height: 606,
      child: Card(
        elevation: 0,
        color: bgColor,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(8.0),
              child: Image(
                image: imageProvider,
                fit: BoxFit.cover,
              ),
            ),
            theme.verticalSpacer,
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Text(
                title,
                textAlign: TextAlign.start,
                style: theme.titleMedium
                    .copyWith(color: titleColor, fontWeight: FontWeight.bold),
              ),
            ),
            theme.verticalSpacer,
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Text(
                description,
                textAlign: TextAlign.justify,
                style: theme.bodyMedium
                    .copyWith(color: theme.currentColorScheme.onSurface),
              ),
            ),
            const Spacer(),
            Padding(
              padding: const EdgeInsets.only(left: 16, right: 16, bottom: 16),
              child: Text(
                'Anzahl: $count',
                style: theme.labelMedium.copyWith(color: titleColor),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
