#!/usr/bin/env python3

from cerberus import Validator
from validation_functions import *


def validate(entity):
    schema_fin = {
        'bic': {
            'type': 'string',
            'required': True,
            'check_with': valid_bic},
        'iban': {
            'type': 'string',
            'required': True,
            'check_with': valid_iban},
        'issuance': {
            'type': 'string',
            'required': True,
            'check_with': iso_date},
        'reference': {'type': 'string'},
        'scan-sepa-mandate': {'type': 'string'},
        'holder': {'type': 'string'}}

    schema_membership = {
        'bis': {
            'type': 'string',
            'oneof': [{'check_with': iso_date}, {'empty': True}]},
        'mitgliedsbeitrag': {
            'type': 'string',
            'check_with': valid_money_amount},
        'scan-antrag': {'type': 'string'},
        'schliessberechtigung': {
            'type': 'string',
            'allowed': ['Ja', 'Nein', 'J', 'N', 'j', 'n', 'y', 'Y']},
        'spendenbeitrag': {
            'type': 'string',
            'check_with': valid_money_amount},
        'status': {
            'type': 'string',
            'required': True,
            'allowed': ['V', 'E', 'F']},
        'von': {
            'type': 'string',
            'required': True,
            'check_with': iso_date}}

    schema_base = {
        'address_code': {
            'type': 'string'},
        'address_country': {
            'type': 'string'},
        'address_label': {
            'type': 'string'},
        'address_locality': {
            'type': 'string'},
        'address_region': {
            'type': 'string'},
        'address_street': {
            'type': 'string'},
        'birth_date': {
            'type': 'string',
            'required': True,
            'check_with': iso_date},
        'birth_location': {
            'type': 'string'},
        'email': {
            'type': 'string',
            'required': True,
            'check_with': valid_email},
        'fullname': {
            'type': 'string',
            'required': True},
        'nickname': {
            'type': 'string'},
        'pgp-key': {
            'type': 'string'},
        'ssh-key': {
            'type': 'string'}}

    schema = {
        'finanzdaten': {
            'type': 'dict',
            'required': True,
            'schema': schema_fin},
        'mitgliederdaten': {
            'type': 'dict',
            'required': True,
            'schema': schema_membership},
        'stammdaten': {
            'type': 'dict',
            'required': True,
            'schema': schema_base},
        'id': {
            'type': 'string',
            'required': True,
            'regex': '^[a-f0-9]{5}$'},
        'timestamp': {
            'type': 'string',
            'required': True}
    }

    v = Validator()
    result = {
        'valid': v.validate(entity, schema),
        'errors': v.errors}
    return result