import 'package:didvan/config/theme_data.dart';
import 'package:didvan/models/overview_data.dart';
import 'package:didvan/models/requests/news.dart';
import 'package:didvan/models/requests/radar.dart';
import 'package:didvan/models/requests/studio.dart';
import 'package:didvan/providers/user.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/views/widgets/didvan/card.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/didvan/text_field.dart';
import 'package:didvan/views/widgets/shimmer_placeholder.dart';
import 'package:didvan/views/widgets/skeleton_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:persian_number_utility/persian_number_utility.dart';

class MultitypeOverview extends StatelessWidget {
  final OverviewData item;
  final bool hasUnmarkConfirmation;
  final void Function(int id, bool value) onMarkChanged;
  final bool enableCaption;
  final bool enableBookmark;
  final bool showDivider;

  const MultitypeOverview({
    Key? key,
    required this.item,
    required this.onMarkChanged,
    this.hasUnmarkConfirmation = false,
    this.enableCaption = false,
    this.enableBookmark = false,
    this.showDivider = true,
  }) : super(key: key);

  get _targetPageArgs {
    if (item.type == 'radar') {
      return const RadarRequestArgs(page: 0);
    }
    if (item.type == 'news') {
      return const NewsRequestArgs(page: 0);
    }
    return StudioRequestArgs(page: 0, type: item.type);
  }

  String? get _targetPageRouteName {
    if (item.type == 'radar') {
      return Routes.radarDetails;
    }
    if (item.type == 'news') {
      return Routes.newsDetails;
    }
    if (item.type == 'podcast') {
      return Routes.podcasts;
    }
    if (item.type == 'video') {
      return Routes.videocasts;
    }
    return null;
  }

  String get _icon {
    switch (item.type) {
      case 'radar':
        return 'lib/assets/icons/Pouyesh_Ofogh_New.svg';
      case 'news':
        return 'lib/assets/icons/Donye_Foolad.svg';
      case 'video':
        return 'lib/assets/icons/video-play.svg';
      case 'podcast':
        return 'lib/assets/icons/microphone-2.svg';
      case 'delphi':
      case 'survey':
        return 'lib/assets/icons/Saha.svg';
      case 'infography':
        return 'lib/assets/icons/hugeicons_chart-02.svg';
      case 'monthly':
        return 'lib/assets/icons/Monthly.svg';
      default:
        return 'DidvanIcons.radar_light';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Stack(
              children: [
                SkeletonImage(imageUrl: item.image, height: 100, width: 140),
                Container(
                  padding:
                      const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
                  decoration: BoxDecoration(
                    color: Theme.of(context).colorScheme.primary,
                    borderRadius: const BorderRadius.horizontal(
                      left: Radius.circular(10),
                      // right: Radius.circular(16),
                    ),
                  ),
                  child: SvgPicture.asset(
                    _icon,
                    color: Theme.of(context).colorScheme.white,
                    height: 18,
                  ),
                ),
              ],
            ),
            const SizedBox(width: 8),
            Expanded(
              child: SizedBox(
                height: 80,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    DidvanText(
                      item.title,
                      style: Theme.of(context).textTheme.bodyLarge,
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    Flexible(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SvgPicture.asset('lib/assets/icons/calendar.svg'),
                              const SizedBox(width: 4),
                              DidvanText(
                                DateTime.parse(item.createdAt)
                                    .toPersianDateStr(),
                                style: Theme.of(context).textTheme.labelSmall,
                              ),
                            ],
                          ),
                          const Spacer(),
                          // if ((item.timeToRead ?? item.duration) != null)
                          // Row(
                          //   crossAxisAlignment: CrossAxisAlignment.start,
                          //   children: [
                          //     const Icon(
                          //       DidvanIcons.timer_light,
                          //       size: 16,
                          //     ),
                          //     const SizedBox(width: 4),
                          //     DidvanText(
                          //       item.timeToRead != null
                          //           ? '${item.timeToRead} دقیقه'
                          //           : DateTimeUtils.normalizeTimeDuration(
                          //               Duration(seconds: item.duration!),
                          //             ),
                          //       style:
                          //           Theme.of(context).textTheme.labelSmall,
                          //     ),
                          //   ],
                          // ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            if (enableCaption)
              Expanded(
                child: Column(
                  children: [
                    const SizedBox(height: 8),
                    Row(
                      children: [
                        SvgPicture.asset('lib/assets/icons/note.svg'),
                        const SizedBox(width: 4),
                        DidvanText(
                          'یادداشت‌های من',
                          style: const TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                          color: Theme.of(context).colorScheme.caption,
                        ),
                      ],
                    ),
                    const SizedBox(
                      height: 3,
                    ),
                    Row(
                      children: [
                        Flexible(
                          child: Container(
                            height: 1,
                            color: Theme.of(context).colorScheme.primary,
                          ),
                        ),
                        Flexible(
                          child: Container(
                            height: 1,
                            color: Theme.of(context).colorScheme.border,
                          ),
                        )
                      ],
                    ),
                    DidvanTextField(
                      disableBorders: true,
                      initialValue: item.description,
                      hintText: 'برای اضافه کردن یادداشت لمس کنید.',
                      onChanged: (value) => UserProvider.changeItemMark(
                        item.type,
                        item.id,
                        null,
                        description: value,
                      ),
                      isSmall: true,
                    ),
                  ],
                ),
              ),
            if (!enableCaption) const Spacer(),
            // if (enableBookmark) ...[
            //   const SizedBox(
            //     width: 12,
            //   ),
            //   BookmarkButton(
            //     value: item.marked,
            //     onMarkChanged: (value) => onMarkChanged(item.id, value),
            //     gestureSize: 32,
            //     type: item.type,
            //     itemId: item.id,
            //     askForConfirmation: true,
            //     svgIconOn: 'lib/assets/icons/bookmark_fill.svg',
            //     svgIconOff: 'lib/assets/icons/archive-tick.svg',
            //     color: Theme.of(context).colorScheme.primary,
            //   ),
            // ],
          ],
        ),
        const SizedBox(
          height: 10,
        ),
        Visibility(
          visible: showDivider,
          child: Divider(color: Theme.of(context).colorScheme.border),
        ),
      ],
    );
  }

  static Widget get placeholder => const DidvanCard(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ShimmerPlaceholder(height: 80, width: 80),
            SizedBox(width: 8),
            SizedBox(
              height: 80,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  ShimmerPlaceholder(height: 18, width: 150),
                  SizedBox(height: 8),
                  ShimmerPlaceholder(height: 18, width: 100),
                  Spacer(),
                  ShimmerPlaceholder(height: 14, width: 80),
                ],
              ),
            ),
          ],
        ),
      );
}
