import 'dart:async';

import 'package:flutter/material.dart';
import 'package:liftmanager/internal/bbs/presenter/mix_search_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/widgets/bbs_content.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';

enum ListType { all, news, video, question, goods, position }

class SearchIndex extends StatefulWidget {
  SearchIndex(this.type, this.text);

  final ListType type;
  final String text;

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

class SearchIndexState extends BasePageState<SearchIndex, MixListPresenter> {
  BaseListProvider provider = BaseListProvider();

  int _page = 1;
  ListType selectedType;

  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.loading);
    super.initState();
    _onRefresh();
    print(widget.text);
  }

  @override
  Widget build(BuildContext context) {
    if (widget.type != selectedType) {
      selectedType = widget.type;
      provider.clear();
      _onRefresh();
    }

    return Container(
        child: Stack(
      children: <Widget>[
        Container(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: ChangeNotifierProvider<BaseListProvider>(
                create: (_) => provider,
                child: Column(
                  children: <Widget>[
                    Expanded(
                        flex: 1,
                        child: Consumer<BaseListProvider>(
                            builder: (_, provider, __) {
                          return MyListView(
                            key: Key('mix_search_list'),
                            itemCount: provider.list.length,
                            stateType: provider.stateType,
                            onRefresh: _onRefresh,
                            loadMore: _loadMore,
                            hasMore: provider.hasMore,
                            itemBuilder: (_, index) {
                              return Column(
                                children: <Widget>[
                                  widget.type == ListType.news
                                      ? NewsCell(provider.list[index])
                                      : Container(child: null),
                                  widget.type == ListType.question
                                      ? QuestionCell(item: provider.list[index])
                                      : Container(child: null),
                                  widget.type == ListType.video
                                      ? VideoCell(provider.list[index])
                                      : Container(child: null),
                                  widget.type == ListType.position
                                      ? PositionCell(provider.list[index])
                                      : Container(child: null),
                                  widget.type == ListType.goods
                                      ? ProductCell(provider.list[index])
                                      : Container(child: null),
                                ],
                              );
                            },
                          );
                        }))
                  ],
                )))
      ],
    ));
  }

  getList() async {
    if (widget.type == ListType.question) {
      await presenter.getQuestionList(_page, widget.text);
    } else if (widget.type == ListType.video) {
      await presenter.getVideoList(_page, widget.text);
    } else if (widget.type == ListType.goods) {
      await presenter.getShopList(_page, widget.text, null);
    } else if (widget.type == ListType.position) {
      await presenter.getPositionList(_page, widget.text);
    } else if (widget.type == ListType.news) {
      await presenter.getNewsList(_page, widget.text);
    }
  }

  Future _onRefresh() async {
    _page = 1;

    getList();
  }

  Future _loadMore() async {
    _page++;

    getList();
  }

  @override
  MixListPresenter createPresenter() {
    return MixListPresenter();
  }
}
