using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageBlockz.Operations;
using ImageBlockz.Operations.Edit;
using ImageBlockz.Api;
using ImageBlockz.Core;

namespace ImageBlockz.Operations.Controls
{
    public partial class CropControl : OperationControl
    {
        private ImageBlockz.Operations.Edit.Crop crop;

        public CropControl()
            : base(typeof(ImageBlockz.Operations.Edit.Crop).FullName)
        {
            IsPreview = false;

            InitializeComponent();

            dbX.ValueChangedEvent += new DigitBox.ValueChangedHandler(anyDigitBox_ValueChanged);
            dbY.ValueChangedEvent += new DigitBox.ValueChangedHandler(anyDigitBox_ValueChanged);
            dbWidth.ValueChangedEvent += new DigitBox.ValueChangedHandler(anyDigitBox_ValueChanged);
            dbHeight.ValueChangedEvent += new DigitBox.ValueChangedHandler(anyDigitBox_ValueChanged);
        }

        public override void SetOperation(IOperation operation)
        {
            base.SetOperation(operation);

            if (operation is ImageBlockz.Operations.Edit.Crop)
            {
                crop = (ImageBlockz.Operations.Edit.Crop)GetOperation();
            }
        }

        public override bool OnOK()
        {
            if (dbX.IsOK() == false || dbY.IsOK() == false || dbWidth.IsOK() == false || dbHeight.IsOK() == false)
            {
                return false;
            }

            crop.Rec = new Rectangle((int)dbX.GetValue(), (int)dbY.GetValue(), (int)dbWidth.GetValue(), (int)dbHeight.GetValue());
            
            return true;
        }

        public override void ShowData()
        {
            if (IsNew)
            {
                dbX.Value = 0;
                dbY.Value = 0;
                dbWidth.Value = 0;
                dbHeight.Value = 0;

                if (Bmp != null)
                {
                    dbWidth.Value = Bmp.Width;
                    dbHeight.Value = Bmp.Height;
                }
            }
            else
            {
                dbX.Value = crop.Rec.X;
                dbY.Value = crop.Rec.Y;
                dbWidth.Value = crop.Rec.Width;
                dbHeight.Value = crop.Rec.Height;
            }
        }

        private void anyDigitBox_ValueChanged(object sender, EventArgs e)
        {
            if (Bmp == null)
            {
                return;
            }

            dbX.SetDigitBox(DigitBox.ValueType.Int, 0, Bmp.Width);
            dbY.SetDigitBox(DigitBox.ValueType.Int, 0, Bmp.Height);
            dbWidth.SetDigitBox(DigitBox.ValueType.Int, 1, (int) (Bmp.Width - dbX.GetValue()));
            dbHeight.SetDigitBox(DigitBox.ValueType.Int, 1, (int) (Bmp.Height - dbY.GetValue()));

            dbX.Validate(dbX.GetValue());
            dbX.Validate(dbY.GetValue());
            dbX.Validate(dbWidth.GetValue()); 
            dbX.Validate(dbHeight.GetValue());
        }     
    }
}
