// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hoshan/core/gen/assets.gen.dart';
import 'package:hoshan/data/model/ai/bots_model.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/cubit/theme_mode_cubit.dart';
import 'package:hoshan/ui/theme/text.dart';
import 'package:hoshan/ui/widgets/components/bot/cubit/bookmark_bot_cubit.dart';
import 'package:hoshan/ui/widgets/components/image/network_image.dart';
import 'package:hoshan/ui/widgets/sections/loading/default_placeholder.dart';

class BotGridCard extends StatelessWidget {
  final Bots bot;
  final Function(bool)? onMark;
  final bool iconic;
  final bool showCat;
  const BotGridCard(
      {super.key,
      required this.bot,
      this.onMark,
      this.iconic = false,
      this.showCat = false});

  @override
  Widget build(BuildContext context) {
    return Container(
      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: Stack(
        children: [
          Positioned(
              top: 16,
              left: 16,
              child: onMark != null
                  ? BlocProvider<BookmarkBotCubit>(
                      create: (context) => BookmarkBotCubit()
                        ..getBotBookMarkStstus(bot.marked ?? false),
                      child: BlocConsumer<BookmarkBotCubit, BookmarkBotState>(
                        listener: (context, state) {
                          if (state is! BookmarkBotLoading &&
                              state is! BookmarkBotInitial) {
                            onMark?.call(state is BookmarkedBot);
                          }
                        },
                        builder: (context, state) {
                          return DefaultPlaceHolder(
                            enabled: state is! BookmarkedBot &&
                                state is! UnBookMarkedBot,
                            child: GestureDetector(
                                onTap: () => context
                                    .read<BookmarkBotCubit>()
                                    .setBotBookMarkStstus(id: bot.id!),
                                child: (state is BookmarkedBot
                                        ? Assets.icon.bold.archiveTick
                                        : Assets.icon.outline.archiveTick)
                                    .svg(
                                        color: Theme.of(context)
                                            .colorScheme
                                            .primary)),
                          );
                        },
                      ),
                    )
                  : const SizedBox.shrink()),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: [
                const SizedBox(
                  height: 8,
                ),
                Center(
                  child: ImageNetwork(
                    width: 58,
                    height: 58,
                    radius: 360,
                    url: bot.image,
                  ),
                ),
                Expanded(
                    child: Padding(
                  padding: const EdgeInsets.symmetric(vertical: 8),
                  child: Column(
                    children: [
                      Text(
                        bot.name ?? '',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: AppTextStyles.body5.copyWith(
                            fontWeight: FontWeight.bold,
                            color: Theme.of(context).colorScheme.onSurface),
                      ),
                      const SizedBox(
                        height: 8,
                      ),
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 8.0),
                        child: Row(
                          mainAxisAlignment: showCat
                              ? MainAxisAlignment.spaceBetween
                              : MainAxisAlignment.center,
                          children: [
                            if (showCat)
                              Expanded(
                                child: Row(
                                  children: [
                                    Container(
                                      width: 8,
                                      height: 8,
                                      decoration: BoxDecoration(
                                        shape: BoxShape.circle,
                                        color: Theme.of(context)
                                            .colorScheme
                                            .secondary,
                                      ),
                                    ),
                                    const SizedBox(
                                      width: 8,
                                    ),
                                    Expanded(
                                      child: Text(bot.category?.name ?? '',
                                          maxLines: 1,
                                          overflow: TextOverflow.ellipsis,
                                          style: AppTextStyles.body6.copyWith(
                                            color: Theme.of(context)
                                                .colorScheme
                                                .onSurface,
                                          )),
                                    )
                                  ],
                                ),
                              ),
                            Expanded(
                              child: Row(
                                mainAxisSize: MainAxisSize.min,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Assets.icon.outline.coin.svg(
                                      width: 16,
                                      height: 16,
                                      color: Theme.of(context)
                                          .colorScheme
                                          .secondary),
                                  const SizedBox(
                                    width: 4,
                                  ),
                                  Flexible(
                                    child: Text(
                                      bot.cost == 0
                                          ? 'رایگان'
                                          : showCat
                                              ? bot.cost.toString()
                                              : 'هر پیام ${bot.cost} سکه',
                                      style: AppTextStyles.body5.copyWith(
                                          color: Theme.of(context)
                                              .colorScheme
                                              .secondary),
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  )
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                )),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
