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 RadiusCropControl : OperationControl
    {
        private ImageBlockz.Operations.Edit.RadiusCrop radiusCrop;

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

            InitializeComponent();
        }

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

            if (operation is ImageBlockz.Operations.Edit.RadiusCrop)
            {
                radiusCrop = (ImageBlockz.Operations.Edit.RadiusCrop)GetOperation();
            }
        }

        public override bool OnOK()
        {
            radiusCrop.Center = new Point((int)dbX.GetValue(), (int)dbY.GetValue());
            radiusCrop.Radius = (int)dbRadius.GetValue();
            return true;
        }

        public override void ShowData()
        {
            if (Bmp != null)
            {
                dbX.SetDigitBox(DigitBox.ValueType.Int, 0, Bmp.Width);
                dbY.SetDigitBox(DigitBox.ValueType.Int, 0, Bmp.Height);
                dbRadius.SetDigitBox(DigitBox.ValueType.Int, 1, (int)Math.Floor(Math.Min(Bmp.Width, Bmp.Height) / 2.0));
            }

            if (IsNew)
            {
                dbX.Value = 0;
                dbY.Value = 0;
                dbRadius.Value = 1;
            }
            else
            {
                dbX.Value = radiusCrop.Center.X;
                dbY.Value = radiusCrop.Center.Y;
                dbRadius.Value = radiusCrop.Radius;
            }
        }
    }
}
