import 'dart:async';

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

import 'model/wallet_style_model.dart';

class WalletBilDetailWidgetBloc extends BlocBase {
  StreamController<List<Map<String, dynamic>>> inputDataController =
      StreamController.broadcast();

  Stream get inputOutData => inputDataController.stream;

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

  Stream get outputOutData => outputDataController.stream;

  List<Map<String, dynamic>> inputDataVM;
  List<Map<String, dynamic>> outputDataVM;

  ///样式model
  BilDetailStyleModel styleData;

  ///支出当前页数
  int outPutCurrentPage = 1;

  ///收入当前页数
  int inPutCurrentPage = 1;

  ///收入显示时间
  String inputShowDate;

  ///支出显示时间
  String outputShowDate;

  //收入显示类型
  String inputSelectType = 'all';

  ///支出显示类型
  String outputSelectType = 'all';

  ///收入下啦刷新
  RefreshController inputRefreshController = RefreshController();

  ///支出页面下拉刷新
  RefreshController outputRefreshController = RefreshController();

  @override
  void dispose() {
    inputDataController.close();
    outputDataController.close();
    inputRefreshController.dispose();
    outputRefreshController.dispose();
  }

  initStyleData(Map<String, dynamic> data) {
    try {
      inputShowDate = DateFormat("yyyy-MM").format(DateTime.now());
      outputShowDate = DateFormat("yyyy-MM").format(DateTime.now());
      styleData = BilDetailStyleModel.fromJson(data);
    } catch (e, s) {
      print(e);
      print(s);
    }
  }

  ///加载收入数据
  loadInputData(int page, String date, {String type}) async {
    type = inputSelectType;
    String paramsStr = "?page=" + page.toString() + "&time=" + date;
    if (type != null && type != "all") {
      paramsStr += "&type=" + type;
    }
    NetUtil.request("/api/v1/user/wallet_detail/input" + paramsStr,
        method: NetMethod.GET, onSuccess: (data) {
      inputRefreshController.refreshCompleted();
      inputRefreshController.loadComplete();
      var listmap = List.from(data).map((e) {
        return e as Map<String, dynamic>;
      }).toList();
      if (inputDataVM == null) {
        inputDataVM = List<Map<String, dynamic>>();
      }

      if (listmap == null || listmap.length == 0) {
        inputRefreshController.loadNoData();
      }

      if (inPutCurrentPage == 1) {
        inputDataVM.clear();
        inputDataVM.addAll(listmap);
      } else {
        inputDataVM.addAll(listmap);
      }

      refresh();
    });
  }

  ///加载支出数据
  loadOutputData(int page, String date, {String type}) async {
    type = outputSelectType;
    String paramsStr = "?page=" + page.toString() + "&time=" + date;
    if (type != null && type != "all") {
      paramsStr += "&type=" + type;
    }
    NetUtil.request("/api/v1/user/wallet_detail/output" + paramsStr,
        method: NetMethod.GET, onSuccess: (data) {
      outputRefreshController.refreshCompleted();
      outputRefreshController.loadComplete();

      var listMap = List.from(data).map((e) {
        return e as Map<String, dynamic>;
      }).toList();

      if (listMap == null || listMap.length == 0) {
        outputRefreshController.loadNoData();
      }
      if (outputDataVM == null) {
        outputDataVM = List();
      }
      if (outPutCurrentPage == 1) {
        outputDataVM.clear();
        outputDataVM.addAll(listMap);
      } else {
        outputDataVM.addAll(listMap);
      }
      refresh();
    }, onError: (e) {
      outputRefreshController.refreshCompleted();
      outputRefreshController.loadComplete();
    });
  }

  inputRefresh() {
    inPutCurrentPage = 1;
    loadInputData(inPutCurrentPage, inputShowDate);
  }

  inputLoad() {
    inPutCurrentPage++;
    loadInputData(inPutCurrentPage, inputShowDate);
  }

  outputRefresh() {
    outPutCurrentPage = 1;
    loadOutputData(outPutCurrentPage, outputShowDate);
  }

  outputLoad() {
    outPutCurrentPage++;
    loadOutputData(outPutCurrentPage, outputShowDate);
  }

  ///全刷新
  refresh() {
    inputDataController.add(inputDataVM);
    outputDataController.add(outputDataVM);
  }
}
