import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/overview_data.dart';
import 'package:didvan/models/requests/studio.dart';
import 'package:didvan/providers/media.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/views/widgets/bookmark_button.dart';
import 'package:didvan/views/widgets/duration_widget.dart';
import 'package:didvan/views/widgets/didvan/card.dart';
import 'package:didvan/views/widgets/didvan/divider.dart';
import 'package:didvan/views/widgets/didvan/icon_button.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/shimmer_placeholder.dart';
import 'package:didvan/views/widgets/skeleton_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:persian_number_utility/persian_number_utility.dart';

class PodcastOverview extends StatelessWidget {
  final OverviewData podcast;
  final void Function(int id, bool value, bool shouldUpdate) onMarkChanged;
  final StudioRequestArgs studioRequestArgs;
  final bool hasUnmarkConfirmation;
  const PodcastOverview({
    Key? key,
    required this.podcast,
    required this.onMarkChanged,
    required this.studioRequestArgs,
    this.hasUnmarkConfirmation = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DidvanCard(
      onTap: () {
        Navigator.of(context).pushNamed(
          Routes.studioDetails,
          arguments: {
            'id': podcast.id,
            'args': studioRequestArgs,
            'onMarkChanged': onMarkChanged,
          },
        );
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              SkeletonImage(
                imageUrl: podcast.image,
                width: 64,
                height: 64,
              ),
              const SizedBox(width: 8),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    DidvanText(
                      podcast.title,
                      style: Theme.of(context).textTheme.bodyLarge,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 4),
                    DidvanText(
                      DateTime.parse(podcast.createdAt).toPersianDateStr(),
                      style: Theme.of(context).textTheme.labelSmall,
                      color: Theme.of(context).colorScheme.caption,
                    ),
                  ],
                ),
              ),
            ],
          ),
          const SizedBox(height: 8),
          DidvanText(
            podcast.description,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
          ),
          const DidvanDivider(verticalPadding: 8),
          Consumer<MediaProvider>(
            builder: (context, state, child) => Row(
              children: [
                DurationWidget(duration: podcast.duration!),
                const Spacer(),
                if (!kIsWeb) ...[
                  if (state.appState == AppState.idle ||
                      !state.downloadQueue.contains(podcast.link))
                    DidvanIconButton(
                      gestureSize: 28,
                      color: _isDownloaded
                          ? Theme.of(context).colorScheme.primary
                          : null,
                      icon: _isDownloaded
                          ? DidvanIcons.download_solid
                          : DidvanIcons.download_regular,
                      onPressed: _isDownloaded
                          ? () {}
                          : () => state.download(
                                fileName: 'podcast-${podcast.id}.mp3',
                                isVideo: false,
                                url: podcast.link!,
                              ),
                    ),
                  if (state.appState == AppState.busy &&
                      state.downloadQueue.contains(podcast.link))
                    const SizedBox(
                      width: 18,
                      height: 18,
                      child: CircularProgressIndicator(
                        strokeWidth: 2,
                      ),
                    ),
                  const SizedBox(width: 8.0),
                ],
                DidvanIconButton(
                  gestureSize: 32,
                  onPressed: () => Navigator.of(context).pushNamed(
                    Routes.mentions,
                    arguments: {
                      'id': podcast.id,
                      'type': 'studio',
                      'title': podcast.title,
                    },
                  ),
                  icon: DidvanIcons.mention_icon,
                ),
                const SizedBox(width: 8.0),
                BookmarkButton(
                  itemId: podcast.id,
                  type: 'podcast',
                  askForConfirmation: hasUnmarkConfirmation,
                  gestureSize: 32,
                  value: podcast.marked,
                  onMarkChanged: (value) =>
                      onMarkChanged(podcast.id, value, false),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  bool get _isDownloaded {
    return MediaProvider.downloadedItemIds.contains(podcast.id);
  }

  static Widget get placeholder => DidvanCard(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ShimmerPlaceholder(height: 64, width: 64),
            SizedBox(width: 8),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ShimmerPlaceholder(height: 18, width: 200),
                SizedBox(height: 8),
                ShimmerPlaceholder(height: 18, width: 100),
              ],
            ),
          ],
        ),
        const SizedBox(height: 16),
        const ShimmerPlaceholder(
          height: 16,
          width: double.infinity,
        ),
        const SizedBox(height: 8),
        const ShimmerPlaceholder(
          height: 16,
          width: 200,
        ),
        const SizedBox(height: 4),
        const DidvanDivider(verticalPadding: 8),
        Row(
          children: [
            ShimmerPlaceholder(
              height: 36,
              width: 92,
              borderRadius: BorderRadius.circular(5),
            ),
            const Spacer(),
            const ShimmerPlaceholder(width: 24, height: 24),
            const SizedBox(width: 16),
            const ShimmerPlaceholder(width: 24, height: 24),
          ],
        ),
      ],
    ),
  );
}
