

import 'package:app_hit_a_wod/src/models/training_workout_line.model.dart';
import 'package:app_hit_a_wod/src/models/user_benchmark_line.model.dart';
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';

class UserBenchmarkLineCreateBloc {

  TrainingWorkoutLine _workoutLine;
  TrainingWorkoutLine get workoutLine => _workoutLine;

  BehaviorSubject<String> _valSubject = new BehaviorSubject();
  Stream<String> get valStream => _valSubject.stream;
  String get valValue => _valSubject.value;
  Function(String) get setVal => _valSubject.sink.add;

  BehaviorSubject<String> _repsSubject = new BehaviorSubject();
  Stream<String> get repsStream => _repsSubject.stream;
  String get repsValue => _repsSubject.value;
  Function(String) get setReps => _repsSubject.sink.add;

  BehaviorSubject<int> _trainingWorkoutLineIdSubject = new BehaviorSubject();

  BehaviorSubject<String> _unitsSubject = new BehaviorSubject();
  Stream<String> get unitsStream => _unitsSubject.stream;
  String get unitsValue => _unitsSubject.value;
  Function(String) get unitsVal => _unitsSubject.sink.add;

  UserBenchmarkLineCreateBloc(UserBenchmarkLine line, TrainingWorkoutLine this._workoutLine) {
    this._trainingWorkoutLineIdSubject.sink.add(line.training_workout_line_id);
    setLine(line);
  }

  //Metodes transformadors
  UserBenchmarkLine getLine() {
    UserBenchmarkLine line = UserBenchmarkLine();
    line.reps = _repsSubject.value;
    line.val = _valSubject.value;
    line.units = _unitsSubject.value;
    line.training_workout_line_id = _trainingWorkoutLineIdSubject.value;
    return line;
  }

  void setLine(UserBenchmarkLine line) {
    this._valSubject.sink.add(line.val);
    this._repsSubject.sink.add(line.reps);
    this._unitsSubject.sink.add(null);
  }

  void setMode(int lvl, String gender) {
    if (lvl==0) setReps(_workoutLine.reps!=null ? _workoutLine.reps : null);
    else if (lvl==1) setReps(_workoutLine.reps_scaled!=null ? _workoutLine.reps_scaled : null);
    else if (lvl==2) setReps(_workoutLine.reps!=null ? _workoutLine.reps : "0");
    if (gender!='F') {
      if (lvl==0) setVal(_workoutLine.maleVal!=null ? _workoutLine.maleVal : null);
      else if (lvl==1) setVal(_workoutLine.maleVal_scaled!=null ? _workoutLine.maleVal_scaled : null);
      else if (lvl==2) setVal(_workoutLine.maleVal!=null ? _workoutLine.maleVal : "0");
    } else {
      if (lvl==0) setVal(_workoutLine.femaleVal!=null ? _workoutLine.femaleVal : null);
      else if (lvl==1) setVal(_workoutLine.femaleVal_scaled!=null ? _workoutLine.femaleVal_scaled : null);
      else if (lvl==2) setVal(_workoutLine.femaleVal!=null ? _workoutLine.femaleVal : "0");
    }

    if(lvl==0 || lvl==2) _unitsSubject.sink.add(_workoutLine.units);
    else if(lvl==1) _unitsSubject.sink.add(_workoutLine.units_scaled);
  }

}