

import 'package:app_hit_a_wod/src/models/user_assistence.model.dart';
import 'package:app_hit_a_wod/src/providers/api.provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:rxdart/subjects.dart';

class UserPageBloc {

  ApiProvider _api;

  //Assistence
  BehaviorSubject<List<UserAssistence>> _assistenceSubject = new BehaviorSubject();
  Stream<List<UserAssistence>> get assistenceStream => _assistenceSubject.stream;

  //constructor + singleton
  static UserPageBloc _instancia;

  factory UserPageBloc({BuildContext context, int box_id, int user_id, Map<String, dynamic> params = const {} }) {
    if(_instancia==null) {
      _instancia = UserPageBloc._internal(context, box_id, user_id, params: params);
    }
    return _instancia;
  }

  UserPageBloc._internal(BuildContext context, int box_id, int user_id, { Map<String, dynamic> params = const {} }) {
    _api = Provider.of<ApiProvider>(context);
    this.refreshAssistence(box_id, user_id, params: params);
  }

  //assistence
  Future<bool> refreshAssistence(int box_id, int user_id, { Map<String, dynamic> params = const {} }) async {
    if(box_id==null) {
      this._assistenceSubject.sink.add([]);
      return false;
    }
    else {
      var resp = await _api.getBoxUserAssistance(box_id, user_id, params: params);
      this._assistenceSubject.sink.add(resp.data);
      return true;
    }
  }

}