import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zhiying_base_widget/pages/search_page/item/bloc/search_item_page_bloc.dart';
import 'package:zhiying_base_widget/pages/search_page/item/bloc/search_item_repostitory.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

  const SearchItemPage(this.data);

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

class _SearchItemPageState extends State<SearchItemPage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    return BlocProvider<SearchItemPageBloc>(
      create: (_) => SearchItemPageBloc(repository: SearchItemRepository())..add(SearchItemPageInitEvent(model: widget?.data)),
      child: SearchItemPageContainer(),
    );
  }
}

class SearchItemPageContainer extends StatefulWidget {
  @override
  _SearchItemPageContainerState createState() => _SearchItemPageContainerState();
}

class _SearchItemPageContainerState extends State<SearchItemPageContainer> {
  RefreshController _refreshController;
  ScrollController _controller;

  @override
  void initState() {
    _refreshController = RefreshController(initialRefresh: false);
    _controller = ScrollController();
    super.initState();
  }

  @override
  void dispose() {
    _refreshController?.dispose();
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<SearchItemPageBloc, SearchItemPageState>(
      listener: (BuildContext context, SearchItemPageState state) {
        if (state is SearchItemErrorState) {
          print('数据加载出错');
        }
      },
      buildWhen: (previous, current) {
        /// 数据加载出错不进行build
        if (current is SearchItemErrorState) {
          return false;
        }
        return true;
      },
      builder: (context, state) {
        print('currente state = $state');
        if (state is SearchItemLoadedState) {
          return _getMainWidget(state?.model ?? null);
        }

        /// 骨架屏幕
        return _getMainWidget(null);
      },
    );
  }

  /// 主视图
  Widget _getMainWidget(List<Map<String, dynamic>> datas) {
    return Scaffold(
      backgroundColor: HexColor.fromHex('#F9F9F9'),
      body: SmartRefresher(
          enablePullDown: false,
          enablePullUp: true,
          footer: ClassicFooter(),
          header: WaterDropHeader(),
          controller: _refreshController,
          child: Container(
              width: double.infinity,
              child: CustomScrollView(
                controller: _controller,
                slivers: _createContentWidget(datas),
              ))),
    );
  }

  /// 构造整体视图
  List<Widget> _createContentWidget(List<Map<String, dynamic>> datas) {
    List<Widget> list = [];
    int length = datas?.length ?? 0;
    if (length > 0) {
      for (int i = 0; i < datas.length; i++) {
        WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
        print('item.modName ${item.modName}');
        list.addAll(WidgetFactory.create(
          item.modName,
          isSliver: true,
          model: datas[i],
        ));
      }
    } else {
      list.add(SliverToBoxAdapter(
        /// TODO 改成骨架图
        child: Container(child: Text('暂无数据～')),
      ));
    }
    return list;
  }
}
