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


namespace ImageBlockz.Gui
{
    public partial class OperationDialog : Form
    {
        private Bitmap image;
        private Bitmap preview;
        private OperationControl control;
        private bool isPreviewEnabled;

        public OperationDialog()
        {
            InitializeComponent();
        }

        public void SetControl(OperationControl control)
        {
            panelTop.Controls.Clear();
            panelTop.Controls.Add(control);

            this.control = control;
        }

        public new DialogResult ShowDialog()
        {
            if (control == null)
            {
                return DialogResult.Abort;
            }

            return ShowDialog(control.IsPreview);
        }

        public DialogResult ShowDialog(bool isPreviewEnabled)
        {
            if (control == null)
            {
                return DialogResult.Abort;
            }

            if (isPreviewEnabled)
            {
                CopyPreviewImage(control.Bmp);
                pictureBoxPreview.Image = preview;
                
                btnPreview.Visible = checkBoxPreview.Checked;
                checkBoxPreview.Visible = true;
            }
            else
            {
                btnPreview.Visible = false;
                checkBoxPreview.Visible = false;
            }
            this.isPreviewEnabled = isPreviewEnabled;

            SetDialogSize();
            
            control.ShowData();

            return base.ShowDialog();
        }

        private void CopyPreviewImage(Bitmap image)
        {
            Rectangle imageRect = new Rectangle(Point.Empty, image.Size);
            Rectangle selectionRect = new Rectangle(0, 0, 400, 266);

            if (imageRect.Contains(selectionRect))
            {
                int x = (imageRect.Width - selectionRect.Width) / 2;
                int y = (imageRect.Height - selectionRect.Height) / 2;

                selectionRect.Offset(x, y);
            }
            else
            {
                selectionRect.Intersect(imageRect);
            }

            this.image = image;
                  
            preview = image.Clone(selectionRect, PixelFormat.Format24bppRgb);
        }

        private void SetDialogSize()
        {
            int widthResult = Math.Max(control.Size.Width, 220);

            Size size = new Size(widthResult + 30, control.Size.Height + 67);

            if (isPreviewEnabled && checkBoxPreview.Checked)
            {
                size.Width = Math.Max(size.Width, preview.Width);
                size.Height = size.Height + preview.Height;
            }

            MinimumSize = size;
            MaximumSize = size;
            Size = size;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool isValid = control.OnOK();

            if (isValid)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            control.OnCancel();
        }

        private void checkBoxPreview_CheckedChanged(object sender, EventArgs e)
        {
            SetDialogSize();
            btnPreview.Visible = checkBoxPreview.Checked;
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            bool isValid = control.OnOK();

            if (isValid)
            {
                pictureBoxPreview.Image = control.GetOperation().Do(preview);
            }
            else
            {
                MessageBox.Show("Unable to display preview. Invalid values.");
            }
            
        }
    }
}
