import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:liftmanager/internal/bbs/bbs_router.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/video_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/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/theme_utils.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 VideoList extends StatefulWidget {
  @override
  VideoListState createState() => VideoListState();
}

class VideoListState
    extends BasePageState<VideoList, VideoListPresenterSeconds> {
  BaseListProvider<Records> provider = BaseListProvider<Records>();
  int _page = 1;
  int selectedBrandId;
  bool showAllBrands = false;
  String checkTypeName;
  String checkType;
  String searchWord;
  List<dynamic> typeList;
  List<dynamic> brandList;
  List<dynamic> hotBrandList;
  int activeFilterIndex = 0;

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

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

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

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return ChangeNotifierProvider<BaseListProvider<Records>>(
      create: (_) => provider,
      child: Scaffold(
        appBar: SearchAppBar2(
          searchWidth: width * 0.7,
          onPressed: (text) {
            searchWord = text;
            _onRefresh();
          },
          actions: <Widget>[
            FlatButton(
              child: Text(
                "上传",
                style: TextStyle(color: Color(0xff333333), fontSize: 15),
              ),
              textColor: Colours.text,
              highlightColor: Colors.transparent,
              onPressed: () {
                NavigatorUtils.push(context, BbsRouter.videoUpload);
              },
            )
          ],
        ),
        body: Container(
          child: Stack(
            children: <Widget>[
              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: () {
                            typeList = [];
                            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(
                    height: 5,
                    thickness: 5,
                    color: Color(0xffF9F9F9),
                  ),
                  Expanded(
                      flex: 1,
                      child: Consumer<BaseListProvider<Records>>(
                          builder: (_, provider, __) {
                        return MyListView(
                          key: Key('video_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 VideoCell(provider.list[index]);
                          },
                        );
                      }))
                ],
              ),
              if (showAllBrands)
                Positioned.fill(
                  top: 50,
                  left: 0,
                  // width: width,
                  // height: height - 50,
                  child: Container(
                    color: Colors.white,
                    child: BrandSelectionPanel(
                      brandList: brandList,
                      onTapBrand: (records) {
                        setState(() {
                          showAllBrands = false;
                          selectedBrandId = records.id;
                          activeFilterIndex = null;
                          _onRefresh();
                        });
                      },
                    ),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }

  Widget loadCircle() {
    return Container(
      padding: EdgeInsets.only(top: 10, bottom: 10),
      color: ThemeUtils.getTabsBg(context),
      child: Center(
        child: SpinKitFadingCircle(
          color: Colors.blueAccent,
          size: 30.0,
        ),
      ),
    );
  }

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

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

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