import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
import 'package:zhiying_base_widget/utils/contants.dart';
import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/util/custom_sliver_persistent_header_delegate.dart';

import 'wallet_page_bloc.dart';
import 'wallet_page_bloc.dart';

class WalletPage extends StatefulWidget {
  final Map<String, dynamic> data;

  const WalletPage({Key key, this.data}) : super(key: key);

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

class _WalletPageState extends State<WalletPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfff9f9f9),
      body: MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: MainPageNotifier()),
          ChangeNotifierProvider.value(value: MainPageBgNotifier()),
        ],
        child: BlocProvider<WalletPageBloc>(
          bloc: WalletPageBloc(),
          child: _WalletPageContainer(data: widget.data),
        ),
      ),
    );
  }
}

class _WalletPageContainer extends StatefulWidget {
  _WalletPageContainer({Key key, this.data}) : super(key: key);

  final Map<String, dynamic> data;

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

class _WalletPageContainerState extends State<_WalletPageContainer> {
  final ScrollController _controller = ScrollController();
  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  WalletPageBloc _pageBloc;

  @override
  void initState() {
    _pageBloc = BlocProvider.of<WalletPageBloc>(context);
    if (widget.data.containsKey(Constants.SkipIdentifierName)) {
      _pageBloc.loadData(widget.data[Constants.SkipIdentifierName]);
    }
    super.initState();
  }

  void _onLoading() async {
    // await Future.delayed(Duration(milliseconds: 1000));
    // if (mounted) setState(() {});
    // _refreshController.loadComplete();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334);
    return SmartRefresher(
      controller: _refreshController,
      enablePullDown: false,
      enablePullUp: false,
      header: WaterDropHeader(),
      onLoading: _onLoading,
      child: Container(
        width: double.infinity,
        child: Stack(
          fit: StackFit.passthrough,
          children: <Widget>[
            MineHeaderBgWidget(
              controller: _controller,
            ),
            StreamBuilder(
              stream: _pageBloc.outData,
              builder: (context, asyncSnapshot) {
                var model = asyncSnapshot.data;
                return CustomScrollView(
                  slivers: _createContent(context, model),
                );
              },
            )
          ],
        ),
      ),
    );
  }

  List<Widget> _createContent(
      BuildContext context, List<Map<String, dynamic>> model) {
    List<Widget> list = List();
    if (model == null) {
      list.addAll(WidgetFactory.create(
        'normal_nav',
        isSliver: true,
        model: Map(),
      ));
      list.addAll(WidgetFactory.create(
        'wallet_data',
        isSliver: true,
        model: Map(),
      ));
      list.addAll(WidgetFactory.create(
        'wallet_detail',
        isSliver: true,
        model: Map(),
      ));

      list.addAll(WidgetFactory.create(
        'wallet_income',
        isSliver: true,
        model: Map(),
      ));
    } else {
      for (var item in model) {
        WidgetModel widgetModel =
            WidgetModel.fromJson(Map<String, dynamic>.from(item));
        if (item.containsKey("mod_name")&&item['mod_name']=="my_wallet_appbar") {
          list.add(SliverPersistentHeader(
              delegate: _SilverAppBarDelegate(context,
                  WidgetFactory.create(widgetModel.modName,
                      isSliver: false, model: item)[0]),pinned: true,));
         continue;
        }
        list.addAll(WidgetFactory.create(widgetModel.modName,
            isSliver: true, model: item));
      }
    }
    return list;
  }
}

class _SilverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SilverAppBarDelegate(BuildContext context,this.child){
    this.context=context;
  }
  BuildContext context;
  final Widget child;

  @override
  double get minExtent =>  MediaQuery.of(context).padding.top+44;

  @override
  double get maxExtent => MediaQuery.of(context).padding.top+44;

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return child;

  }

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return false;
  }
}
