import 'package:carousel_slider/carousel_slider.dart';
import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/utils/date_time.dart';
import 'package:didvan/views/home/widgets/multitype_overview.dart';
import 'package:didvan/views/home/widgets/tag_item.dart';
import 'package:didvan/views/widgets/animated_visibility.dart';
import 'package:didvan/views/widgets/didvan/card.dart';
import 'package:didvan/views/widgets/didvan/divider.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/ink_wrapper.dart';
import 'package:didvan/views/widgets/item_title.dart';
import 'package:didvan/views/widgets/skeleton_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

class DidvanPageView extends StatefulWidget {
  final List items;
  final int initialIndex;
  final int currentIndex;
  final bool isRadar;
  final ScrollController scrollController;

  final void Function(int index) onPageChanged;
  const DidvanPageView({
    Key? key,
    required this.initialIndex,
    required this.items,
    required this.scrollController,
    required this.onPageChanged,
    required this.isRadar,
    required this.currentIndex,
  }) : super(key: key);

  @override
  State<DidvanPageView> createState() => _DidvanPageViewState();
}

class _DidvanPageViewState extends State<DidvanPageView> {
  @override
  Widget build(BuildContext context) {
    final double deviceTopPadding = MediaQuery.of(context).padding.top;
    return Stack(
      children: [
        Directionality(
          textDirection: TextDirection.ltr,
          child: CarouselSlider.builder(
            itemCount: widget.items.length,
            options: CarouselOptions(
              onPageChanged: (index, reason) => widget.onPageChanged(index),
              height: double.infinity,
              initialPage: widget.initialIndex,
              viewportFraction: 0.94,
              enableInfiniteScroll: false,
            ),
            itemBuilder: (context, index, realIndex) => Directionality(
              textDirection: TextDirection.rtl,
              child: SizedBox(
                height: MediaQuery.of(context).size.height,
                child: SingleChildScrollView(
                  controller: index == widget.currentIndex
                      ? widget.scrollController
                      : null,
                  physics: const BouncingScrollPhysics(),
                  padding: EdgeInsets.only(
                    left: 4,
                    right: 4,
                    top: 16 + deviceTopPadding,
                    bottom: 92,
                  ),
                  child: DidvanCard(
                    padding: EdgeInsets.zero,
                    enableBorder: false,
                    child: Builder(
                      builder: (context) {
                        final item = widget.items[index];
                        if (item == null) {
                          return const SizedBox();
                        }
                        return Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SkeletonImage(
                              imageUrl: item.image,
                              aspectRatio: 16 / 9,
                            ),
                            const SizedBox(height: 20),
                            Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 16),
                              child: DidvanText(
                                item.title,
                                style: Theme.of(context).textTheme.subtitle1,
                              ),
                            ),
                            const SizedBox(height: 8),
                            Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 16),
                              child: _subtitle(item),
                            ),
                            for (var i = 0; i < item.contents.length; i++)
                              Padding(
                                padding: const EdgeInsets.symmetric(
                                  horizontal: 16,
                                  vertical: 4,
                                ),
                                child: _contentBuilder(item, i),
                              ),
                            if (item.tags.isNotEmpty)
                              const SizedBox(height: 20),
                            Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 16),
                              child: Wrap(
                                spacing: 8,
                                runSpacing: 8,
                                children: [
                                  for (var i = 0; i < item.tags.length; i++)
                                    TagItem(tag: item.tags[i]),
                                ],
                              ),
                            ),
                            const Padding(
                              padding: EdgeInsets.only(
                                left: 16,
                                right: 16,
                                top: 16,
                              ),
                              child: DidvanDivider(verticalPadding: 0),
                            ),
                            const Padding(
                              padding: EdgeInsets.symmetric(horizontal: 16.0),
                              child: ItemTitle(title: 'مطالب مشابه'),
                            ),
                            if (item.relatedContents.isEmpty)
                              for (var i = 0; i < 3; i++)
                                Padding(
                                  padding: const EdgeInsets.only(
                                    bottom: 8,
                                    left: 16,
                                    right: 16,
                                  ),
                                  child: MultitypeOverview.placeholder,
                                ),
                            for (var i = 0;
                                i < item.relatedContents.length;
                                i++)
                              Padding(
                                padding: const EdgeInsets.only(
                                  bottom: 8,
                                  left: 16,
                                  right: 16,
                                ),
                                child: MultitypeOverview(
                                  item: item.relatedContents[i],
                                  onMarkChanged: (id, value) {},
                                ),
                              ),
                            const SizedBox(height: 8),
                          ],
                        );
                      },
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
        Positioned(
          child: _BackButton(scrollController: widget.scrollController),
          right: 24,
          top: 24 + deviceTopPadding,
        ),
      ],
    );
  }

  Widget _contentBuilder(dynamic item, int index) {
    final content = item.contents[index];
    if (content.text != null) {
      return Html(
        data: content.text,
        style: {
          '*': Style(
            direction: TextDirection.rtl,
            lineHeight: LineHeight.percent(135),
            margin: EdgeInsets.zero,
            padding: EdgeInsets.zero,
          ),
        },
      );
    }

    if (content.image != null) {
      return Column(
        children: [
          GestureDetector(
            onTap: () {
              showDialog(
                context: context,
                builder: (context) => Stack(
                  children: [
                    Positioned.fill(
                      child: InteractiveViewer(
                        child: Center(
                          child: SkeletonImage(
                            width: MediaQuery.of(context).size.width,
                            imageUrl: content.largeImage ?? content.image,
                          ),
                        ),
                      ),
                    ),
                    const Positioned(
                      right: 24,
                      top: 24,
                      child: _BackButton(),
                    ),
                  ],
                ),
              );
            },
            child: SkeletonImage(
              imageUrl: content.image!,
              width: double.infinity,
            ),
          ),
          if (content.caption != null)
            Center(
              child: DidvanText(
                content.caption,
                textAlign: TextAlign.center,
                style: Theme.of(context).textTheme.caption,
              ),
            ),
        ],
      );
    }
    return const SizedBox();
  }

  Widget _subtitle(dynamic item) {
    if (widget.isRadar) {
      return Wrap(
        crossAxisAlignment: WrapCrossAlignment.center,
        children: [
          DidvanText(
            'رادار ',
            style: Theme.of(context).textTheme.caption,
          ),
          for (var i = 0; i < item.categories.length; i++)
            DidvanText(
              item.categories[i].label +
                  '${i != item.categories.length - 1 ? '،' : ''} ',
              style: Theme.of(context).textTheme.caption,
            ),
          DidvanText(
            ' - ' + DateTimeUtils.momentGenerator(item.createdAt),
            style: Theme.of(context).textTheme.caption,
          ),
        ],
      );
    } else {
      return Row(
        children: [
          DidvanText(
            item.reference,
            style: Theme.of(context).textTheme.caption,
          ),
          DidvanText(
            ' - ' + DateTimeUtils.momentGenerator(item.createdAt),
            style: Theme.of(context).textTheme.caption,
          ),
        ],
      );
    }
  }
}

