using System;
using System.Drawing;

namespace RyzStudio.Drawing
{
    public class Circle
    {

        public Circle()
        {

        }

        public Circle(int x, int y, int width, int height)
        {
            this.X = x;
            this.Y = y;
            this.Width = width;
            this.Height = height;
        }

        public Circle(Rectangle rectangle)
        {
            this.X = rectangle.X;
            this.Y = rectangle.Y;
            this.Width = rectangle.Width;
            this.Height = rectangle.Height;
        }

        public Circle(Point position, Size size)
        {
            this.X = position.X;
            this.Y = position.Y;
            this.Width = size.Width;
            this.Height = size.Height;
        }

        public Circle(Size size)
        {
            this.X = 0;
            this.Y = 0;
            this.Width = size.Width;
            this.Height = size.Height;
        }


        public int X { get; set; } = 0;

        public int Y { get; set; } = 0;

        public int Width { get; set; } = 0;

        public int Height { get; set; } = 0;

        public PointF Origin
        {
            get
            {
                return new PointF()
                {
                    X = (this.Width / 2) + this.X,
                    Y = (this.Height / 2) + this.Y
                };
            }
        }

        public int MinSideLength => Math.Min(this.Width, this.Height);

        public static PointF GetPointromOrigin(PointF originPosition, Size size)
        {
            float offsetX = (size.Width / 2);
            float offsetY = (size.Height / 2);

            PointF rs = new PointF()
            {
                X = originPosition.X,
                Y = originPosition.Y
            };

            rs.X -= offsetX;
            rs.Y -= offsetY;

            return rs;
        }

        public PointF GetPoint(int deg, int distance)
        {
            PointF origin = this.Origin;
            PointF rs = new PointF()
            {
                X = origin.X,
                Y = origin.Y
            };

            if (deg == 0)
            {
                rs.Y -= distance;
            }
            else if (deg == 90)
            {
                rs.X += distance;
            }
            else if (deg == 180)
            {
                rs.Y += distance;
            }
            else if (deg == 270)
            {
                rs.X -= distance;
            }
            else if ((deg > 0) && (deg < 90))
            {
                rs.X += (float)(distance * Math.Sin(calcToRad(deg)));
                rs.Y -= (float)(distance * Math.Cos(calcToRad(deg)));
            }
            else if ((deg > 90) && (deg < 180))
            {
                rs.X += (float)(distance * Math.Cos(calcToRad(deg - 90)));
                rs.Y += (float)(distance * Math.Sin(calcToRad(deg - 90)));
            }
            else if ((deg > 180) && (deg < 270))
            {
                rs.X -= (float)(distance * Math.Sin(calcToRad(deg - 180)));
                rs.Y += (float)(distance * Math.Cos(calcToRad(deg - 180)));
            }
            else if ((deg > 270) && (deg < 360))
            {
                rs.X -= (float)(distance * Math.Cos(calcToRad(deg - 270)));
                rs.Y -= (float)(distance * Math.Sin(calcToRad(deg - 270)));
            }

            // adjust position
            rs.X += this.X;
            rs.Y += this.Y;

            return rs;
        }

        public void Deflate(int x, int y)
        {
            this.Width -= x;
            this.Height -= y;
        }

        protected double calcToRad(int deg) => ((Math.PI / 180) * deg);

    }
}
