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 SaturationControl : OperationControl
    {
        private ImageBlockz.Operations.Filters.Saturation saturation;

        public SaturationControl()
            : base(typeof(ImageBlockz.Operations.Filters.Contrast).FullName)
        {
            IsPreview = true;

            InitializeComponent();

            dbSaturation.SetDigitBox(DigitBox.ValueType.Int, -100, +100, 0);
            
        }

        public override IOperation GetOperation()
        {
            saturation.Value = dbSaturation.GetValue();
            return saturation;
        }

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

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

            saturation = (ImageBlockz.Operations.Filters.Saturation)operation;
        }

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

            saturation.Value = (dbSaturation.GetValue());

            return true;
        }

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

        }

        private void colorSlider_ValuesChanged(object sender, EventArgs e)
        {

            dbSaturation.Value = (int) (colorSlider.Min / 255.0 * 200.0 - 100.0);
        }

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

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


    }
}
