﻿using GeneticCreatures.UtilityClasses;
using OpenTK.Graphics.OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeneticCreatures.Classes.UtilityClasses
{
    class ShapeDrawer
    {
        public static void DrawCircle(Vector2D center, float r, Colour col)
        {
            DrawCircle(center.x, center.y, r, col);
        }
        public static void DrawCircle(float x, float y, float r, Colour col)
        {
            int segments = 32;
            col.SetGLColour();
            GL.Begin(BeginMode.Polygon);

            for (int i = 0; i < segments; i++)
            {
                float theta = (2.0f * (float)Math.PI * (float)i) / (float)segments;
                float xx = r * (float)Math.Cos(theta);
                float yy = r * (float)Math.Sin(theta);
                GL.Vertex3(x + xx, y + yy, 4.0f);
            }

            GL.End();
        }

        public static void DrawLine(Vector2D one, Vector2D two, float width, Colour col)
        {
            DrawLine(one.x, one.y, two.x, two.y, width, col);
        }
        public static void DrawLine(float x1, float y1, float x2, float y2, float width, Colour col)
        {
            col.SetGLColour();
            GL.Begin(BeginMode.Lines);
            GL.Vertex3(x2, y2, 4.0f);
            GL.Vertex3(x1, y1, 4.0f);
            GL.End();
        }


        public static void DrawRectangle(Vector2D center, float width, float height, Colour col)
        {
            DrawRectangle(center.x, center.y, width, height, col);
        }
        public static void DrawRectangle(float x, float y, float width, float height, Colour col)
        {
            col.SetGLColour();
            GL.Begin(BeginMode.Quads);

            GL.Vertex3(x + width/2f, y + height/2f, 4.0f);
            GL.Vertex3(x - width / 2f, y + height / 2f, 4.0f);
            GL.Vertex3(x - width / 2f, y - height / 2f, 4.0f);
            GL.Vertex3(x + width / 2f, y - height / 2f, 4.0f);

            GL.End();
        }

        public static void DrawArrow(Vector2D tip, Vector2D direction, float length, Rotation angle, Colour colour, float lineWidth)
        {
            Vector2D normal = (new Vector2D(1, -direction.x / direction.y)).ConvertToUnit();
            float y = length * (float)Math.Cos(angle.Radians);
            float z = length * (float)Math.Sin(angle.Radians);

            Vector2D end1 = tip - y * direction.ConvertToUnit() + z * normal;
            Vector2D end2 = tip - y * direction.ConvertToUnit() - z * normal;

            ShapeDrawer.DrawLine(end1, tip, lineWidth, colour);
            ShapeDrawer.DrawLine(end2, tip, lineWidth, colour);
        }
    }
}
