using System;
using System.Collections.Generic;
using ZionVault.Basic;

namespace ZionVault.Binder.OCR
{
    public class Paragraph : IDisposable
    {
        private int index;
        private Location position;
        private List<Line> lines;

        public void Dispose()
        {
            if (this.lines != null)
                foreach (Line l in lines)
                    l.Dispose();
            GC.SuppressFinalize(this);
        }

        public int Index
        {
            get { return index; }
            set { index = value; }
        }

        public Location Location
        {
            get { return position; }
            set { position = value; }
        }

        public List<Line> Lines
        {
            get { return lines; }
            set { lines = value; }
        }

        public Paragraph(int _Index, Location _Posision, List<Line> _Lines)
        {
            this.index = _Index;
            this.position = _Posision;
            this.lines = _Lines;
        }

        public Paragraph(System.Xml.XmlElement node)
        {
            this.lines = new List<Line>();

            this.index = int.Parse(node.GetAttribute(@"index"));
            this.position = new Location(node.GetAttribute(@"location"));
            foreach (System.Xml.XmlElement line in node.GetElementsByTagName(@"line"))
            {
                this.lines.Add(new Line(line));
            }
        }

        public System.Xml.XmlNode Export(System.Xml.XmlDocument context)
        {
            System.Xml.XmlElement root = context.CreateElement(@"paragraph");
            root.SetAttribute(@"index", this.index.ToString());
            root.SetAttribute(@"location", this.position.ToString());

            foreach (Line element in this.lines)
            {
                root.AppendChild(element.Export(context));
            }

            return root;
        }
    }
}
