import os

from base.field import *
from jsonparser.documents import BaseJsonDocument
from jsonparser.encoder import EnumEncoder
import json


class Person(BaseJsonDocument):
    Name = StringField()
    Inn = StringField()
    AccountNumber = StringField()
    BankCode = StringField()
    BankName = StringField()


class Item(BaseJsonDocument):
    EntryDate = StringField()
    EntryDocumentNumber = StringField()
    EntryAccountNumber = StringField()
    EntryAmountDebit = PyFloatField(required=True)
    EntryAmountDebitBase = PyFloatField()
    EntryAmountCredit = PyFloatField()
    EntryAmountCreditBase = PyFloatField()
    EntryAmountBase = PyFloatField()
    EntryComment = StringField()
    EntryDepartment = StringField()
    EntryAccountPoint = StringField()
    DocumentProductGroup = StringField()
    DocumentValueDate = StringField()
    SenderDetails = Person()
    BeneficiaryDetails = Person()
    DocumentTreasuryCode = StringField()
    DocumentNomination = StringField()
    DocumentInformation = StringField()
    DocumentSourceAmount = PyFloatField()
    DocumentSourceCurrency = StringField()
    DocumentDestinationAmount = PyFloatField()
    DocumentDestinationCurrency = StringField()
    DocumentReceiveDate = StringField()
    DocumentBranch = StringField()
    DocumentDepartment = StringField()
    DocumentActualDate = StringField()
    DocumentExpiryDate = StringField()
    DocumentRateLimit = StringField()
    DocumentRate = PyFloatField()
    DocumentRegistrationRate = PyFloatField()
    DocumentSenderInstitution = StringField()
    DocumentIntermediaryInstitution = StringField()
    DocumentBeneficiaryInstitution = StringField()
    DocumentPayee = StringField()
    DocumentCorrespondentAccountNumber = StringField()
    DocumentCorrespondentBankCode = StringField()
    DocumentCorrespondentBankName = StringField()
    DocumentKey = PyFloatField()
    EntryId = PyFloatField()


class RootData(BaseJsonDocument):
    items = ListField(Item())


def run_example():
    dir_name = os.path.dirname(os.path.realpath(__file__))
    file_path = "{0}/json/bog".format(dir_name)
    with open('{0}.json'.format(file_path), 'r') as test_data_file:
        a = RootData()
        a.load(test_data_file.read().replace('\n', ''))
        if a.is_valid():
            with open("{0}_result.json".format(file_path), "w") as f:
                print(a.dump(), file=f)
        else:
            with open("{0}_errors.json".format(file_path), "w") as f:
                json.dump(a.errors, f, cls=EnumEncoder)
