import 'dart:async';

import 'package:stripe_sdk/stripe_sdk_ui.dart';

class Validators {

  final validarEmail = StreamTransformer<String, String>.fromHandlers(
    handleData: ( email, sink ) {

      Pattern pattern = r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
      RegExp regExp   = new RegExp(pattern);

      if ( regExp.hasMatch( email ) ) {
        sink.add( email );
      } else {
        sink.addError('Email no es correcto');
      }

    }
  );


  final validarPassword = StreamTransformer<String, String>.fromHandlers(
    handleData: ( password, sink ) {

      if ( password.length >= 6 ) {
        sink.add( password );
      } else {
        sink.addError('Más de 6 caracteres por favor');
      }

    }
  );


  final validarTelefonoSpain = StreamTransformer<String, String>.fromHandlers(
    handleData: (String phone, sink) {
      if(phone.length == 9) {
        Pattern pattern = r'^[679]{1}[0-9]{8}$';
        RegExp regExp = new RegExp(pattern);
        if(regExp.hasMatch(phone)) sink.add(phone);
        else sink.addError("Teléfono no válido");
      }
      else sink.addError("Un número de teléfono es de 9 carácteres.");
    }
  );

  final validarNumeroPositivo = StreamTransformer<double, double>.fromHandlers(
    handleData: (double val, sink) {
      if(val!=null && val>=0) {
        sink.add(val);
      }
      else sink.addError("El número debe ser positivo");
    }
  );

  final validarNotEmpty = StreamTransformer<String, String>.fromHandlers(
    handleData: (String val, sink) {
      if(val!=null && val.length>0) sink.add(val);
      else sink.addError("No puede estar vacío.");
    }
  );

}