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 BrightnessControl : OperationControl
    {
        private ImageBlockz.Operations.Filters.Brightness brightness;

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

            dbBrightness.ValueChangedEvent += new DigitBox.ValueChangedHandler(dbBrightness_ValueChanged);
            dbBrightness.SetDigitBox(DigitBox.ValueType.Int, -100, +100, 0);
            
        }

        public override IOperation GetOperation()
        {
            brightness.Value = dbBrightness.GetValue();
            return brightness;
        }

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

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

            brightness = (ImageBlockz.Operations.Filters.Brightness)operation;
        }

        public override bool OnOK()
        {
            if (dbBrightness.IsOK() == false)
            {
                return false;
            }

            brightness.Value = dbBrightness.GetValue();

            return true;
        }

        public override void ShowData()
        {
            if (IsNew)
            {
                dbBrightness.Value = 0;
                
            }
            else
            {
                dbBrightness.Value = brightness.Value;               
            }
            CalculateColorSlider();

        }

        private void colorSlider_ValuesChanged(object sender, EventArgs e)
        {

            dbBrightness.Value = (int) (colorSlider.Min / 256.0 * 200.0 - 100.0);
        }

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

        private void CalculateColorSlider()
        {
            colorSlider.Min = (int)((dbBrightness.GetValue() + 100) / 200.0 * 256.0);
        }

        private void dbBrightness_ValueChanged(object sender, EventArgs e)
        {
            CalculateColorSlider();
        }

    }
}
