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

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

        internal CircleElementConverter()
        {
        }

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

        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 r = double.Parse(element.Attribute("r").Value);
            var arcCommonParam = $"{r} {r} 0 0 1 ";
            var builder = new StringBuilder();
            builder.Append("M ").Append($"{cx + r} {cy} ")
                   .Append("A ").Append($"{arcCommonParam}").Append($"{cx} {cy + r} ")
                                .Append($"{arcCommonParam}").Append($"{cx - r} {cy} ")
                                .Append($"{arcCommonParam}").Append($"{cx} {cy - r} ")
                                .Append($"{arcCommonParam}").Append($"{cx + r} {cy} ")
                   .Append("Z");
            return new SvgPath(builder.ToString(), unarc: true);
        }
    }
}
