import 'package:flutter/material.dart';
import 'package:liftmanager/internal/bbs/model/brand_model_type.dart';
import 'package:liftmanager/internal/bbs/model/mix_model.dart';
import 'package:liftmanager/internal/bbs/presenter/product_list_presenter.dart';
import 'package:liftmanager/internal/search/presenter/base_list_provider.dart';
import 'package:liftmanager/mvp/base_page_state.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_search_bar.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';

import '../../brand_page.dart';

class ProductList extends StatefulWidget {
  // QuestionList(this.index);

  // final String index;

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

class ProductListState
    extends BasePageState<ProductList, ProductListPresenter> {
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  ScrollController _scrollController = new ScrollController();
  int _page = 1;
  String searchWord;
  List<dynamic> brandList;
  List<dynamic> hotBrandList;
  int activeFilterIndex = 0;
  bool showAllBrands = false;
  int selectedBrandId;

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

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

  Future getBrandList() async {
    await NewApiService().getBrandListType(null, onSuccess: (res) {
      if (res != null) {
        BrandModelType bm = res[0];
        brandList = res;
        hotBrandList = bm?.listBrand;
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

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

    return ChangeNotifierProvider<BaseListProvider<Records>>(
      create: (_) => provider,
      child: Scaffold(
        appBar: SearchAppBar2(
          onPressed: (text) {
            searchWord = text;
            _onRefresh();
          },
        ),
        body: Stack(
          children: [
            Column(
              children: <Widget>[
                Container(
                  width: width,
                  height: 50,
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: CustomFilterHeader(
                          activeIndex: activeFilterIndex,
                          filterNameList: hotBrandList
                              ?.map<String>((e) => e.name)
                              ?.toList(),
                          onTap: (index) {
                            setState(() {
                              activeFilterIndex = index;
                              showAllBrands = false;
                            });

                            if (activeFilterIndex == 0) {
                              selectedBrandId = null;
                            } else {
                              selectedBrandId = hotBrandList[index - 1].id;
                            }

                            _onRefresh();
                          },
                        ),
                      ),
                      GestureDetector(
                        onTap: () {
                          getBrandList();
                          setState(() {
                            showAllBrands = !showAllBrands;
                          });
                        },
                        child: Container(
                          padding: EdgeInsets.only(top: 3),
                          child: Icon(
                            showAllBrands
                                ? Icons.keyboard_arrow_up
                                : Icons.keyboard_arrow_down,
                            size: 26.0,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Divider(
                  thickness: 5,
                  height: 5,
                  color: Color(0xffF9F9F9),
                ),
                Expanded(
                  flex: 1,
                  child: Consumer<BaseListProvider<Records>>(
                    builder: (_, provider, __) {
                      return MyListView(
                        key: Key('product_list'),
                        itemCount: provider.list.length,
                        stateType: provider.stateType,
                        onRefresh: _onRefresh,
                        pageSize: 10,
                        loadMore: _loadMore,
                        hasMore: provider.hasMore,
                        padding: EdgeInsets.symmetric(horizontal: 10),
                        itemBuilder: (_, index) {
                          return ProductCell(provider.list[index]);
                        },
                      );
                    },
                  ),
                ),
              ],
            ),
            if (showAllBrands)
              Positioned.fill(
                top: 50,
                left: 0,
                // width: width,
                // height: height - 50,
                child: brandList != null ? Container(
                  color: Colors.white,
                  child: BrandSelectionPanel(
                    brandList: brandList,
                    onTapBrand: (records) {
                      setState(() {
                        showAllBrands = false;
                        selectedBrandId = records.id;
                        activeFilterIndex = null;
                        _onRefresh();
                      });
                    },
                  ),
                ) : CircularLoading(),
              ),
          ],
        ),
      ),
    );
  }

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

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

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

