// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:hoshan/core/gen/assets.gen.dart';
import 'package:hoshan/core/routes/route_generator.dart';
import 'package:hoshan/data/model/tools_categories_model.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/cubit/theme_mode_cubit.dart';
import 'package:hoshan/ui/theme/responsive.dart';
import 'package:hoshan/ui/theme/text.dart';
import 'package:hoshan/ui/widgets/components/image/network_image.dart';

class ToolCard extends StatelessWidget {
  final Categories cat;
  const ToolCard({super.key, required this.cat});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => context.go(Routes.singleTool, extra: cat),
      child: Container(
        margin: const EdgeInsets.all(8),
        constraints: BoxConstraints(
            maxWidth: MediaQuery.sizeOf(context).width *
                (Responsive(context).isMobile() ? 0.7 : 0.15)),
        decoration: BoxDecoration(
            boxShadow: const [
              BoxShadow(
                color: Color(0x664D4D4D),
                blurRadius: 6,
                offset: Offset(0, 1),
                spreadRadius: 0,
              )
            ],
            color: Theme.of(context).colorScheme.surface,
            borderRadius: BorderRadius.circular(16)),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: const EdgeInsets.all(12),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    padding: const EdgeInsets.all(8),
                    width: 64,
                    height: 64,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(16),
                        color: context.read<ThemeModeCubit>().isDark()
                            ? AppColors.black[900]
                            : AppColors.primaryColor[50]
                        // boxShadow: const [
                        //   BoxShadow(
                        //     color: Color(0x664D4D4D),
                        //     blurRadius: 30,
                        //     offset: Offset(0, 1),
                        //     spreadRadius: 0,
                        //   )
                        // ],
                        ),
                    child: ImageNetwork(
                        radius: 16,
                        // color: Theme.of(context).colorScheme.onSurface,
                        url: cat.image),
                  ),
                  const SizedBox(
                    width: 8,
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        cat.name ?? '',
                        style: AppTextStyles.headline6.copyWith(
                            color: Theme.of(context).colorScheme.onSurface),
                      ),
                      const SizedBox(
                        height: 4,
                      ),
                      Row(
                        children: [
                          Assets.icon.outline.coin.svg(
                              width: 20,
                              height: 20,
                              color: Theme.of(context).colorScheme.primary),
                          Builder(builder: (context) {
                            if (cat.bots != null && cat.bots!.length == 1) {
                              return Text(
                                '${cat.bots!.first.cost} سکه',
                                style: AppTextStyles.body5.copyWith(
                                    color: AppColors.gray[
                                        context.read<ThemeModeCubit>().isDark()
                                            ? 600
                                            : 900]),
                              );
                            }
                            final biggestVal = cat.bots?.fold(
                              0,
                              (previousValue, element) {
                                try {
                                  if (element.cost! >= previousValue) {
                                    return element.cost!;
                                  }
                                } catch (e) {
                                  if (kDebugMode) {
                                    print('Error is: $e');
                                  }
                                }

                                return previousValue;
                              },
                            );
                            final smallestVal = cat.bots?.fold(
                              9999,
                              (previousValue, element) {
                                try {
                                  if (element.cost! <= previousValue) {
                                    return element.cost!;
                                  }
                                } catch (e) {
                                  if (kDebugMode) {
                                    print('Error is: $e');
                                  }
                                }

                                return previousValue;
                              },
                            );
                            return Text(
                              smallestVal == 0 && biggestVal == 0
                                  ? 'رایگان'
                                  : smallestVal == biggestVal
                                      ? '$biggestVal سکه'
                                      : '$smallestVal ${biggestVal != null && biggestVal != 0 ? 'تا $biggestVal' : ''} سکه',
                              style: AppTextStyles.body5.copyWith(
                                  color: AppColors.gray[
                                      context.read<ThemeModeCubit>().isDark()
                                          ? 600
                                          : 900]),
                            );
                          }),
                        ],
                      ),
                    ],
                  )
                ],
              ),
            ),
            Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 6, vertical: 8),
                      decoration: BoxDecoration(
                          color: Theme.of(context)
                              .colorScheme
                              .secondary
                              .withAlpha(50),
                          borderRadius: BorderRadius.circular(8)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Container(
                            width: 4,
                            height: 4,
                            decoration: BoxDecoration(
                                shape: BoxShape.circle,
                                color: Theme.of(context).colorScheme.secondary),
                          ),
                          const SizedBox(
                            width: 4,
                          ),
                          Text(
                            '${cat.bots?.length ?? ''} مدل',
                            style: AppTextStyles.body6.copyWith(
                                color: Theme.of(context).colorScheme.onSurface,
                                fontWeight: FontWeight.bold),
                          )
                        ],
                      ),
                    ),
                    const SizedBox(
                      width: 8,
                    ),
                    cat.bots != null && cat.bots!.length > 1
                        ? Row(
                            children: [
                              ...List.generate(
                                2,
                                (index) {
                                  final yourText = cat.bots![index].name ?? '';
                                  return Row(
                                    children: [
                                      Text(
                                        yourText,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        style: AppTextStyles.body6.copyWith(
                                            fontSize: 10,
                                            color: AppColors.gray[context
                                                    .read<ThemeModeCubit>()
                                                    .isDark()
                                                ? 600
                                                : 900]),
                                      ),
                                      const SizedBox(
                                        width: 2,
                                      ),
                                      if (cat.bots!.length != 2 || index != 1)
                                        Text(
                                          ' | ',
                                          style: AppTextStyles.body6.copyWith(
                                              fontSize: 10,
                                              color: AppColors.gray[context
                                                      .read<ThemeModeCubit>()
                                                      .isDark()
                                                  ? 600
                                                  : 900]),
                                        ),
                                      const SizedBox(
                                        width: 2,
                                      ),
                                    ],
                                  );
                                },
                              ),
                              if (cat.bots!.length > 2)
                                Text(
                                  'و...',
                                  style: AppTextStyles.body6.copyWith(
                                      fontSize: 10,
                                      color: AppColors.gray[context
                                              .read<ThemeModeCubit>()
                                              .isDark()
                                          ? 600
                                          : 900]),
                                ),
                            ],
                          )
                        : cat.bots != null && cat.bots!.isNotEmpty
                            ? Text(
                                cat.bots!.first.name ?? '',
                                style: AppTextStyles.body6.copyWith(
                                    color: AppColors.gray[
                                        context.read<ThemeModeCubit>().isDark()
                                            ? 600
                                            : 900]),
                              )
                            : const SizedBox.shrink(),
                  ],
                )),
            const SizedBox(
              height: 12,
            )
          ],
        ),
      ),
    );
  }
}
