using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV.UI;
using Emgu.CV;
using Emgu.CV.Structure;

namespace Foto_Scanner_New
{
    /// <summary>
    /// Class to set new perimter to detect images.
    /// </summary>
    public partial class Extras : Form
    {
        public int ergebnis = 0;
        
        Image<Bgr, byte> image;
        Rectangle r;

        int x = -1;
        int y = -1;
        int w = 0;
        int h = 0;

        int[] posX;
        int[] posY;
        int index = -1;

        int points = 0;
        int faktor = 1;

        /// <summary>
        /// Shows scaled image.
        /// </summary>
        /// <param name="image">Input image</param>
        /// <param name="f">Scale factor</param>
        public Extras(Image<Bgr, byte> image, int f)
        {
            InitializeComponent();
            this.Width = image.Width;
            this.Height = image.Height;
            this.pictureBox1.Height = image.Height;
            this.pictureBox1.Width = image.Width;
            this.pictureBox1.Image = image.ToBitmap();
            this.image = image;

            posX = new int[2];
            posY = new int[2];

            this.faktor = f;
        }

        /// <summary>
        /// Handler to set corners of the rectangle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            x = 0;
            y = 0;
            w = 0;
            h = 0;

            x = e.X;
            y = e.Y;

            if (points < 2)
            {
                index ++;
                CircleF c = new CircleF(new PointF(x, y), 5);
                this.image.Draw(c, new Bgr(Color.Red), 2);
                this.pictureBox1.Image = image.ToBitmap();
                posX[index] = x;
                posY[index] = y;
            }

            if (index == 1) {
                r = new Rectangle(posX[0], posY[0], posX[1] - posX[0], posY[1] - posY[0]);
                image.Draw(r, new Bgr(Color.Red), 2);
                this.pictureBox1.Image = image.ToBitmap();
                index = -1;

                int umfang = (r.Height*2) + (r.Width*2);
                umfang = umfang * faktor;
                

                DialogResult dr = MessageBox.Show("Berechneter Umfang: " + umfang, "Umfang", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (dr == DialogResult.OK && umfang > 0)
                {
                    this.DialogResult = DialogResult.OK;
                    this.ergebnis = umfang;
                }
                else { 
                    this.DialogResult = DialogResult.Cancel;
                    this.ergebnis = 0;    
                }
                
            }

            points++;

            if (points == 2) {
                
                this.Close();
                
                posX = new int[2];
                posY = new int[2];
                points = 0;
                index = -1;
            }
           
        }
    }
}
