import 'dart:convert';

import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/bloc/home_goods_header_bloc.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_header.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'dart:convert' as convert;

import 'models/home_goods_style_model.dart';

class GoodsListCreater extends WidgetCreater {
  @override
  List<Widget> createWidgets(Map<String, dynamic> model) {
    print('创建商品列表');
    // final EventBus _eventBus = EventBus();
    String d = model['data'];
    dynamic jsonData = convert.jsonDecode(d);
    var _style = HomeGoodsStyleModel.fromJson(Map<String, dynamic>.from(jsonData));
    final EventBus _eventBus = model['eventBus'];
    Widget _widget = BlocProvider<HomeGoodsHeaderBloc>(bloc: HomeGoodsHeaderBloc(), child: HomeGoodsHeader(model, _eventBus));
    Map<String, dynamic> json = jsonDecode(model['data']);

    List<Widget> listWidget = List();
    listWidget.add(SliverToBoxAdapter(child: Container(margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(null != json ? json['top_margin'] : '0')))));

    double height = 64.0;

    ///是否添加头部
    if (_style.recommendList.length < 2) {
      height = 0;
    }
    listWidget.add(SliverPersistentHeader(pinned: true, floating: false, delegate: HomeGoodsHeaderDelegate(_widget, height: height)));

    listWidget.add(HomeGoods(model, eventBus: _eventBus));
    return listWidget;
  }

  @override
  bool isSliverChild() {
    return true;
  }
}

class HomeGoodsHeaderEvent {
  dynamic provider;

  HomeGoodsHeaderEvent(this.provider);
}
