

import 'dart:async';

import 'package:app_hit_a_wod/src/blocs/validators.dart';
import 'package:app_hit_a_wod/src/models/user.model.dart';
import 'package:rxdart/subjects.dart';

class UserEditFormBloc with Validators {

  int _id;
  int get id => _id;

  BehaviorSubject<String> _nameSubject = new BehaviorSubject();
  Stream<String> get nameStream => _nameSubject.stream.transform(validarNotEmpty);
  String get nameValue => _nameSubject.value;
  Function(String) get setName => _nameSubject.sink.add;

  BehaviorSubject<String> _surnamesSubject = new BehaviorSubject();
  Stream<String> get surnamesStream => _surnamesSubject.stream;
  String get surnamesValue => _surnamesSubject.value;
  Function(String) get setSurnames => _surnamesSubject.sink.add;

  BehaviorSubject<String> _imageProfileUrlSubject = new BehaviorSubject();
  Stream<String> get imageProfileUrlStream => _imageProfileUrlSubject.stream;
  String get imageProfileUrlValue => _imageProfileUrlSubject.value;
  Function(String) get setImageProfileUrl => _imageProfileUrlSubject.sink.add;

  BehaviorSubject<String> _phoneSubject = new BehaviorSubject();
  Stream<String> get phoneStream => _phoneSubject.stream.transform(validarNotEmpty).transform( validarTelefonoSpain );
  String get phoneValue => _phoneSubject.value;
  Function(String) get setPhone => _phoneSubject.sink.add;

  BehaviorSubject<String> _emailSubject = new BehaviorSubject();
  Stream<String> get emailStream => _emailSubject.stream.transform(validarNotEmpty).transform( validarEmail );
  String get emailValue => _emailSubject.value;
  Function(String) get setEmail => _emailSubject.sink.add;

  BehaviorSubject<String> _nifSubject = new BehaviorSubject();
  Stream<String> get nifStream => _nifSubject.stream.transform(validarNotEmpty);
  String get nifValue => _nifSubject.value;
  Function(String) get setNif => _nifSubject.sink.add;

  BehaviorSubject<DateTime> _dateOfBirthSubject = new BehaviorSubject();
  Stream<DateTime> get dateOfBirthStream => _dateOfBirthSubject.stream;
  DateTime get dateOfBirthValue => _dateOfBirthSubject.value;
  Function(DateTime) get setDateOfBirth => _dateOfBirthSubject.sink.add;

  BehaviorSubject<String> _addressSubject = new BehaviorSubject();
  Stream<String> get addressStream => _addressSubject.stream.transform(validarNotEmpty);
  String get addressValue => _addressSubject.value;
  Function(String) get setAddress => _addressSubject.sink.add;

  BehaviorSubject<String> _zipcodeSubject = new BehaviorSubject();
  Stream<String> get zipcodeStream => _zipcodeSubject.stream.transform(validarNotEmpty);
  String get zipcodeValue => _zipcodeSubject.value;
  Function(String) get setZipcode => _zipcodeSubject.sink.add;

  BehaviorSubject<String> _poblacioSubject = new BehaviorSubject();
  Stream<String> get poblacioStream => _poblacioSubject.stream.transform(validarNotEmpty);
  String get poblacioValue => _poblacioSubject.value;
  Function(String) get setPoblacio => _poblacioSubject.sink.add;

  BehaviorSubject<String> _countrySubject = new BehaviorSubject();
  Stream<String> get countryStream => _countrySubject.stream.transform(validarNotEmpty);
  String get countryValue => _countrySubject.value;
  Function(String) get setCountry => _countrySubject.sink.add;

  BehaviorSubject<String> _passwordSubject = new BehaviorSubject();
  Stream<String> get passwordStream => _passwordSubject.stream.transform(validarPassword);
  String get passwordValue => _passwordSubject.value;
  Function(String) get setPassword => _passwordSubject.sink.add;

  BehaviorSubject<String> _repeatPasswordSubject = new BehaviorSubject();
  Stream<String> get repeatPasswordStream => _repeatPasswordSubject.stream.transform(StreamTransformer<String, String>.fromHandlers( handleData: ( password, sink ) {
      if ( password == passwordValue ) sink.add( password );
      else sink.addError('Las contraseñas no coinciden.');
  }));
  String get repeatPasswordValue => _repeatPasswordSubject.value;
  Function(String) get setRepeatPassword => _repeatPasswordSubject.sink.add;

  BehaviorSubject<double> _weightSubject = new BehaviorSubject();
  Stream<double> get weightStream => _weightSubject.stream.transform(validarNumeroPositivo);
  double get weightValue => _weightSubject.value;
  Function(double) get setWeight => _weightSubject.sink.add;

  BehaviorSubject<double> _heightSubject = new BehaviorSubject();
  Stream<double> get heightStream => _heightSubject.stream.transform(validarNumeroPositivo);
  double get heightValue => _heightSubject.value;
  Function(double) get setHeight => _heightSubject.sink.add;

  BehaviorSubject<String> _genderSubject = new BehaviorSubject();
  Stream<String> get genderStream => _genderSubject.stream;
  String get genderValue => _genderSubject.value;
  Function(String) get setGender => _genderSubject.sink.add;

  static UserEditFormBloc _instancia;
  factory UserEditFormBloc({User user}) {
    if(_instancia==null) {
      _instancia = UserEditFormBloc._internal(user);
    }
    return _instancia;
  }
  UserEditFormBloc._internal(User user) {
    _id = user.id;
    setName(user.name);
    setSurnames(user.surnames);
    setPhone(user.phone);
    setEmail(user.email);
    setNif(user.nif);
    setImageProfileUrl(user.url_profile_photo);
    setDateOfBirth(user.date_of_birth);
    setAddress(user.address);
    setZipcode(user.zipcode);
    setPoblacio(user.poblacio);
    setCountry(user.country);
    //password  no
    setWeight(user.weight);
    setHeight(user.height);
    setGender(user.gender);
  }

  User getUser() {
    User user = new User();
    user.id = _id;
    user.name = nameValue;
    user.surnames = surnamesValue;
    user.email = emailValue;
    user.phone = phoneValue;
    user.nif = nifValue;
    user.url_profile_photo = imageProfileUrlValue;
    user.date_of_birth = dateOfBirthValue;
    user.address = addressValue;
    user.zipcode = zipcodeValue;
    user.poblacio = poblacioValue;
    user.country = countryValue;
    user.weight = weightValue;
    user.height = heightValue;
    user.gender = genderValue;
    return user;
  } 

}