import 'dart:async';

import 'package:zhiying_base_widget/pages/main_page/model/background_model.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'dart:convert' as Con;

class MainPageBloc extends BlocBase {
  List<Map<String, dynamic>> _pageData = List();

  StreamController<List<Map<String, dynamic>>> _mainController =
      StreamController<List<Map<String, dynamic>>>.broadcast();

  Stream<List<Map<String, dynamic>>> get outData => _mainController.stream;

  BackgroundModel backgroundModel;

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

  void loadData(String skipIdentifier) {
    print("加载mainPage"+skipIdentifier);
    NetUtil.request('/api/v1/mod/${skipIdentifier}', method: NetMethod.GET,
        onCache: (data) {
      _loadData(data);
    }, onSuccess: (data) {
      _loadData(data);
    });
  }

  void _loadData(dynamic data) {
    Map<String, dynamic> json = Map<String, dynamic>.from(data);

    List<dynamic> list = json['mod_list'];
    _pageData = list.map((item) {
      return Map<String, dynamic>.from(item);
    }).toList();
    try{
      backgroundModel=BackgroundModel.fromJson(Con.json.decode(json['out_data']));
    }catch(e,s){
      print(e);
      print(s);
    }

    _mainController.add(_pageData);
  }
}
