import 'dart:async';

import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GoodsShareData {
  List<Map<String, dynamic>> tabs;
}

class GoodsShareBloc extends BlocBase {
  GoodsShareData _data = GoodsShareData();

  StreamController<GoodsShareData> _tabController =
      StreamController<GoodsShareData>();

  Stream<GoodsShareData> get outData => _tabController.stream;

  @override
  void dispose() {
    _tabController.close();
    _tabController = null;
  }

  void loadData(String shopType, Map<String, dynamic> params) async {
    await loadMod(shopType);
    _tabController.add(_data);
  }

  Future loadMod(String shopType) async {
    var data = await NetUtil.post(
        '/api/v1/tmp/mod/pub.flutter.share?type=${shopType.toString()}',
        method: NetMethod.GET);
    _loadMod(data['data']);
  }

  void _loadMod(dynamic data) {
    try {
      List list = Map<String, dynamic>.from(data)['mod_list'];
      for (dynamic model in list) {
        Map d = Map<String, dynamic>.from(model);
        if (d.containsKey('tab_view')) {
          List tabs = d['tab_view'];
          _data.tabs = tabs.map((item) {
            return Map<String, dynamic>.from(item);
          }).toList();
        }
      }
    } catch (err) {
      Logger.error(err);
    }
  }
}