class _BackButton extends StatefulWidget {
  final ScrollController? scrollController;
  const _BackButton({Key? key, this.scrollController}) : super(key: key);

  @override
  __BackButtonState createState() => __BackButtonState();
}

class __BackButtonState extends State<_BackButton> {
  bool _isVisible = false;

  @override
  void didUpdateWidget(covariant _BackButton oldWidget) {
    if (widget.scrollController != null) {
      _isVisible = false;
      _handleScroll();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void initState() {
    if (widget.scrollController != null) {
      _handleScroll();
    } else {
      _isVisible = true;
    }
    super.initState();
  }

  void _handleScroll() {
    widget.scrollController!.addListener(() {
      final position = widget.scrollController!.position.pixels;
      final size = MediaQuery.of(context).size.height * 0.3;
      if (position > size && _isVisible == false) {
        setState(() {
          _isVisible = true;
        });
      }
      if (position < size && _isVisible == true) {
        setState(() {
          _isVisible = false;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedVisibility(
      duration: DesignConfig.lowAnimationDuration,
      isVisible: _isVisible,
      child: InkWrapper(
        borderRadius: DesignConfig.lowBorderRadius,
        onPressed: Navigator.of(context).pop,
        child: Container(
          decoration: BoxDecoration(
            color: Theme.of(context).colorScheme.splash,
            border: Border.all(color: Theme.of(context).colorScheme.border),
            borderRadius: DesignConfig.lowBorderRadius,
          ),
          child: const Icon(
            DidvanIcons.back_regular,
            size: 32,
          ),
        ),
      ),
    );
  }
}
