import os
import subprocess
from datetime import date, datetime

from decimal import Decimal
from os.path import join
from pprint import pprint

import pytz
from django.conf import settings
from django.template.loader import get_template
from django.test import TestCase

from jbank.ecb import parse_euro_exchange_rates_xml
from jbank.helpers import validate_xml
from jbank.models import WsEdiConnection, WsEdiSoapCall
from jbank.parsers import parse_tiliote_statements_from_file, parse_svm_batches_from_file
from jbank.sepa import Pain001, Pain002, PAIN001_REMITTANCE_INFO_OCR, PAIN001_REMITTANCE_INFO_OCR_ISO
from jutil.format import format_xml
from jutil.validators import iban_bic


class Tests(TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_pain001(self):
        debtor_acc = 'FI4947300010416310'
        p = Pain001('201802071211XJANITEST', 'Vuokrahelppi', debtor_acc, iban_bic(debtor_acc), '020840699',
                    ['Koukkukankareentie 29', '20320 Turku'], 'FI')
        creditor_acc = 'FI8847304720017517'
        p.add_payment('201802071339A0001', 'Jani Kajala', creditor_acc, iban_bic(creditor_acc), Decimal('49.00'), 'vuokratilitys')
        p.add_payment('201802071339A0001', 'Jani Kajala', creditor_acc, iban_bic(creditor_acc), Decimal('49.00'), '302300', PAIN001_REMITTANCE_INFO_OCR)
        p.add_payment('201802071339A0001', 'Jani Kajala', creditor_acc, iban_bic(creditor_acc), Decimal('49.00'), 'RF92 1229', PAIN001_REMITTANCE_INFO_OCR_ISO)
        xml_str = format_xml(p.render_to_bytes().decode())
        # print(xml_str)

        filename = '/tmp/pain001.xml'
        with open(filename, 'wt') as fp:
            fp.write(xml_str)
            # print(filename, 'written')

        # /usr/bin/xmllint --format --pretty 1 --load-trace --debug --schema $1 $2
        res = subprocess.run([
            '/usr/bin/xmllint',
            '--noout',
            # '--format',
            # '--pretty', '1',
            # '--load-trace',
            # '--debug',
            '--schema',
            join(settings.BASE_DIR, 'data/pain001/pain.001.001.03.xsd'),
            filename,
        ])
        self.assertEqual(res.returncode, 0)

    def test_to(self):
        filename = join(settings.BASE_DIR, 'data/to/547404896.TO')
        statements = parse_tiliote_statements_from_file(filename)
        rec = statements[0]['records'][0]
        # pprint(rec)
        self.assertEqual(rec['amount'], Decimal('-1799.00'))
        self.assertEqual(rec['archive_identifier'], '180203473047IE5807')
        self.assertEqual(rec['paid_date'], date(2018, 2, 3))
        self.assertEqual(rec['sepa']['iban_account_number'], 'FI8847304720017517')

    def test_svm(self):
        filename = join(settings.BASE_DIR, 'data/svm/547392460.SVM')
        batches = parse_svm_batches_from_file(filename)
        recs = batches[0]['records']
        self.assertEqual(len(recs), 1)
        rec = recs[0]
        # pprint(rec)
        self.assertEqual(rec['amount'], Decimal('49.00'))
        self.assertEqual(rec['archive_identifier'], '02042588WWRV0212')
        self.assertEqual(rec['remittance_info'], '00000000000000013013')

    def test_xp(self):
        filename = join(settings.BASE_DIR, 'data/xp/547958656.XP')
        with open(filename, 'rb') as fp:
            file_content = fp.read()
        p = Pain002(file_content)
        self.assertEqual(p.original_msg_id, '201802071211XJANITEST')
        self.assertEqual(p.msg_id, 'V000000009726773')
        self.assertEqual(p.group_status, 'ACCP')
        self.assertEqual(p.is_accepted, True)

    def test_ecb_rates(self):
        filename = join(settings.BASE_DIR, 'data/ecb-rates-2019-08-15.xml')
        with open(filename, 'rt') as fp:
            content = fp.read()
        rates = parse_euro_exchange_rates_xml(content)
        # for record_date, currency, rate in rates[20:21]:
            # print(record_date, currency, rate)
        record_date, currency, rate = rates[20]
        self.assertEqual(record_date, date(2019, 8, 15))
        self.assertEqual(currency, 'HKD')
        self.assertEqual(rate, Decimal('8.744'))

    def normalize_soap_env(self, content: bytes) -> bytes:
        from lxml import etree  # local
        doc = etree.fromstring(content)
        doc.find('.//{http://www.w3.org/2000/09/xmldsig#}DigestValue').text = 'x'
        doc.find('.//{http://www.w3.org/2000/09/xmldsig#}Reference').attrib['URI'] = 'x'
        doc.find('.//{http://www.w3.org/2000/09/xmldsig#}SignatureValue').text = 'x'
        doc.find('.//{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Reference').attrib['URI'] = 'x'
        doc.find('.//{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}BinarySecurityToken').attrib['{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Id'] = 'x'
        doc.find('.//{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}BinarySecurityToken').text = 'x'
        doc.find('.//{http://schemas.xmlsoap.org/soap/envelope/}Body').attrib['{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Id'] = 'x'
        return etree.tostring(doc)

    def test_x509(self):
        from lxml import etree  # local
        from zeep.wsse import BinarySignature
        print('MEDIA_ROOT = {}'.format(settings.MEDIA_ROOT))
        ws = WsEdiConnection(name='test', sender_identifier='12319203', receiver_identifier='123192031', target_identifier='1', environment='TEST', soap_endpoint='http://localhost')
        ws.signing_cert_file.name = 'data/x509/cert.pem'
        ws.signing_key_file.name = 'data/x509/key.pem'
        ws.save()
        self.assertEqual(WsEdiConnection.objects.get_by_target_identifier('1').id, ws.id)
        app = open('data/x509/appreq.xml', 'rb').read()
        signed = ws.sign_application_request(app)
        ref_signed = b'<?xml version="1.0"?>\n<ApplicationRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://bxd.fi/xmldata/">\n    <CustomerId>061133</CustomerId>\n    <Timestamp>2012-02-20T08:50:59.4319012+01:00</Timestamp>\n    <Environment>TEST</Environment>\n    <FileReferences>\n    <FileReference>1202170046-1202171334</FileReference>\n    </FileReferences>\n    <SoftwareId>DBSEPAClient</SoftwareId>\n    <FileType>pain.002.001.02</FileType>\n    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">\n        <SignedInfo>\n          <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>\n          <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>\n          <Reference URI="">\n            <Transforms>\n              <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>\n            </Transforms>\n            <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>\n            <DigestValue>D4Bg9OJdQHgsXMQM2ecyoqNwCn4=</DigestValue>\n          </Reference>\n        </SignedInfo>\n        <SignatureValue>NwocqJ57CuLnQJaIA0Jm8JM2WlNOrNSM65Qt9nq69az4QMCgZBwRIGudZ+uMVWu0\n8TRriTHgrCTw5W50cVnhC/z+sw0mcNuYskXmGpr+4nUASYbcbT6EzktrHHkbhVTZ\nI4X4NdN8skU06TdgzF1Jvw/GzKENrm4l9hBLGbQyIaJppGmK1lN0g1suxnyvzgZK\ngFEEJZdFgFWcP+mCU+88FkcYOVjoX48rLtr0hSXyWRgUC9x3Seiw5GoS3toaY9dE\nmo6GREWUFEX9TjaH2sOy4WexiejlYisCyEGzBnyBGZ/Xi5EdoZSRoGrh60r+XDLX\n3Vu3vCJm5zD3SPX5fV9o/A==</SignatureValue>\n        <KeyInfo>\n          <X509Data>\n            <X509IssuerSerial>\n              <X509IssuerName>Issuer: {{ ws.signing_cert.issuer.rfc4514_string }}</X509IssuerName>\n              <X509SerialNumber>{{ ws.signing_cert.serial_number }}</X509SerialNumber>\n            </X509IssuerSerial>\n            <X509Certificate>MIIDVDCCAjygAwIBAgIUGhIGnbdNdnLHN2Gb3GCgUeSJjQYwDQYJKoZIhvcNAQEL\nBQAwVzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlRYMQ8wDQYDVQQHDAZEYWxsYXMx\nFTATBgNVBAoMDEthamFsYSBHcm91cDETMBEGA1UEAwwKa2FqYWxhLmNvbTAeFw0x\nOTEyMDMxNzU0NDFaFw0xOTEyMTMxNzU0NDFaMFcxCzAJBgNVBAYTAlVTMQswCQYD\nVQQIDAJUWDEPMA0GA1UEBwwGRGFsbGFzMRUwEwYDVQQKDAxLYWphbGEgR3JvdXAx\nEzARBgNVBAMMCmthamFsYS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\nAoIBAQDzpgdgMXiW9SOxnFPLdeFIltlCxH2rAz/nj7bLGK9ycVMMOW08M55jLhMK\nfOnyrHECWSP1aSKV6ZsNrRqm87JK8LEHgnIIDauqmJY4bEqB9nj7SzhAJWw5dJWN\nVNQto0csItaywXm0OjYRo4spDoKlyUbgXEcIouH4nWe91fvpvCfZiTHlfToTqKJy\nHtJ+kPi2+PSySs4+167g3A+v4YnoHgxTJD+q6KSimQQ0VGpqcV4Mt7+U3VV3Jb61\nSJNoanB31DLDugTjCOYm6PfT1/abQgPi7TOkizNa6HIMtMO/KFXC/6P+Bg05cQM9\n1Qq7iZR2zIg62AjC7z41xivdf8PdAgMBAAGjGDAWMBQGA1UdEQQNMAuCCWxvY2Fs\naG9zdDANBgkqhkiG9w0BAQsFAAOCAQEAAn4D/QY8pdvbPYx+yDeYPlHnYv68ErBK\n7Ib2rrtM7jtumBVL9BCneacjdsLmsrXwNdQkQMynxl6bMa+uR3YkXyQSVs+aSwKy\nIkz++rI5ALRI5KQr/DGzWrrmlIbBrXtQkLUR2mnyw9t+ozSMPtdedVCr50c88B5j\ndnksF6odkXet2gpVa5aZ8T2HUl+DtixkKoQ66Ra0/cXXdi3pk6zfRAm8/wtVIzQY\nX2+rur2NOPUuCzdWAvNjzuWCgmH8A2BxCOWBAMJ/GpsVJpqJp0tgm7ah1N+r1y0c\nRqxXZw7bu3NXedB1YqmOvYRcAGK2WXH4aV7I/rDRCc+aMs1GCOocbw==</X509Certificate>\n          </X509Data>\n        </KeyInfo>\n    </Signature>\n</ApplicationRequest>\n'
        self.assertEqual(signed, ref_signed)
        encoded = ws.encode_application_request(signed)
        ref_encoded = b'PEFwcGxpY2F0aW9uUmVxdWVzdCB4bWxuczp4c2k9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hLWluc3RhbmNlIiB4bWxuczp4c2Q9Imh0dHA6Ly93d3cudzM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RmOFBkQWdNQkFBR2pHREFXTUJRR0ExVWRFUVFOTUF1Q0NXeHZZMkZzCmFHOXpkREFOQmdrcWhraUc5dzBCQVFzRkFBT0NBUUVBQW40RC9RWThwZHZiUFl4K3lEZVlQbEhuWXY2OEVyQksKN0liMnJydE03anR1bUJWTDlCQ25lYWNqZHNMbXNyWHdOZFFrUU15bnhsNmJNYSt1UjNZa1h5UVNWcythU3dLeQpJa3orK3JJNUFMUkk1S1FyL0RHeldycm1sSWJCclh0UWtMVVIybW55dzl0K296U01QdGRlZFZDcjUwYzg4QjVqCmRua3NGNm9ka1hldDJncFZhNWFaOFQySFVsK0R0aXhrS29RNjZSYTAvY1hYZGkzcGs2emZSQW04L3d0Vkl6UVkKWDIrcnVyMk5PUFV1Q3pkV0F2Tmp6dVdDZ21IOEEyQnhDT1dCQU1KL0dwc1ZKcHFKcDB0Z203YWgxTityMXkwYwpScXhYWnc3YnUzTlhlZEIxWXFtT3ZZUmNBR0syV1hINGFWN0kvckRSQ2MrYU1zMUdDT29jYnc9PTwvWDUwOUNlcnRpZmljYXRlPgogICAgICAgICAgPC9YNTA5RGF0YT4KICAgICAgICA8L0tleUluZm8+CiAgICA8L1NpZ25hdHVyZT4KPC9BcHBsaWNhdGlvblJlcXVlc3Q+Cg=='
        self.assertEqual(encoded, ref_encoded)
        timestamp = pytz.timezone('Europe/Helsinki').localize(datetime(2015, 2, 3, 14, 30))
        soap_call = WsEdiSoapCall.objects.create(connection=ws, command='HelloWorld', created=timestamp)
        soap_body = get_template('jbank/soap_template.xml').render({'soap_call': soap_call, 'payload': encoded.decode()})
        body_bytes = soap_body.encode()
        envelope = etree.fromstring(body_bytes)
        binary_signature = BinarySignature(ws.signing_key_full_path, ws.signing_cert_full_path)
        soap_headers = {
        }
        envelope, soap_headers = binary_signature.apply(envelope, soap_headers)
        signed_body_bytes = etree.tostring(envelope)
        ref_bytes = b'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://model.bxd.fi" xmlns:ns2="http://bxd.fi/CorporateFileService">\n  <SOAP-ENV:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><Signature xmlns="http://www.w3.org/2000/09/xmldsig#">\n<SignedInfo>\n<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>\n<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>\n<Reference URI="#id-311224dc-3b4c-4729-b057-b3abb3bcd95d">\n<Transforms>\n<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>\n</Transforms>\n<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>\n<DigestValue>FGoo2ZirnTBuQ9N22TPlCO7eXaw=</DigestValue>\n</Reference>\n</SignedInfo>\n<SignatureValue>MNseos+F09uzlAH+1B1o01OBPslz80vJZWmi4yJ4NniZBXAD0rtPBRhbVwF4gSw1\nbLt1Eb6YKDl+nyRMb0f1H/QUuIibMXvlKu2IVYyTUktovW29oKkmXKfJtix9Eh+w\nzq4XEDZ3BjbkHW7FrS5ZZGRJB+IeePTHSZZ1JSxiBnOg5BQ0MlypUhdEaaHmJUM+\n7vSp/pttXc8vNdB/8pFCV0Yw/DrjANeNbFv2HXG0p1R1Dhmk2Tj3+cWDpWoFuWoj\nOg1y0eoTHW/5SSob9S9mEQPJi6aYr1ni89dF1I78o7kRDxK/JbyTNBq0YnLaXA2m\n6hJlwwfj9iYH3AkYe9YXsw==</SignatureValue>\n<KeyInfo>\n<wsse:SecurityTokenReference><wsse:Reference ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" URI="#id-e328e4ba-0ee6-4570-8476-89c88611e5ac"/></wsse:SecurityTokenReference></KeyInfo>\n</Signature><wsse:BinarySecurityToken xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ns1:Id="id-e328e4ba-0ee6-4570-8476-89c88611e5ac">MIIDVDCCAjygAwIBAgIUGhIGnbdNdnLHN2Gb3GCgUeSJjQYwDQYJKoZIhvcNAQEL\nBQAwVzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlRYMQ8wDQYDVQQHDAZEYWxsYXMx\nFTATBgNVBAoMDEthamFsYSBHcm91cDETMBEGA1UEAwwKa2FqYWxhLmNvbTAeFw0x\nOTEyMDMxNzU0NDFaFw0xOTEyMTMxNzU0NDFaMFcxCzAJBgNVBAYTAlVTMQswCQYD\nVQQIDAJUWDEPMA0GA1UEBwwGRGFsbGFzMRUwEwYDVQQKDAxLYWphbGEgR3JvdXAx\nEzARBgNVBAMMCmthamFsYS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\nAoIBAQDzpgdgMXiW9SOxnFPLdeFIltlCxH2rAz/nj7bLGK9ycVMMOW08M55jLhMK\nfOnyrHECWSP1aSKV6ZsNrRqm87JK8LEHgnIIDauqmJY4bEqB9nj7SzhAJWw5dJWN\nVNQto0csItaywXm0OjYRo4spDoKlyUbgXEcIouH4nWe91fvpvCfZiTHlfToTqKJy\nHtJ+kPi2+PSySs4+167g3A+v4YnoHgxTJD+q6KSimQQ0VGpqcV4Mt7+U3VV3Jb61\nSJNoanB31DLDugTjCOYm6PfT1/abQgPi7TOkizNa6HIMtMO/KFXC/6P+Bg05cQM9\n1Qq7iZR2zIg62AjC7z41xivdf8PdAgMBAAGjGDAWMBQGA1UdEQQNMAuCCWxvY2Fs\naG9zdDANBgkqhkiG9w0BAQsFAAOCAQEAAn4D/QY8pdvbPYx+yDeYPlHnYv68ErBK\n7Ib2rrtM7jtumBVL9BCneacjdsLmsrXwNdQkQMynxl6bMa+uR3YkXyQSVs+aSwKy\nIkz++rI5ALRI5KQr/DGzWrrmlIbBrXtQkLUR2mnyw9t+ozSMPtdedVCr50c88B5j\ndnksF6odkXet2gpVa5aZ8T2HUl+DtixkKoQ66Ra0/cXXdi3pk6zfRAm8/wtVIzQY\nX2+rur2NOPUuCzdWAvNjzuWCgmH8A2BxCOWBAMJ/GpsVJpqJp0tgm7ah1N+r1y0c\nRqxXZw7bu3NXedB1YqmOvYRcAGK2WXH4aV7I/rDRCc+aMs1GCOocbw==</wsse:BinarySecurityToken></wsse:Security></SOAP-ENV:Header><SOAP-ENV:Body xmlns:ns0="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" ns0:Id="id-311224dc-3b4c-4729-b057-b3abb3bcd95d">\n    <ns2:helloWorldin>\n      <ns1:RequestHeader>\n        <ns1:SenderId>12319203</ns1:SenderId>\n        <ns1:RequestId>1</ns1:RequestId>\n        <ns1:Timestamp>2015-02-03T14:30:00+02:00</ns1:Timestamp>\n        <ns1:Language>FI</ns1:Language>\n        <ns1:UserAgent>Kajala WS</ns1:UserAgent>\n        <ns1:ReceiverId>123192031</ns1:ReceiverId>\n      </ns1:RequestHeader>\n    <ns1:ApplicationRequest>PEFwcGxpY2F0aW9uUmVxdWVzdCB4bWxuczp4c2k9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hLWluc3RhbmNlIiB4bWxuczp4c2Q9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hIiB4bWxucz0iaHR0cDovL2J4ZC5maS94bWxkYXRhLyI+CiAgICA8Q3VzdG9tZXJJZD4wNjExMzM8L0N1c3RvbWVySWQ+CiAgICA8VGltZXN0YW1wPjIwMTItMDItMjBUMDg6NTA6NTkuNDMxOTAxMiswMTowMDwvVGltZXN0YW1wPgogICAgPEVudmlyb25tZW50PlRFU1Q8L0Vudmlyb25tZW50PgogICAgPEZpbGVSZWZlcmVuY2VzPgogICAgPEZpbG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zl0K296U01QdGRlZFZDcjUwYzg4QjVqCmRua3NGNm9ka1hldDJncFZhNWFaOFQySFVsK0R0aXhrS29RNjZSYTAvY1hYZGkzcGs2emZSQW04L3d0Vkl6UVkKWDIrcnVyMk5PUFV1Q3pkV0F2Tmp6dVdDZ21IOEEyQnhDT1dCQU1KL0dwc1ZKcHFKcDB0Z203YWgxTityMXkwYwpScXhYWnc3YnUzTlhlZEIxWXFtT3ZZUmNBR0syV1hINGFWN0kvckRSQ2MrYU1zMUdDT29jYnc9PTwvWDUwOUNlcnRpZmljYXRlPgogICAgICAgICAgPC9YNTA5RGF0YT4KICAgICAgICA8L0tleUluZm8+CiAgICA8L1NpZ25hdHVyZT4KPC9BcHBsaWNhdGlvblJlcXVlc3Q+Cg==</ns1:ApplicationRequest>\n    </ns2:helloWorldin>\n  </SOAP-ENV:Body>\n</SOAP-ENV:Envelope>'
        self.assertEqual(self.normalize_soap_env(ref_bytes), self.normalize_soap_env(signed_body_bytes))

    def test_validate_xml(self):
        xml = os.path.join(settings.BASE_DIR, 'data/finvoice/xsd-test.xml')
        xsd = os.path.join(settings.BASE_DIR, 'data/finvoice/xsd-test.xsd')
        with open(xml, 'rb') as fp:
            validate_xml(fp.read(), xsd)
