import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:zhiying_base_widget/pages/hot_ranking_page/hot_ranking_page_bloc.dart';
import 'package:zhiying_base_widget/widgets/hot_ranking/hot_ranking_goods/hot_ranking_goods.dart';
import 'package:zhiying_base_widget/widgets/hot_ranking/hot_ranking_list/model/hot_ranking_list_data_model.dart';
import 'package:zhiying_base_widget/widgets/hot_ranking/hot_ranking_list/model/hot_ranking_list_model.dart';
import 'package:zhiying_base_widget/widgets/refresh/refresh_footer/refresh_footer.dart';
import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_header.dart';
import 'package:zhiying_comm/util/base_bloc.dart';

import 'hot_ranking_bloc.dart';
import 'hot_ranking_list_sk.dart';

class HotRankingList extends StatefulWidget {
  final Map<String, dynamic> data;

  const HotRankingList(this.data, {Key key}) : super(key: key);

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

class _HotRankingState extends State<HotRankingList>
    with TickerProviderStateMixin {
  HotRankingListBloc _bloc;
  TabController _tabController;
  RefreshController _refreshController;
  List<Good> goods;

  HotRankingPageBloc _pageBloc;

  ScrollController _scrollcontroller;

  @override
  void initState() {
    _bloc = HotRankingListBloc();
    _refreshController = RefreshController();
    _pageBloc = BlocProvider.of<HotRankingPageBloc>(context);
    _scrollcontroller = ScrollController();

    if (_pageBloc != null) {
      _pageBloc.event.listen((event) {
        if (event.containsKey('type') && event['type'] == "loadData") {
          _bloc.currentPage = 1;
          _bloc.loadData(event['type_id'], _bloc.currentPage, () {});
        }
      });
    }

    super.initState();
  }

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

  ///构建
  void createTabController() {
    _tabController = TabController(length: 4, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<HotRankingListDataModel>(
        stream: _bloc.outData,
        builder: (context, snapshot) {
          if (snapshot.data == null) {
            return HotRankingSkeleton();
          } else {
            goods = snapshot.data.good;
            if (_bloc.currentPage == 1) {
              Future.delayed(Duration(milliseconds: 100), () {
                _scrollcontroller.animateTo(0,
                    duration: Duration(milliseconds: 100), curve: Curves.ease);
              });
            }
            return SmartRefresher(
              controller: _refreshController,
              enablePullDown: true,
              enablePullUp: true,
              onLoading: _onLoading,
              onRefresh: _onRefresh,
              header: RefreshHeader(),
              footer: RefreshFooter(),
              child: CustomScrollView(
                controller: _scrollcontroller,
                slivers: <Widget>[
                  SliverList(
                      delegate: SliverChildBuilderDelegate(_buildListItem,
                          childCount: goods.length))
                ],
              ),
            );
          }
        });
  }

  Future _onLoading() async {
    _bloc.loadMoreData(_bloc.currentTypeId, () {
      _refreshController.loadComplete();
    });
    return null;
  }

  Future _onRefresh() async {
    _bloc.currentPage = 1;
    _bloc.loadData(_bloc.currentTypeId, _bloc.currentPage, () {
      _refreshController.refreshCompleted();
    });
  }

  Widget _buildListItem(BuildContext context, int index) {
    return HotRankingGoods(
      styleModel:
          HotRankingListModel.fromJson(json.decode(widget.data['data'])),
      good: goods[index],
      index: index,
    );
  }
}
