using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bbiwarg.Utility
{
    class Vector2D
    {
        public float X { get; private set; }
        public float Y { get; private set; }
        public int IntX { get { return (int)X; } }
        public int IntY { get { return (int)Y; } }
        public float Length { get { return (float)Math.Sqrt(X * X + Y * Y); } }

        public Vector2D(float x, float y)
        {
            X = x;
            Y = y;
        }

        public Vector2D(Vector2D vec)
        {
            X = vec.X;
            Y = vec.Y;
        }

        public Vector2D(Point point)
        {
            X = point.X;
            Y = point.Y;
        }

        public Vector2D(PointF point)
        {
            X = point.X;
            Y = point.Y;
        }

        public float getDistanceTo(Vector2D point)
        {
            return (this - point).Length;
        }

        public float getAngleBetween(Vector2D vector)
        {
            return (float)Math.Acos(dotProduct(vector) / (Length * vector.Length));
        }

        public float dotProduct(Vector2D vector)
        {
            return X * vector.X + Y * vector.Y;
        }

        public float cross(Vector2D v)
        {
            return X * v.Y - Y * v.X;
        }

        public bool isInBox(Vector2D corner1, Vector2D corner2)
        {
            float minX = Math.Min(corner1.X, corner2.X);
            float maxX = Math.Max(corner1.X, corner2.X);
            float minY = Math.Min(corner1.Y, corner2.Y);
            float maxY = Math.Max(corner1.Y, corner2.Y);
            return (minX <= X && X <= maxX && minY <= Y && Y <= maxY);
        }

        public Vector2D normalize()
        {
            float length = Length;
            return new Vector2D(X / length, Y / length);
        }

        public Vector2D normalizeComponentOne() {
            if (Math.Abs(X) > Math.Abs(Y))
                return new Vector2D(1, Y / X);
            else
                return new Vector2D(X / Y, 1);
        } 

        public bool isWithin(float minX, float minY, float maxX, float maxY)
        {
            return (X >= minX && Y >= minY && X <= maxX && Y <= maxY);
        }

        public Vector2D moveInBound(float minX, float minY, float maxX, float maxY)
        {
            float x = Math.Min(maxX, Math.Max(minX, X));
            float y = Math.Min(maxY, Math.Max(minY, Y));
            return new Vector2D(x, y);
        }

        public Vector2D getOrthogonal(bool side)
        {
            if (side)
                return new Vector2D(Y, -X);
            else
                return new Vector2D(-Y, X);
        }

        public Vector2D getInverse()
        {
            return new Vector2D(-X, -Y);
        }

        public override string ToString()
        {
            return "(" + X + "|" + Y + ")";
        }

        public static Vector2D operator *(float scalar, Vector2D vector)
        {
            return new Vector2D(scalar * vector.X, scalar * vector.Y);
        }

        public static Vector2D operator *(Vector2D vector, float scalar)
        {
            return new Vector2D(scalar * vector.X, scalar * vector.Y);
        }

        public static Vector2D operator /(Vector2D vector, float scalar)
        {
            return new Vector2D(vector.X / scalar, vector.Y / scalar);
        }

        public static Vector2D operator +(Vector2D vector1, Vector2D vector2)
        {
            return new Vector2D(vector1.X + vector2.X, vector1.Y + vector2.Y);
        }

        public static Vector2D operator -(Vector2D vector1, Vector2D vector2)
        {
            return new Vector2D(vector1.X - vector2.X, vector1.Y - vector2.Y);
        }

        public static Vector2D mean(List<Vector2D> vectors)
        {
            Vector2D meanVector = new Vector2D(0, 0);
            foreach (Vector2D vector in vectors)
            {
                meanVector += vector;
            }
            return meanVector / vectors.Count;
        }

        public static implicit operator PointF(Vector2D vec)
        {
            return new PointF(vec.X, vec.Y);
        }

        public static implicit operator Point(Vector2D vec)
        {
            return new Point(vec.IntX, vec.IntY);
        }

    }
}
