import 'dart:convert' as convert;

import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/goods_details/recommend/goods_detail_commend_list.dart';
import 'package:zhiying_base_widget/widgets/goods_details/recommend/goods_detail_commend_widget.dart';
import 'package:zhiying_base_widget/widgets/goods_details/recommend/models/goods_detail_recommend_model.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_header.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GoodsDetailCommendCreater extends WidgetCreater {
  // final EventBus _eventBus = EventBus();

  @override
  List<Widget> createWidgets(Map<String, dynamic> model) {
    // String d = model['data'];
    // Map<String, dynamic> json = convert.jsonDecode(d);
    // GoodsDetailRecommendModel style = GoodsDetailRecommendModel.fromJson(json);

    // print('创建商品列表');
    // Widget _widget = BlocProvider<HomeGoodsHeaderBloc>(
    //   bloc: HomeGoodsHeaderBloc(),
    //   child: HomeGoodsHeader(model, _eventBus),
    // );

    String provider = GlobalConfig.PROVIDER_TB;
    Map<String, dynamic> json = convert.jsonDecode(model['data']);
    bool haveProvider = false;
    try{
      haveProvider = json.containsKey('provider') && !EmptyUtil.isEmpty(json['provider']);
      if(haveProvider){
        provider = json['provider'];
      }
    }catch(e, s){
      provider = GlobalConfig.PROVIDER_TB;
      Logger.error(e,s);
    }

    try {
      if(!haveProvider) {
        var listStyle = json['list_style'];
        provider = listStyle['product_type']?.toString() ?? GlobalConfig.PROVIDER_TB;
      }
    } catch (e, s) {
      provider = GlobalConfig.PROVIDER_TB;
      Logger.error(e, s);
    }
    Logger.warn(model?.toString());

    return [
      SliverToBoxAdapter(
        child: GoodsDetailsRecommendWidget(model),
      ),
      GoodsDetailCommendList(
        model,
        provider: provider, // 猜你喜欢数据源暂无数据
      ),
    ];
  }

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