using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace PAINT
{
    public partial class Paint : Form
    {
        Graphics graphics;
        Bitmap NewImg;
        PictureBox picBoxClicked;
        PictureBox ToolClicked;
        WidthRectanglePointer widthRectangle;
        WidthCirclePointer widthCircle;
        TypeTool type;
        Pen pen ;
        string name,Penname;
        int Loc_X, Loc_Y, Loc_X1, Loc_Y1;
        Rectangle rect , rect1;
        Color colorFront, colorBack;

        public enum WidthRectanglePointer
        {
            Big = 7,
            Medium = 3,
            Small = 1
        }

        public enum WidthCirclePointer
        {
            Big = 7,
            Medium = 3,
            Small = 1
        }

        public enum TypeTool
        {
            RectPointer,
            CirPointer,
            BlankRectangle,
            FillRectangle,
            FBRectangle,
            BlankCircle ,
            FillCircle,
            FBCircle,
            Line 
        }

        public Paint()
        {
            InitializeComponent();
        }

        private void Paint_Load(object sender, EventArgs e)
        {
            NewPage();
            picBoxClicked = BCpicture;
            ToolClicked = penpicture;
            colorFront = Color.Black ;
            colorBack = Color.White;
        }

        private void picBlack_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Black, e);
        }

        private void picDarkGray_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.DarkGray, e);
        }

        private void picMaroon_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Maroon, e);
        }

        private void picOlive_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Olive, e);
        }

        private void picGreen_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Green, e);
        }

        private void picTeal_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Teal, e);
        }

        private void picNavy_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Navy, e);
        }

        private void picPurple_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Purple, e);
        }

        private void picDarkKhaki_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.DarkKhaki, e);
        }

        private void picDarkGreen_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.DarkGreen, e);
        }

        private void picDodgerBlue_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.DodgerBlue, e);
        }

        private void picDarkSlateGray_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.DarkSlateGray, e);
        }

        private void pictureMediumSlateBlue_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.MediumSlateBlue, e);
        }

        private void pictureSaddleBrown_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.SaddleBrown, e);
        }

        private void picWhite_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.White, e);
        }

        private void picLightGray_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.LightGray, e);
        }

        private void picRed_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Red, e);
        }

        private void picYellow_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Yellow, e);
        }

        private void picLime_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Lime, e);
        }

        private void picCyan_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Cyan, e);
        }

        private void picBlue_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Blue, e);
        }

        private void picFuchsia_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Fuchsia, e);
        }

        private void picLemonChiffon_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.LemonChiffon, e);
        }

        private void picGreenYellow_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.GreenYellow, e);
        }

        private void picSkyBlue_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.SkyBlue, e);
        }

        private void picPlum_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Plum, e);
        }

        private void picDeepPink_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.DeepPink, e);
        }

        private void picCoral_MouseDown(object sender, MouseEventArgs e)
        {
            ChangeColor(Color.Coral, e);
        }

        private void ChangeColor(Color color, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                colorFront = color;
                FrontColor.BackColor = color;
            }
            else
                if (e.Button == MouseButtons.Right)
                {
                    colorBack = color;
                    BackColor.BackColor = color;
                }
        }
        
        private void NewPage()
        {
            Bitmap bmp = new Bitmap(pictureBox.Width, pictureBox.Height);
            graphics = Graphics.FromImage(bmp);
            graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox.Width, pictureBox.Height);
            NewImg = (Bitmap)bmp.Clone();
            pictureBox.Image = NewImg;
            graphics = Graphics.FromImage(NewImg);
        }

        private void clickToolBox(PictureBox picBox)
        {
            ToolClicked.BorderStyle = BorderStyle.None;
            picBox.BorderStyle = BorderStyle.Fixed3D;
            ToolClicked = picBox;

            BCpicture.Visible = false;
            MCpicture.Visible = false;
            SCpicture.Visible = false;
            BRpicture.Visible = false;
            MRpicture.Visible = false;
            SRpicture.Visible = false;
            Nullpicture.Visible = false;
            Fillpicture.Visible = false;
            Blankpicture.Visible = false;
            FBpicture.Visible = false;

            Penname = picBox.Name;

            if (Penname == "penpicture")
            {
                clickPictureBox(BCpicture);
                name = "True";
                BCpicture.Visible = true;
                MCpicture.Visible = true;
                SCpicture.Visible = true;
                BRpicture.Visible = true;
                MRpicture.Visible = true;
                SRpicture.Visible = true;
            }

            else if (Penname == "linepicture")
            {
                type = TypeTool.Line;
                Nullpicture.Visible = true;
            }
            else if (Penname == "Rectpicture")
            {
                clickPictureBox(Blankpicture);
                name = "False";
                Fillpicture.Visible = true;
                Blankpicture.Visible = true;
                FBpicture.Visible = true;
            }
            else if (Penname == "circlepicture")
            {
                clickPictureBox(Blankpicture);
                name = "False";
                Fillpicture.Visible = true;
                Blankpicture.Visible = true;
                FBpicture.Visible = true;
            }
        }

        private void clickPictureBox(PictureBox picBox)
        {
            picBoxClicked.BorderStyle = BorderStyle.None;
            picBox.BorderStyle = BorderStyle.Fixed3D;
            picBoxClicked = picBox;
            
            string name = picBox.Name;
            
            switch (name)
            {
                case "BCpicture":
                    widthCircle = WidthCirclePointer.Big;
                    type = TypeTool.CirPointer;
                    break;
                case "MCpicture":
                    widthCircle = WidthCirclePointer.Medium;
                    type = TypeTool.CirPointer;
                    break;
                case "SCpicture":
                    widthCircle = WidthCirclePointer.Small;
                    type = TypeTool.CirPointer;
                    break;
                case "BRpicture":
                    widthRectangle = WidthRectanglePointer.Big;
                    type = TypeTool.RectPointer;
                    break;
                case "MRpicture":
                    widthRectangle = WidthRectanglePointer.Medium;
                    type = TypeTool.RectPointer;
                    break;
                case "SRpicture":
                    widthRectangle = WidthRectanglePointer.Small;
                    type = TypeTool.RectPointer;
                    break;
                case "Fillpicture":
                    if (Penname == "Rectpicture")
                    {
                        type = TypeTool.FillRectangle;
                    }
                    else if (Penname == "circlepicture")
                    {
                        type = TypeTool.FillCircle;
                    }

                    break;
                case "Blankpicture":
                    if (Penname == "Rectpicture")
                    {
                        type = TypeTool.BlankRectangle;
                    }
                    else if (Penname == "circlepicture")
                    {
                        type = TypeTool.BlankCircle;
                    }
                    break;
                case "FBpicture":
                    if (Penname == "Rectpicture")
                    {
                        type = TypeTool.FBRectangle;
                    }
                    else if (Penname == "circlepicture")
                    {
                        type = TypeTool.FBCircle;
                    }
                    break;
            }
        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            Loc_X = e.X;
            Loc_Y = e.Y;
        }

        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {

            Loc_X1 = e.X;
            Loc_Y1 = e.Y;
            PaintOrErase(e);
        }

        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (name == "True")
            {
                PaintOrErase(e);
            }
        }

        private void PaintOrErase(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DrawFigure(colorFront, colorBack , e);
                pictureBox.Refresh();
            }
            else
                if (e.Button == MouseButtons.Right)
                {
                    DrawFigure(colorBack ,colorFront, e);
                    pictureBox.Refresh();
                }
        }
        private void DrawFigure(Color color1 , Color color2 , MouseEventArgs e)
        {
            pen = new Pen(color1);
            switch (type)
            {
                case TypeTool.RectPointer:
                    rect = new Rectangle(e.X, e.Y, (int)widthRectangle, (int)widthRectangle);
                    graphics.DrawRectangle(pen, rect);
                    graphics.FillRectangle(new SolidBrush(color1), rect);
                    break;

                case TypeTool.CirPointer:

                    rect = new Rectangle(e.X, e.Y, (int)widthCircle, (int)widthCircle);
                    graphics.DrawEllipse(pen, rect);
                    graphics.FillEllipse(new SolidBrush(color1), rect);
                    break;

                case TypeTool.Line:
                    graphics.DrawLine(pen, Loc_X1, Loc_Y1, Loc_X, Loc_Y);
                    break;   

                case TypeTool.BlankRectangle :

                    rect = new Rectangle(Loc_X, Loc_Y, Loc_X1 - Loc_X, Loc_Y1 - Loc_Y);
                    graphics.DrawRectangle(pen, rect);
                    break;

                case TypeTool.FillRectangle:

                    rect = new Rectangle(Loc_X, Loc_Y, Loc_X1 - Loc_X, Loc_Y1 - Loc_Y);
                    rect1 = new Rectangle(Loc_X+1, Loc_Y+1, Loc_X1 - Loc_X-1, Loc_Y1 - Loc_Y-1);
                    graphics.DrawRectangle(pen, rect);
                    graphics.FillRectangle(new SolidBrush(color2), rect1);
                    break;

                case TypeTool.FBRectangle:

                    rect = new Rectangle(Loc_X, Loc_Y, Loc_X1 - Loc_X, Loc_Y1 - Loc_Y);
                    graphics.FillRectangle(new SolidBrush(color1), rect);
                    break;
                case TypeTool.BlankCircle:

                    rect = new Rectangle(Loc_X, Loc_Y, Loc_X1 - Loc_X, Loc_Y1 - Loc_Y);
                    graphics.DrawEllipse(pen, rect);
                    break;

                case TypeTool.FillCircle:
                    rect = new Rectangle(Loc_X, Loc_Y, Loc_X1 - Loc_X, Loc_Y1 - Loc_Y);
                    rect1 = new Rectangle(Loc_X+1 , Loc_Y+1 , Loc_X1 - Loc_X -2, Loc_Y1 - Loc_Y-2 );
                    graphics.DrawEllipse(pen, rect);
                    graphics.FillEllipse(new SolidBrush(color2), rect1);
                    break;

                case TypeTool.FBCircle:

                    rect = new Rectangle(Loc_X, Loc_Y, Loc_X1 - Loc_X, Loc_Y1 - Loc_Y);
                    graphics.FillEllipse(new SolidBrush(color1), rect);
                    break;
            }
        }

        private void BCpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(BCpicture);
            name = "True";
        }

        private void MCpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(MCpicture);
            name = "True";
        }

        private void SCpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(SCpicture);
            name = "True";
        }

        private void BRpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(BRpicture);
            name = "True";
        }

        private void MRpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(MRpicture);
            name = "True";
        }

        private void SRpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(SRpicture);
            name = "True";
        }

        private void penpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickToolBox(penpicture);
        }

        private void linepicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickToolBox(linepicture);
        }

        private void Rectpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickToolBox(Rectpicture);
        }

        private void circlepicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickToolBox(circlepicture);
        }

        private void newToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            NewPage();
        }

        private void Blankpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(Blankpicture);
            name = "False";
        }

        private void Fillpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(Fillpicture);
            name = "False";
        }

        private void FBpicture_MouseDown(object sender, MouseEventArgs e)
        {
            clickPictureBox(FBpicture);
            name = "False";
        }
    }
}