import 'dart:convert' as convert;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:zhiying_base_widget/pages/goods_details_page/notifier/goods_details_page_notifier.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/bloc/home_goods_bloc.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item_single.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_model.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_style_model.dart';
import 'package:zhiying_comm/util/base_bloc.dart';

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

  GoodsDetailCommendList(this.data, {Key key, this.provider}) : super(key: key);

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

class _GoodsDetailCommendListState extends State<GoodsDetailCommendList> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider<HomeGoodsBloc>(
      bloc: HomeGoodsBloc(),
      child: _GoodsDetailCommendListContainer(widget.data, widget.provider),
    );
  }
}

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

  _GoodsDetailCommendListContainer(this.data, this.provider, {Key key})
      : super(key: key);

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

class _GoodsDetailCommendListContainerState
    extends State<_GoodsDetailCommendListContainer> {
  HomeGoodsBloc _bloc;
  String _provider = '';
  HomeGoodsStyleModel _style;

  @override
  void initState() {
    _bloc = BlocProvider.of<HomeGoodsBloc>(context);

    String d = widget.data['data'];
    dynamic json = convert.jsonDecode(d);
    _style = HomeGoodsStyleModel.fromJson(Map<String, dynamic>.from(json));

    _bloc.loadMore(widget.provider);

    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('didChangeDependencies');
    bool isNeedLoadMore =
        Provider.of<GoodsDetailsPageNotifier>(context).scrollEnd;
    if (isNeedLoadMore && _provider != '') {
      print('HomeGoods loadmore...');
      _bloc.loadMore(_provider);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: StreamBuilder<List<HomeGoodsModel>>(
        stream: _bloc.outData,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.data == null) {
            return Container();
          }

          List<HomeGoodsModel> goods = snapshot.data;
          int column = 2;
          int count = (goods.length / column).ceil();
          return ListView.builder(
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              itemCount: count,
              itemBuilder: (context, index) {
                if (column == 1) {
                  return HomeGoodsItemSingle(
                    goods[index],
                    _style,
                    data: widget.data,
                  );
                } else {
                  // return Container(color: Colors.red, height: 126,margin: EdgeInsets.all(10),);
                  return Padding(
                    padding: const EdgeInsets.only(left: 5, right: 5),
                    child: Row(
                      children: List.generate(column, (c) {
                        int i = index * column + c;
                        return Expanded(
                          child: i < goods.length
                              ? HomeGoodsItem(
                                  goods[i],
                                  _style,
                                  data: widget.data,
                                )
                              : Container(),
                        );
                      }).toList(),
                    ),
                  );
                }
              });
        },
      ),
    );
  }
}
