

import 'package:app_hit_a_wod/src/blocs/create-benchmark/user_benchmark_line_create.bloc.dart';
import 'package:app_hit_a_wod/src/models/training_workout_section.model.dart';
import 'package:app_hit_a_wod/src/models/user_benchmark.model.dart';
import 'package:app_hit_a_wod/src/models/user_benchmark_line.model.dart';
import 'package:app_hit_a_wod/src/models/user_benchmark_section.model.dart';
import 'package:app_hit_a_wod/src/utils.dart';
import 'package:flutter/material.dart';
import 'package:rxdart/subjects.dart';

class UserBenchmarkSectionCreateBloc {

  TrainingWorkoutSection _tws;
  TrainingWorkoutSection get tws => _tws;

  BehaviorSubject<int> _userBenchmarkIdSubject = new BehaviorSubject();
  BehaviorSubject<int> _trainingWorkoutSectionIdSubject = new BehaviorSubject();

  BehaviorSubject<int> _completedSubject = new BehaviorSubject();
  Stream<int> get completedStream => _completedSubject.stream;
  int get completedValue => _completedSubject.value;
  Function(int) get setCompleted => _completedSubject.sink.add;

  BehaviorSubject<String> _timeSubject = new BehaviorSubject();
  Stream<String> get timeStream => _timeSubject.stream;
  String get timeValue => _timeSubject.value;
  Function(String) get setTime => _timeSubject.sink.add;

  BehaviorSubject<int> _roundsSubject = new BehaviorSubject();
  Stream<int> get roundsStream => _roundsSubject.stream;
  int get roundsValue => _roundsSubject.value;
  Function(int) get setRounds => _roundsSubject.sink.add;

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

  BehaviorSubject<int> _customScoreSubject = new BehaviorSubject();
  Stream<int> get customScoreStream => _customScoreSubject.stream;
  int get customScoreValue => _customScoreSubject.value;
  Function(int) get setCustomScore => _customScoreSubject.sink.add;

  //linies
  BehaviorSubject<List<UserBenchmarkLineCreateBloc>> _linesSubject = new BehaviorSubject();
  Stream<List<UserBenchmarkLineCreateBloc>> get linesStream => _linesSubject.stream;
  List<UserBenchmarkLineCreateBloc> get getLines => _linesSubject.value;
  Function(List<UserBenchmarkLineCreateBloc>) get _setLines => _linesSubject.sink.add; 

  UserBenchmarkSectionCreateBloc(UserBenchmarkSection ubs, TrainingWorkoutSection this._tws) {
    List<UserBenchmarkLineCreateBloc> lines = [];

    if(ubs.user_benchmark_lines.length != _tws.lines.length) throw Exception("Workout and Benchmark lines must be the same.");

    ubs.user_benchmark_lines.asMap().forEach((i, element) {
      lines.add(UserBenchmarkLineCreateBloc(element, _tws.lines[i]));
    });
    _setLines(lines);

    _userBenchmarkIdSubject.sink.add(ubs.user_benchmark_id);
    _trainingWorkoutSectionIdSubject.sink.add(ubs.training_workout_section_id);
    setSection(ubs);
  }

  UserBenchmarkSection getSection() {
    UserBenchmarkSection section = UserBenchmarkSection();
    section.completed = _completedSubject.value;
    section.custom_score = _customScoreSubject.value;
    section.reps = _repsSubject.value;
    section.rounds = _roundsSubject.value;
    section.time = _timeSubject.value;
    section.training_workout_section_id = _trainingWorkoutSectionIdSubject.value;
    section.user_benchmark_lines = _linesSubject.value.map((lineBloc) => lineBloc.getLine()).toList();
    return section;
  }

  void setSection(UserBenchmarkSection ubs) {
    setCompleted(ubs.completed);
    setTime(ubs.time);
    setRounds(ubs.rounds);
    setReps(ubs.reps);
    setCustomScore(ubs.custom_score);
  }

  void setMode(int lvl, String gender) {
    setTime(tws.seconds!=null ? UtilsHAW.dateToCronoTime(new DateTime.utc(2020,01,01,0,0,0,0,0).add(new Duration(seconds: tws.seconds))) : null);
    setCompleted(1);

    this.getLines.forEach((bloc) {
      bloc.setMode(lvl, gender);
    });
  }

}