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 ContrastControl : OperationControl
    {
        private ImageBlockz.Operations.Filters.Contrast contrast;

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

            InitializeComponent();

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

        public override IOperation GetOperation()
        {          
            contrast.Value = dbContrast.GetValue();
            return contrast;
        }

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

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

            contrast = (ImageBlockz.Operations.Filters.Contrast)operation;
        }

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

            contrast.Value = (dbContrast.GetValue());

            return true;
        }

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

        }

        private void colorSlider_ValuesChanged(object sender, EventArgs e)
        {

            dbContrast.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)((dbContrast.GetValue() + 100) / 200.0 * 255.0);
        }

    }
}
