import 'package:didvan/models/view/app_bar_data.dart';
import 'package:didvan/views/widgets/didvan/app_bar.dart';
import 'package:flutter/material.dart';

class DidvanScaffold extends StatefulWidget {
  final List<Widget>? slivers;
  final List<Widget>? children;
  final AppBarData appBarData;
  final EdgeInsets padding;
  final Color? backgroundColor;
  final bool reverse;

  const DidvanScaffold({
    Key? key,
    this.slivers,
    required this.appBarData,
    this.children,
    this.padding = const EdgeInsets.symmetric(horizontal: 16),
    this.backgroundColor,
    this.reverse = false,
  }) : super(key: key);

  @override
  State<DidvanScaffold> createState() => _DidvanScaffoldState();
}

class _DidvanScaffoldState extends State<DidvanScaffold> {
  final _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    final double statusBarHeight = MediaQuery.of(context).padding.top;
    return Scaffold(
      backgroundColor: widget.backgroundColor,
      body: Padding(
        padding: EdgeInsets.only(top: statusBarHeight),
        child: Stack(
          children: [
            CustomScrollView(
              controller: _scrollController,
              reverse: widget.reverse,
              slivers: [
                if (!widget.reverse)
                  SliverAppBar(
                    toolbarHeight: kToolbarHeight,
                    backgroundColor: widget.backgroundColor ??
                        Theme.of(context).colorScheme.background,
                    automaticallyImplyLeading: false,
                    pinned: true,
                    flexibleSpace: DidvanAppBar(appBarData: widget.appBarData),
                  ),
                if (!widget.reverse)
                  const SliverToBoxAdapter(
                    child: SizedBox(height: 16),
                  ),
                if (widget.children != null)
                  SliverPadding(
                    padding: widget.padding,
                    sliver: SliverList(
                      delegate: SliverChildBuilderDelegate(
                        (context, index) => widget.children![index],
                        childCount: widget.children!.length,
                      ),
                    ),
                  ),
                if (widget.slivers != null)
                  for (var i = 0; i < widget.slivers!.length; i++)
                    SliverPadding(
                      padding: widget.padding,
                      sliver: widget.slivers![i],
                    ),
                if (widget.reverse)
                  SliverToBoxAdapter(
                    child: SizedBox(
                      height: kToolbarHeight +
                          MediaQuery.of(context).padding.top +
                          12,
                    ),
                  ),
              ],
            ),
            if (widget.reverse)
              _AppBar(
                appBarData: widget.appBarData,
                scrollController: _scrollController,
              ),
          ],
        ),
      ),
    );
  }
}

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

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

class __AppBarState extends State<_AppBar> {
  bool _isScrolled = false;

  @override
  void initState() {
    widget.scrollController.addListener(() {
      final position = widget.scrollController.position.pixels;
      if (position > 10 && _isScrolled == false) {
        setState(() {
          _isScrolled = true;
        });
      }
      if (position < 10 && _isScrolled == true) {
        setState(() {
          _isScrolled = false;
        });
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DidvanAppBar(
      backgroundColor: Theme.of(context).colorScheme.surface,
      appBarData: widget.appBarData,
      hasBorder: _isScrolled,
    );
  }
}
