import 'dart:async';

import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zhiying_base_widget/pages/orders_page/bloc/order_content_bloc.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_filter_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_page_style_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/views/order_item_widget.dart';
import 'package:zhiying_base_widget/widgets/empty/empty_widget.dart';
import 'package:zhiying_comm/util/base_bloc.dart';

class OrderContentPage extends StatefulWidget {
  final String state; // 订单状态
  final OrderPageStyleModel style;
  final OrderFilterModel filter;
  final int index;
  final EventBus eventBus;

  const OrderContentPage(this.state,
      {Key key, this.filter, this.style, this.index, this.eventBus})
      : super(key: key);

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

class _OrderContentPageState extends State<OrderContentPage> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<OrderContentBloc>(
      bloc: OrderContentBloc(),
      child: OrderContentContainer(
        widget.state,
        widget.filter,
        widget.style,
        index: widget.index,
        eventBus: widget.eventBus,
      ),
    );
  }
}

class OrderContentContainer extends StatefulWidget {
  final String state; // 订单状态
  final OrderFilterModel filter;
  final OrderPageStyleModel style;
  final int index;
  final EventBus eventBus;

  const OrderContentContainer(this.state, this.filter, this.style,
      {Key key, this.index, this.eventBus})
      : super(key: key);

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

class _OrderContentContainerState extends State<OrderContentContainer> {
  OrderContentBloc _bloc;

  StreamSubscription streamSubscription;

  @override
  void initState() {
    _bloc = BlocProvider.of(context);
    _bloc.refresh(widget.state, widget.filter);

   streamSubscription=  widget.eventBus.on<int>().listen((index) {
      print('eventbus' + index.toString());
      if (index == widget.index) {
        _bloc.refresh(widget.state, widget.filter);
      }
    });

    super.initState();
  }
  @override
  void dispose() {
    streamSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print(context.toString());
    return StreamBuilder<List<OrderModel>>(
        stream: _bloc.outData,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.data == null) {
            return Container();
          }

          List<OrderModel> orders = snapshot.data;
          if (orders.length == 0) {
            return Container(
              child: Center(
                child: EmptyWidget(tips: '暂无订单，快去下一单吧'),
              ),
            );
          }
          return SmartRefresher(
            enablePullDown: true,
            enablePullUp: true,
            header: WaterDropHeader(),
            controller: _bloc.refreshController,
            onLoading: () {
              _bloc.loadMore();
            },
            onRefresh: () {
              _bloc.refresh(widget.state, widget.filter);
            },
            child: ListView.builder(
                itemCount: orders.length,
                itemBuilder: (context, index) {
                  return OrderItemWidget(
                    orders[index],
                    widget.style,
                  );
                }),
          );
        });
  }
}
