import 'dart:math';

import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/home_page_content/content.dart';
import 'package:didvan/providers/user.dart';
import 'package:didvan/views/home/home_state.dart';
import 'package:didvan/views/home/main/main_page_state.dart';
import 'package:didvan/views/widgets/didvan/card.dart';
import 'package:didvan/views/widgets/didvan/text.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';
import 'package:provider/provider.dart';

class MainPageGeneralItem extends StatefulWidget {
  final MainPageContentType content;
  final String type;
  const MainPageGeneralItem(
      {super.key, required this.content, required this.type});

  @override
  State<MainPageGeneralItem> createState() => _MainPageGeneralItemState();
}

class _MainPageGeneralItemState extends State<MainPageGeneralItem> {
  IconData _generateSubtitleIcon(int index) {
    switch (widget.type) {
      case 'news':
        return DidvanIcons.calendar_day_light;
      case 'radar':
        if (index == 0) {
          return DidvanIcons.calendar_day_light;
        } else {
          return DidvanIcons.timer_light;
        }
      case 'video':
        if (index == 0) {
          return DidvanIcons.calendar_day_light;
        } else {
          return DidvanIcons.timer_light;
        }
      case 'startup':
        if (index == 0) {
          return DidvanIcons.location_light;
        }
        return DidvanIcons.puzzle_light;
      default:
        return DidvanIcons.puzzle_light;
    }
  }

  String _formatSubtitle(int index) {
    switch (widget.type) {
      case 'news':
        return DateTime.parse(widget.content.subtitles[index])
            .toPersianDateStr();
      case 'radar':
        if (index == 0) {
          return DateTime.parse(widget.content.subtitles[index])
              .toPersianDateStr();
        } else {
          return '${widget.content.subtitles[index]} دقیقه';
        }
      case 'video':
        if (index == 0) {
          return DateTime.parse(widget.content.subtitles[index])
              .toPersianDateStr();
        } else {
          return '${widget.content.subtitles[index]} دقیقه';
        }
      case 'startup':
      default:
        return widget.content.subtitles[index];
    }
  }

  void _onMarkChange() {
    UserProvider.changeItemMark(
      widget.type,
      widget.content.id,
      !widget.content.marked,
    );
    setState(() => widget.content.marked = !widget.content.marked);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => context.read<MainPageState>().navigationHandler(
            widget.type,
            widget.content.id,
            widget.content.link,
          ),
      child: DidvanCard(
        padding: EdgeInsets.zero,
        child: Column(
          children: [
            Stack(
              children: [
                SkeletonImage(
                  imageUrl: widget.content.image,
                  height: 140,
                  width: double.infinity,
                  borderRadius:
                      const BorderRadius.vertical(top: Radius.circular(10)),
                ),
                if (widget.type == 'video')
                  Positioned.fill(
                    child: Center(
                      child: Container(
                        height: 36,
                        width: 36,
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: Theme.of(context)
                              .colorScheme
                              .secondary
                              .withValues(alpha: 0.7),
                        ),
                        child: Icon(
                          DidvanIcons.play_solid,
                          color: Theme.of(context).colorScheme.white,
                        ),
                      ),
                    ),
                  ),
                if (widget.type == 'radar')
                  Positioned(
                    left: 0,
                    bottom: 0,
                    child: Container(
                      width: 36,
                      height: 36,
                      padding: const EdgeInsets.all(4),
                      decoration: BoxDecoration(
                        borderRadius: const BorderRadius.only(
                          topRight: Radius.circular(10),
                        ),
                        color: Theme.of(context).colorScheme.surface,
                      ),
                      child: SvgPicture.asset(
                        context
                                .read<HomeState>()
                                .categories
                                .firstWhere(
                                  (element) =>
                                      element.id.toString() ==
                                      widget.content.subtitles[2],
                                )
                                .asset ??
                            '',
                      ),
                    ),
                  ),
              ],
            ),
            Expanded(
              child: Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                      child: DidvanText(
                        widget.content.title,
                        style: Theme.of(context).textTheme.bodyLarge,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Column(
                      children: [
                        for (int i = 0;
                            i <
                                min(
                                  widget.content.subtitles.length,
                                  widget.type == 'radar'
                                      ? 2
                                      : widget.content.subtitles.length,
                                );
                            i++)
                          _subtitleItem(i, context)
                      ],
                    )
                  ],
                ),
              ),
            ),
            GestureDetector(
              onTap: _onMarkChange,
              child: Padding(
                padding: const EdgeInsets.only(left: 12, right: 12, bottom: 8),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Icon(
                      widget.content.marked
                          ? DidvanIcons.bookmark_solid
                          : DidvanIcons.bookmark_regular,
                      color: widget.content.marked
                          ? Theme.of(context).colorScheme.secondary
                          : Theme.of(context).colorScheme.caption,
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _subtitleItem(int i, BuildContext context) => Row(
        mainAxisAlignment:
            i == 0 ? MainAxisAlignment.start : MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Icon(
            _generateSubtitleIcon(i),
            size: 16,
          ),
          const SizedBox(width: 4),
          Expanded(
            child: DidvanText(
              _formatSubtitle(i),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: Theme.of(context).textTheme.bodySmall,
            ),
          ),
        ],
      );
}
