

import 'package:app_hit_a_wod/src/models/bi_training_session.model.dart';
import 'package:app_hit_a_wod/src/models/user.model.dart';
import 'package:app_hit_a_wod/src/models/user_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 TrainingWorkoutPageBloc {

  ApiProvider _api;

  //Selected Lvl
  BehaviorSubject<int> _selectedLvlSubject = new BehaviorSubject();
  Stream<int> get selectedLvlStream => _selectedLvlSubject.stream;
  int get selectedLvlValue => _selectedLvlSubject.value;
  Function(int) get selectedLvlSet => _selectedLvlSubject.sink.add;

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

  //Favorite
  BehaviorSubject<bool> _favoriteSubject = new BehaviorSubject();
  Stream<bool> get favoriteStream => _favoriteSubject.stream;
  bool get favoriteValue => _favoriteSubject.value;
  Function(bool) get favoriteSet => _favoriteSubject.sink.add;


  //constructor + singleton
  static TrainingWorkoutPageBloc _instancia;

  factory TrainingWorkoutPageBloc({BuildContext context, int user_id, int workout_id, User user }) {
    _instancia = TrainingWorkoutPageBloc._internal(context, user_id, workout_id, user);
    return _instancia;
  }

  TrainingWorkoutPageBloc._internal(BuildContext context, int user_id, int workout_id, User user ) {
    _api = Provider.of<ApiProvider>(context);
    this._selectedLvlSubject.sink.add(0);
    this.userBenchmarksWorkout(user_id, workout_id);
    this._favoriteSubject.sink.add(user.favorite_workouts_ids.contains(workout_id));
  }

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

}