import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.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/routes/routes.dart';
import 'package:didvan/utils/date_time.dart';
import 'package:didvan/views/home/studio/studio_details/studio_details_state.dart';
import 'package:didvan/views/widgets/didvan/card.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/material.dart';
import 'package:persian_number_utility/persian_number_utility.dart';
import 'package:provider/provider.dart';

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

  const MultitypeOverview({
    Key? key,
    required this.item,
    required this.onMarkChanged,
    this.hasUnmarkConfirmation = false,
  }) : 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;
    }
    return Routes.studioDetails;
  }

  IconData get _icon {
    if (item.type == 'radar') {
      return DidvanIcons.radar_light;
    }
    if (item.type == 'news') {
      return DidvanIcons.news_light;
    }
    if (item.type == 'video') {
      return DidvanIcons.video_light;
    }
    return DidvanIcons.podcast_light;
  }

  @override
  Widget build(BuildContext context) {
    return DidvanCard(
      onTap: () {
        if (item.type == 'podcast') {
          context.read<StudioDetailsState>().getStudioDetails(
                item.id,
                args: StudioRequestArgs(page: 0, type: item.type),
              );
          return;
        }
        Navigator.of(context).pushNamed(
          _targetPageRouteName,
          arguments: {
            'onMarkChanged': onMarkChanged,
            'id': item.id,
            'args': _targetPageArgs,
            'hasUnmarkConfirmation': hasUnmarkConfirmation,
          },
        );
      },
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Stack(
            children: [
              SkeletonImage(imageUrl: item.image, height: 80, width: 80),
              Container(
                padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
                decoration: BoxDecoration(
                  color: Theme.of(context).colorScheme.secondary,
                  borderRadius: const BorderRadius.horizontal(
                    left: Radius.circular(10),
                  ),
                ),
                child: Icon(
                  _icon,
                  color: Theme.of(context).colorScheme.white,
                  size: 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.bodyText1,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  Row(
                    children: [
                      const Icon(
                        DidvanIcons.calendar_day_light,
                        size: 18,
                      ),
                      const SizedBox(width: 4),
                      DidvanText(
                        DateTime.parse(item.createdAt).toPersianDateStr(),
                        style: Theme.of(context).textTheme.overline,
                      ),
                      const Spacer(),
                      if ((item.timeToRead ?? item.duration) != null) ...[
                        const Icon(
                          DidvanIcons.timer_light,
                          size: 18,
                        ),
                        const SizedBox(width: 4),
                        DidvanText(
                          item.timeToRead != null
                              ? 'خواندن در ${item.timeToRead} دقیقه'
                              : DateTimeUtils.normalizeTimeDuration(
                                  Duration(seconds: item.duration!),
                                ),
                          style: Theme.of(context).textTheme.overline,
                        ),
                      ]
                    ],
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

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