import 'package:didvan/models/requests/news.dart';
import 'package:didvan/views/news/news_details/news_details_state.dart';
import 'package:didvan/views/widgets/floating_navigation_bar.dart';
import 'package:didvan/views/widgets/didvan/page_view.dart';
import 'package:didvan/views/widgets/state_handlers/state_handler.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class NewsDetails extends StatefulWidget {
  final Map<String, dynamic> pageData;
  const NewsDetails({Key? key, required this.pageData}) : super(key: key);

  @override
  State<NewsDetails> createState() => _NewsDetailsState();
}

class _NewsDetailsState extends State<NewsDetails> {
  final ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    final state = context.read<NewsDetailsState>();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels >
              _scrollController.position.maxScrollExtent - 200 &&
          !state.relatedQueue.contains(state.currentNews.id)) {
        state.getRelatedContents();
      }
    });
    state.args = widget.pageData['args'] ?? const NewsRequestArgs(page: 0);
    state.initialDescription = widget.pageData['description'];
    Future.delayed(Duration.zero, () {
      state.getNewsDetails(widget.pageData['id']);
    });
    if (widget.pageData['goToComment'] != null) {
      state.openComments = widget.pageData['goToComment'];
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Consumer<NewsDetailsState>(
        builder: (context, state, child) => StateHandler<NewsDetailsState>(
          onRetry: () => state.getNewsDetails(widget.pageData['id']),
          state: state,
          builder: (context, state) => Stack(
            children: [
              IgnorePointer(
                ignoring: state.isFetchingNewItem,
                child: DidvanPageView(
                  isRadar: false,
                  initialIndex: state.initialIndex,
                  onPageChanged: _onPageChnaged,
                  scrollController: _scrollController,
                  items: state.news,
                  currentIndex: state.currentIndex,
                  onMarkChanged: (id, value) =>
                      widget.pageData['onMarkChanged'](id, value),
                ),
              ),
              Positioned(
                bottom: 0,
                left: 0,
                right: 0,
                child: FloatingNavigationBar(
                  openComments: state.openComments,
                  hasUnmarkConfirmation:
                      widget.pageData['hasUnmarkConfirmation'] ?? false,
                  scrollController: _scrollController,
                  item: state.currentNews,
                  onCommentsChanged: state.onCommentsChanged,
                  onMarkChanged: (value) {
                    state.currentNews.marked = value;
                    widget.pageData['onMarkChanged'](
                      state.currentNews.id,
                      value,
                    );
                  },
                  isRadar: false,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _onPageChnaged(int index) {
    final state = context.read<NewsDetailsState>();
    state.getNewsDetails(
      state.news[index]!.id,
      isForward: state.currentIndex < index,
    );
  }
}
