import 'dart:async';

import 'package:zhiying_base_widget/pages/goods_share_page/models/goods_share_poster_model.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/models/goods_share_temp_add_model.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GoodsShareContentBloc extends BlocBase {
  GoodsShareTempDataModel _data = GoodsShareTempDataModel();
  GoodsSharePosterModel _poster;

  StreamController<GoodsShareTempDataModel> _dataController =
  StreamController<GoodsShareTempDataModel>();

  StreamController<GoodsSharePosterModel> _posterController =
  StreamController<GoodsSharePosterModel>();

  Stream<GoodsShareTempDataModel> get outData => _dataController.stream;
  Stream<GoodsSharePosterModel> get outPoster => _posterController.stream;

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

  Future loadData(String shareType, Map<String, dynamic> params) async {
    if (shareType == 'image') {
      _loadPoster(params);
    }

      var json = await NetUtil.post('/api/v1/share?type=${shareType.toString()}',
          params: params);
      Map<String, dynamic> data = json['data'];

      _data = GoodsShareTempDataModel.fromJson(Map<String, dynamic>.from(data));

      _dataController.add(_data);
  }

  // 请求分享海报
  void _loadPoster(Map<String, dynamic> params) async {
    var json = await NetUtil.post('/api/v1/share?type=poster', params: params);
    Map<String, dynamic> data = Map<String, dynamic>.from(json['data']);

    _poster = GoodsSharePosterModel.fromJson(data);
    _posterController.add(_poster);
  }
}
