using SvgPathProperties;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;

namespace PCUT.Extensions.Converters
{
    internal class EllipseElementConverter : PathConverter
    {
        private static readonly string[] EllipseExcludedAttributes = new[] { "transform", "cx", "cy", "rx", "ry" };
        protected override IEnumerable<string> ExcludedAttributes => EllipseExcludedAttributes;

        internal EllipseElementConverter()
        {
        }

        protected override bool CanApply(XElement element) => element.Name.LocalName == "ellipse";

        protected override SvgPath GetPathFromElement(XElement element)
        {
            var cx = double.Parse(element.Attribute("cx")?.Value ?? "0");
            var cy = double.Parse(element.Attribute("cy")?.Value ?? "0");
            var rx = double.Parse(element.Attribute("rx").Value);
            var ry = double.Parse(element.Attribute("ry").Value);
            var arcCommonParam = $"{rx} {ry} 0 0 1 ";
            var builder = new StringBuilder();
            builder.Append("M ").Append($"{cx + rx} {cy} ")
                   .Append("A ").Append($"{arcCommonParam}").Append($"{cx} {cy + ry} ")
                                .Append($"{arcCommonParam}").Append($"{cx - rx} {cy} ")
                                .Append($"{arcCommonParam}").Append($"{cx} {cy - ry} ")
                                .Append($"{arcCommonParam}").Append($"{cx + rx} {cy} ")
                   .Append("Z");
            return new SvgPath(builder.ToString(), unarc: true);
        }
    }
}
