def time_sequence(ms):
    try:
        ref = get_reference('sequence_timing')
        ref.push({'ms': ms.total_seconds() * 1000})
        return True
    except Exception as e:
        print("Error ocurred on saving time = ", e)
        return False
def register_attempt(login_allowed, u_id):
    if login_allowed:
        path = 'attempts/success'
    else:
        path = 'attempts/failed'
    attempt_ref = get_reference(f'users/{u_id}/' + path)
    now = datetime.now()
    timestamp = datetime.timestamp(now)
    attempt_ref.push({'time': timestamp})
def save_results(json):
    status = True
    try:
        user_survey = get_reference(f'users/{json["userId"]}/survey')
        json.pop('userId', None)
        user_survey.set(json)
    except Exception as e:
        status = False
    return status
def create_user(args):
    if does_user_exist(args["email"]):
        return ""

    seed = generate_seed()
    secret = generate_secret_key(seed)
    user = {'email': args["email"], 'name': args["fullName"], 'secret': secret}
    u_id = uuid.uuid4()
    ref = get_reference(f'users/{u_id}')
    ref.set(user)
    return f'email:{args["email"]}?secret:{secret}?uid:{u_id}'
def upload_movement_on_user(data_obj, uid, success):
    if 'movement' not in data_obj or 'movement_data' not in data_obj:
        return False
    ref = get_reference(f'/users_data/{uid}/{data_obj["movement"]}')
    data = data_obj["movement_data"]
    now = datetime.now()
    timestamp = datetime.timestamp(now)
    pushed_ref = ref.push({
        'data': data,
        'timestamp': timestamp,
        'success': success
    })
    return True
def get_user_key(u_id):
    user = get_reference(f'users/{u_id}').get()
    if user is None: return None
    else:
        return user['secret']
def does_user_exist(email_to_check):
    users = get_reference('users').get()
    for attr, value in users.items():
        if value['email'] == email_to_check:
            return attr
    return False
def upload_attempt(shape, success):
    ref = get_reference(f'attempts_by_shape/{shape}')
    pushed_ref = ref.push(success)
def upload_movement(data_obj):
    ref = get_reference(f'get_password_data/')
    data = data_obj
    pushed_ref = ref.push(data)
    return True