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

class BrandList extends StatefulWidget {
  @override
  BrandListState createState() => BrandListState();
}

class BrandListState
    extends BasePageState<BrandList, BrandListPresenterSeconds> {
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  int _page = 1;

  ScrollController _scrollController = new ScrollController();

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

  @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(
          onPressed: () {
            NavigatorUtils.push(context, SearchRouter.searchBrand);
          }
        ),
        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),
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                    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);
  }

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

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