import 'package:didvan/models/requests/radar.dart';
import 'package:didvan/views/radar/radar_details/radar_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 RadarDetails extends StatefulWidget {
  final Map<String, dynamic> pageData;

  const RadarDetails({Key? key, required this.pageData}) : super(key: key);

  @override
  State<RadarDetails> createState() => _RadarDetailsState();
}

class _RadarDetailsState extends State<RadarDetails> {
  final ScrollController _scrollController = ScrollController();

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

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

  void _onPageChanged(int index) {
    final state = context.read<RadarDetailsState>();
    state.getRadarDetails(
      state.radars[index]!.id,
      isForward: state.currentIndex < index,
    );
  }
}
