using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Polygons
{
    public partial class Form1 : Form
    {
        private const int ALPHA = 120;
        Bitmap bitmap;
        Graphics graphics;
        public Form1()
        {
            InitializeComponent();

            //for testing triangle class with a BITMAP
            bitmap = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            graphics = Graphics.FromImage(bitmap);
            //end...BITMAP
        }

        /* FUNCTION: DrawPolygon()
           PURPOSE: Draw an individual polygon on the form, fill the polygon */
        public void Draw()
        {
            //Create GRAPHICS
            Graphics polygon = CreateGraphics();
            MessageBox.Show("in draw");

            //Create BRUSH
            SolidBrush brush = new SolidBrush(Color.FromArgb(ALPHA, 255, 255, 0));

            //Create POINTS
            Point point1 = new Point(250, 100);
            Point point2 = new Point(200, 150);
            Point point3 = new Point(300, 150);

            Point[] points = { point1, point2, point3 };

            //FILL polygon
            polygon.CompositingMode = CompositingMode.SourceOver;
            polygon.FillPolygon(brush, points);


            //second triangle
            //Graphics polygon2 = CreateGraphics();
            Point point4 = new Point(225, 120);
            Point point5 = new Point(150, 200);
            Point point6 = new Point(250, 200);

            SolidBrush brush2 = new SolidBrush(Color.FromArgb(ALPHA,0, 0, 255));
            Point[] points2 = { point4, point5, point6 };
            polygon.FillPolygon(brush2, points2);
        }
        //testing, triangles at random
        public void DrawRandom()
        {
            Graphics polygon = CreateGraphics();
            SolidBrush brush = new SolidBrush(Color.FromArgb(ALPHA, 0, 0, 255));

            Point[] points = new Point[3];
            Random rnd = new Random();
            int point_X, point_Y;

            for (int i = 0; i < 25; i++)
            {
                for (int j = 0; j < points.Length; j++)
                {
                    point_X = rnd.Next(0, this.Width);
                    point_Y = rnd.Next(0, this.Height);
                    points[j] = new Point(point_X, point_Y);
                }

                Color color = new Color();
                int alpha, red, green, blue;

                alpha = rnd.Next(0, 255);
                red = rnd.Next(0, 255);
                green = rnd.Next(0, 255);
                blue = rnd.Next(0, 255);

                color = Color.FromArgb(alpha, red, green, blue);

                brush.Color = color;
                polygon.CompositingMode = CompositingMode.SourceOver;
                polygon.FillPolygon(brush, points);
                //outside, draws; inside, call function to generate
            }
            //first generate triangles
            //second create bitmap
        }

        //testing the trianlge class on the FORM
        public void TestTriangleFORM()
        {
            Triangle test = new Triangle(this.Width, this.Height);
            Point[] points = test.Points;
            Color color = test.Color;

            Graphics triangle = CreateGraphics();
            SolidBrush brush = new SolidBrush(color);

            for (int i = 0; i < 25; i++)
            {
                triangle.CompositingMode = CompositingMode.SourceOver;
                triangle.FillPolygon(brush, points);

                test = new Triangle(this.Width, this.Height);
                points = test.Points;
                color = test.Color;
                brush = new SolidBrush(color);
            }
        }
       
        //testing the triangle class on a BITMAP
        public void TestTriangleBITMAP()
        {
            Triangle test = new Triangle(this.Width, this.Height);
            Point[] points = test.Points;
            Color color = test.Color;

            Graphics triangle = CreateGraphics();
            SolidBrush brush = new SolidBrush(color);

            graphics.Clear(Color.White);

            for (int i = 0; i < 1; i++)
            {
                triangle.CompositingMode = CompositingMode.SourceOver;
                graphics.FillPolygon(brush, points);

                test = new Triangle(this.Width, this.Height);
                points = test.Points;
                color = test.Color;
                brush = new SolidBrush(color);
            }
         
        }
        //TESTING on form, button click
        private void buttonClick(object sender, EventArgs e)
        {
            //Draw();
            //DrawRandom();
            //Console.WriteLine("before testTri");
            //TestTriangleFORM();
            //Console.WriteLine("after testTri");

            //Console.WriteLine("before testTriBit");
            //TestTriangleBITMAP();
            //Console.WriteLine("after testTriBit");

            Invalidate();
        }

        private void Bitmap_Paint(object sender, PaintEventArgs e)
        {
            Console.WriteLine("in paint event");
            e.Graphics.DrawImage(bitmap, 0, 0);
            Console.WriteLine("bitmap drawn...?");
        }
    }
}
