using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Xml.Serialization;

namespace BallotTemplates
{
    public class ChangeFields
    {
        private IReadOnlyList<ChangeField> _items;
        
        public ChangeFieldType Type;

        
        
        
        

        
        
        
        [XmlAttribute("origin")]
        [Browsable(false)]
        public string XmlOrigin
        {
            get =>
                $"{Origin.X.ToString(CultureInfo.InvariantCulture)},{Origin.Y.ToString(CultureInfo.InvariantCulture)}";
            set
            {
                var comps = value.Split(',');
                Origin = new PointF(float.Parse(comps[0], CultureInfo.InvariantCulture), float.Parse(comps[1], CultureInfo.InvariantCulture));
            }
        }

        
        
        
        [XmlAttribute("width")]
        [Browsable(false)]
        public float Width { get; set; }


        
        
        
        [XmlAttribute("height")]
        [Browsable(false)]
        public float Height { get; set; }

        [XmlAttribute("maxNumberOfEntries")]
        [Browsable(false)]
        public int MaxNumberOfEntries { get; set; }


        [XmlAttribute("skip")]
        [Browsable(false)]
        public int Skip { get; set; }

        
        
        
        [XmlAttribute("numRows")]
        [Browsable(false)]
        public int NumRows { get; set; }

        
        
        
        [XmlAttribute("rowDistance")]
        [Browsable(false)]
        public float RowDistance { get; set; }

        
        
        
        [XmlAttribute("numCols")]
        [Browsable(false)]
        public int NumCols { get; set; }

        
        
        
        [XmlAttribute("colDistance")]
        [Browsable(false)]
        public float ColDistance { get; set; }

        private PointF Origin { get; set; } = new PointF(0, 0);

        
        
        
        [XmlAttribute("pattern")]
        [Browsable(false)]
        public string Pattern { get; set; }

        [XmlIgnore]
        public IReadOnlyList<ChangeField> Items
        {
            get
            {
                if (_items != null) return _items;
                var ret = new List<ChangeField>();
                var maxSize = MaxNumberOfEntries + Skip;
                for (var col = 0; col < NumCols && ret.Count < maxSize; ++col)
                    for (var row = 0; row < NumRows; ++row)
                    {
                        var index = col * NumRows + row + 1;
                        var deltaTopLeft = new SizeF(col * ColDistance, row * RowDistance);
                        var deltaBottomRight = new SizeF(deltaTopLeft.Width + Width, deltaTopLeft.Height + Height);
                        var field = ChangeField.Create(Type);

                        field.Id = $"ChangeField{index}";
                        field.CandidateNumber = index;
                        field.TopLeft = new PointF(Origin.X + deltaTopLeft.Width, Origin.Y + deltaTopLeft.Height);
                        field.BottomRight = new PointF(Origin.X + deltaBottomRight.Width, Origin.Y + deltaBottomRight.Height);
                        field.Pattern = Pattern;
                        ret.Add(field);
                        if (ret.Count >= maxSize)
                            break;
                    }
                _items = ret.Skip(Skip).Take(MaxNumberOfEntries).ToList();
                return _items;
            }
        }
    }
}
