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 AForge.Controls;
using ImageBlockz.Operations;
using ImageBlockz.Operations.Edit;
using ImageBlockz.Api;
using ImageBlockz.Core;

namespace ImageBlockz.Operations.Controls
{
    public partial class MidtoneLevelsControl : OperationControl
    {
        private ImageBlockz.Operations.Filters.MidtoneLevels midtoneLevels;

        public MidtoneLevelsControl()
            : base(typeof(ImageBlockz.Operations.Filters.MidtoneLevels).FullName)
        {
            IsPreview = true;
            InitializeComponent();

            dbMinimum.SetDigitBox(DigitBox.ValueType.Int, 0, 255, 0);
            dbMaximum.SetDigitBox(DigitBox.ValueType.Int, 0, 255, 255);
            dbMidtone.SetDigitBox(DigitBox.ValueType.Int, 1, 254, 122);
            
        }

        public override IOperation GetOperation()
        {
            midtoneLevels.Min = dbMinimum.GetValue();
            midtoneLevels.Max = dbMaximum.GetValue();
            midtoneLevels.Midtone = dbMidtone.GetValue();
            return midtoneLevels;
        }

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

            if (operation is ImageBlockz.Operations.Filters.MidtoneLevels == false)
            {
                throw new ArgumentException("operation", "Argument operation is not instance of "
                    + typeof(ImageBlockz.Operations.Filters.MidtoneLevels).FullName);
            }

            midtoneLevels = (ImageBlockz.Operations.Filters.MidtoneLevels)operation;
        }

        public override bool OnOK()
        {
            midtoneLevels.Min = dbMinimum.GetValue();
            midtoneLevels.Max = dbMaximum.GetValue();
            midtoneLevels.Midtone = dbMidtone.GetValue();

            return true;
        }

        public override void ShowData()
        {
            if (IsNew)
            {
                dbMinimum.Value = 0;
                dbMaximum.Value = 255;
                dbMidtone.Value = 123;
                
            }
            else
            {
                dbMinimum.Value = midtoneLevels.Min;
                dbMaximum.Value = midtoneLevels.Max;
                dbMidtone.Value = midtoneLevels.Midtone;
            }
            CalculateColorSlider();

        }

        private void colorSlider_ValuesChanged(object sender, EventArgs e)
        {

            dbMinimum.Value = (int) (colorSlider.Min);
            dbMaximum.Value = (int)(colorSlider.Max);
            dbMidtone.Value = (int)((colorSlider.Min + colorSlider.Max) / 2);
            dbMidtone.SetDigitBox(DigitBox.ValueType.Int, colorSlider.Min, colorSlider.Max);
            dbMinimum.SetDigitBox(DigitBox.ValueType.Int, colorSlider.Min, colorSlider.Max);
            dbMaximum.SetDigitBox(DigitBox.ValueType.Int, colorSlider.Min, colorSlider.Max);
        }

        private void dbMinimum_Validating(object sender, CancelEventArgs e)
        {
            CalculateColorSlider();
            dbMidtone.Value = (int)((colorSlider.Min + colorSlider.Max) / 2);
            dbMidtone.SetDigitBox(DigitBox.ValueType.Int, colorSlider.Min, colorSlider.Max);
        }

        private void dbMaximum_Validating(object sender, CancelEventArgs e)
        {
            CalculateColorSlider();
            dbMidtone.Value = (int)((colorSlider.Min + colorSlider.Max) / 2);
            dbMidtone.SetDigitBox(DigitBox.ValueType.Int, colorSlider.Min, colorSlider.Max);
        }

        private void CalculateColorSlider()
        {
            colorSlider.Min = (int)((dbMinimum.GetValue()));
            colorSlider.Max = (int)((dbMaximum.GetValue()));
        }

    }
}
