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 Form2 : Form
    {
        private const int ALPHA = 120;
        Bitmap bitmap;
        Graphics graphics;
        public Form2()
        {
            InitializeComponent();

            //for testing quad 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)); //yellow

            //Create POINTS
            Point point0 = new Point(100, 50);
            Point point1 = new Point(50, 100);
            Point point2 = new Point(100, 150);
            Point point3 = new Point(150, 50);
      
            Point[] points = { point0, point1, point2, point3 };

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

            //Graphics polygon2 = CreateGraphics();
            Point point4 = new Point(150, 150);
            Point point5 = new Point(200, 200);
            Point point6 = new Point(250, 200);
            Point point7 = new Point(300, 150);


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

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

            for (int i = 0; i < 10; 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 quad class on the FORM
        public void TestQuadFORM()
        {
            Quadrilateral test = new Quadrilateral(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 < 6; i++)
            {
                triangle.CompositingMode = CompositingMode.SourceOver;
                triangle.FillPolygon(brush, points);

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

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

            graphics.Clear(Color.White);

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

                test = new Quadrilateral(this.Width, this.Height);
                points = test.Points;
                color = test.Color;
                brush = new SolidBrush(color);
            }

        }
        private void click(object sender, EventArgs e)
        {
            //Draw();
            //DrawRandom();
            //TestQuadFORM();
            TestQuadBITMAP();

            Invalidate();
        }

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