﻿namespace SimpleXlsx.Entities
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;

    public class Workbook
    {
        public IEnumerable<WorkbookSheet> Sheets { get; set; }

        public static Workbook FromXmlStream(Stream stream)
        {
            var workbookDocument = XDocument.Load(stream);
            var workbook = new Workbook();

            // Getting xmlns
            var ns = workbookDocument.Root.GetDefaultNamespace();
            
            var sheetName = XName.Get("sheet", ns.NamespaceName);
            var sheets = workbookDocument.Descendants(sheetName);
            
            workbook.Sheets = sheets.Select(WorkbookSheet.FromXElement);

            return workbook;
        }
    }
}
