﻿// Copyright (c) Narvalo.Org. All rights reserved. See LICENSE.txt in the project root for license information.

namespace Quaderno.Xml.XDom
{
    using System.IO;
    using System.Xml.Linq;
    using Narvalo;
    using Narvalo.Finance;
    using Narvalo.Xml;
    using Quaderno.Entities;
    using Quaderno.Internal;
    using static Quaderno.Xml.QuadernoSchema;

    public sealed partial class BankReader
    {
        private readonly XElement _element;

        public BankReader(XElement element)
        {
            Require.NotNull(element, nameof(element));

            _element = element;
        }

        public Bank Read()
        {
            // <Bank bic>
            var bic = Bic.TryParse(_element.AttributeOrThrow(BicAttrName).Value)
                .ValueOrThrow(err => throw new InvalidDataException(err));

            // <Bank name>
            var name = _element.AttributeOrThrow(NameAttrName).GetValueAsDisplayName();

            return new Bank(bic, name);
        }
    }
}
