import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/search_result_page/bloc/search_result_bloc.dart';
import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_bloc.dart';
import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_repository.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'bloc/search_result_repository.dart';

///
/// 搜索结果页面
///
class SearchResultPage extends StatelessWidget {
  final Map<String, dynamic> data;

  const SearchResultPage(this.data);

  @override
  Widget build(BuildContext context) {
    // return BlocProvider<SearchResultBloc>(
    //   create: (_) => SearchResultBloc(repository: SearchResultRepository())..add(SearchResultInitEvent(model: data)),
    //   child: SearchResultContianer(),
    // );

    return MultiBlocProvider(
      providers: [
        /// 页面的数据
        BlocProvider<SearchResultBloc>(
          create: (_) => SearchResultBloc(repository: SearchResultRepository())..add(SearchResultInitEvent(model: data)),
        ),

        /// 输入框联想的bloc
        BlocProvider<SearchThinkBloc>(
          create: (_)=> SearchThinkBloc(SearchThinkRepository()),
        ),
      ],
      child: SearchResultContianer(),
    );
  }
}

class SearchResultContianer extends StatefulWidget {
  @override
  _SearchResultContianerState createState() => _SearchResultContianerState();
}

class _SearchResultContianerState extends State<SearchResultContianer> {
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<SearchResultBloc, SearchResultState>(
      listener: (BuildContext context, SearchResultState state) {
        if (state is SearchResultErrorState) {
          print('数据加载出错');
        }
      },
      buildWhen: (previous, current) {
        /// 数据加载出错不进行build
        if (current is SearchResultErrorState) {
          return false;
        }

        return true;
      },
      builder: (context, state) {
        print('currente state = $state');
        if (state is SearchResultLoadedState) {
          return _getMainWidget(state?.model);
        }

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

  /// 获取主视图
  Widget _getMainWidget(List<Map<String, dynamic>> data) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: _createContentWidget(data),
      ),
    );
  }

  /// 创建视图
  List<Widget> _createContentWidget(List<Map<String, dynamic>> datas) {
    int length = datas?.length ?? 0;
    List<Widget> list = [];

    if (length > 0) {
      for (int i = 0; i < length; i++) {
        WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
        print('item.modName ${item.modName}');
        list.addAll(WidgetFactory.create(
          item.modName,
          isSliver: false,
          model: datas[i],
        ));
      }
    } else {
      list.add(Container());
    }

    return list;
  }
}
