import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/bbs/model/brand_model.dart';
import 'package:liftmanager/internal/means/means_router.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/internal/search/presenter/brand_search_presenter.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/widgets/my_refresh_list.dart';
import 'package:liftmanager/widgets/search_app_bar.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:provider/provider.dart';

class SearchBrand extends StatefulWidget {
  @override
  SearchBrandState createState() => SearchBrandState();
}

class SearchBrandState
    extends BasePageState<SearchBrand, BrandSearchPresenterSeconds> {
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  int _page = 1;
  String searchWord = '';

  ScrollController _scrollController = new ScrollController();

  @override
  void initState() {
    provider.setStateTypeNotNotify(StateType.empty);
    super.initState();
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;

    return ChangeNotifierProvider<BaseListProvider<Records>>(
      create: (_) => provider,
      child: Scaffold(
        appBar: SearchAppBar(
          hintText: "请输入需要搜索的内容",
          onPressed: (text) {
            if (text.isEmpty) {
              toasts("搜索关键字不能为空！");
              return;
            }
            FocusScope.of(context).unfocus();
            searchWord = text;
            _onRefresh();
          },
        ),
        body: Container(
          child: Column(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Consumer<BaseListProvider<Records>>(
                  builder: (_, provider, __) {
                    return MyListView(
                      key: Key('brand_list'),
                      itemCount: provider.list.length,
                      stateType: provider.stateType,
                      onRefresh: _onRefresh,
                      loadMore: _loadMore,
                      hasMore: provider.hasMore,
                      itemBuilder: (_, index) {
                        return GestureDetector(
                          child: Container(
                            padding: EdgeInsets.only(
                              left: ScreenUtil().setWidth(15),
                              right: ScreenUtil().setWidth(15),
                              top: ScreenUtil().setHeight(10),
                              bottom: ScreenUtil().setHeight(10),
                            ),
                            decoration: BoxDecoration(
                              border: Border(
                                bottom:
                                    BorderSide(width: 0.5, color: Colours.line),
                              ),
                            ),
                            child: Column(children: <Widget>[
                              Container(
                                padding: EdgeInsets.only(
                                    bottom: ScreenUtil().setHeight(5)),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Row(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      children: <Widget>[
                                        Container(
                                          padding: EdgeInsets.only(
                                              right: ScreenUtil().setWidth(10)),
                                          child: ClipRRect(
                                            borderRadius: BorderRadius.circular(
                                                ScreenUtil().setWidth(18)),
                                            child: Container(
                                              width: ScreenUtil().setWidth(36),
                                              height: ScreenUtil().setWidth(36),
                                              color: Color(0xffFAF7FA),
                                              padding: EdgeInsets.all(2),
                                              child: LoadNetworkImage(
                                                provider.list[index].logo,
                                                fit: BoxFit.cover,
                                                width:
                                                    ScreenUtil().setWidth(100),
                                                height:
                                                    ScreenUtil().setWidth(100),
                                              ),
                                            ),
                                          ),
                                        ),
                                        Container(
                                          child: Text(
                                            provider.list[index].name ?? '',
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                              fontSize: ScreenUtil().setSp(14),
                                              color: Color(0xff333333),
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                    Container(
                                      child: Icon(
                                        Icons.keyboard_arrow_right,
                                        size: 26.0,
                                        color: Color(0xffdddddd),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ]),
                          ),
                          onTap: () {
                            NavigatorUtils.push(context,
                                "${MeansRouter.brandDetail}?id=${provider.list[index].id.toString()}");
                          },
                        );
                      },
                    );
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.getBrandList(_page, searchWord);
  }

  Future _loadMore() async {
    _page++;
    await presenter.getBrandList(_page, searchWord);
  }

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