

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

class BoxCalendarPageBloc {

  ApiProvider _api;

  //Training Sessions
  BehaviorSubject<List<BITrainingSession>> _trainingSessionsSubject = new BehaviorSubject();
  Stream<List<BITrainingSession>> get trainingSessionStream => _trainingSessionsSubject.stream;

  //constructor + singleton
  static BoxCalendarPageBloc _instancia;

  factory BoxCalendarPageBloc({BuildContext context, int box_id, DateTime day }) {
    if(_instancia==null) {
      _instancia = BoxCalendarPageBloc._internal(context, box_id, day);
    }
    return _instancia;
  }

  BoxCalendarPageBloc._internal(BuildContext context, int box_id, DateTime day) {
    _api = Provider.of<ApiProvider>(context);
    this.refreshTrainingSessions(box_id, day);
  }

  //training session
  Future<bool> refreshTrainingSessions(int box_id, DateTime day) async {
    this._trainingSessionsSubject.sink.add(null);
    var resp = await _api.getTrainingSessionsBI(box_id, DateFormat('yyyy-MM-dd').format(day));
    this._trainingSessionsSubject.sink.add(resp.data);
    return true;
  }

}