

import 'package:app_hit_a_wod/src/models/exercise_benchmark.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 ExerciseBloc {

  ApiProvider _api;

  //Benchmarks
  BehaviorSubject<ExerciseBenchmark> _benchmarksSubject = new BehaviorSubject();
  Stream<ExerciseBenchmark> get benchmarksStream => _benchmarksSubject.stream;

  //constructor + singleton
  static ExerciseBloc _instancia;

  factory ExerciseBloc({BuildContext context, int user_id, int exercise_id }) {
    if(_instancia==null) {
      _instancia = ExerciseBloc._internal(context, user_id, exercise_id);
    }
    return _instancia;
  }

  ExerciseBloc._internal(BuildContext context, int user_id, int exercise_id ) {
    _api = Provider.of<ApiProvider>(context);
    this.userBenchmarksExercise(user_id, exercise_id);
  }

  //training session
  Future<bool> userBenchmarksExercise(int user_id, int exercise_id) async {
    var resp = await _api.getExerciseBenchmarks(exercise_id, user_id);
    this._benchmarksSubject.sink.add(resp);
    return true;
  }

}