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 SimpleLevelsControl : OperationControl
    {
        private ImageBlockz.Operations.Filters.SimpleLevels simpleLevels;

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

            dbMinimum.SetDigitBox(DigitBox.ValueType.Int, 0, 255, 0);
            dbMaximum.SetDigitBox(DigitBox.ValueType.Int, 0, 255, 255);
            
        }

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

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

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

            simpleLevels = (ImageBlockz.Operations.Filters.SimpleLevels)operation;
        }

        public override bool OnOK()
        {
            simpleLevels.Min = dbMinimum.GetValue();
            simpleLevels.Max = dbMaximum.GetValue();

            return true;
        }

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

        }

        private void colorSlider_ValuesChanged(object sender, EventArgs e)
        {

            dbMinimum.Value = (int) (colorSlider.Min);
            dbMaximum.Value = (int)(colorSlider.Max);
        }

        private void dbMinimum_Validating(object sender, CancelEventArgs e)
        {
            CalculateColorSlider();
        }

        private void dbMaximum_Validating(object sender, CancelEventArgs e)
        {
            CalculateColorSlider();
        }

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

    }
}
