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 ColorWeightControl : OperationControl
    {
        private ImageBlockz.Operations.Filters.ColorWeight colorWeight;

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

            dbRed.SetDigitBox(ImageBlockz.Core.DigitBox.ValueType.Dec, 0, 100, 100);
            dbBlue.SetDigitBox(ImageBlockz.Core.DigitBox.ValueType.Dec, 0, 100, 100);
            dbGreen.SetDigitBox(ImageBlockz.Core.DigitBox.ValueType.Dec, 0, 100, 100);
        }

        public override IOperation GetOperation()
        {
            colorWeight.Red   = dbRed.GetValue();
            colorWeight.Green = dbGreen.GetValue();
            colorWeight.Blue  = dbBlue.GetValue();

            return colorWeight;
        }

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

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

            colorWeight = (ImageBlockz.Operations.Filters.ColorWeight)operation;
        }

        public override bool OnOK()
        {
            if (dbRed.IsOK() == false || dbGreen.IsOK() == false || dbBlue.IsOK() == false)
            {
                return false;
            }

            colorWeight.Red = dbRed.GetValue();
            colorWeight.Green = dbGreen.GetValue();
            colorWeight.Blue = dbBlue.GetValue();

            return true;
        }

        public override void ShowData()
        {
            if (IsNew)
            {
                dbRed.Value   = 100;
                dbGreen.Value = 100;
                dbBlue.Value  = 100;
            }
            else
            {
                dbRed.Value   = colorWeight.Red;
                dbGreen.Value = colorWeight.Green;
                dbBlue.Value  = colorWeight.Blue;
            }

            CalculateColorSlider();
        }

        private void colorSliderRed_ValuesChanged(object sender, EventArgs e)
        {
            dbRed.Value = (int) (colorSliderRed.Min / 255.0 * 100);
        }

        private void colorSliderGreen_ValuesChanged(object sender, EventArgs e)
        {
            dbGreen.Value = (int)(colorSliderGreen.Min / 255.0 * 100);
        }

        private void colorSliderBlue_ValuesChanged(object sender, EventArgs e)
        {
            dbBlue.Value = (int)(colorSliderBlue.Min / 255.0 * 100);
        }

        private void dbRed_Validating(object sender, CancelEventArgs e)
        {
            colorSliderRed.Min = (int)(dbRed.GetValue() * 255.0 / 100);
        }

        private void dbGreen_Validating(object sender, CancelEventArgs e)
        {
            colorSliderGreen.Min = (int)(dbGreen.GetValue() * 255.0 / 100);
        }

        private void dbBlue_Validating(object sender, CancelEventArgs e)
        {
            colorSliderBlue.Min = (int)(dbBlue.GetValue() * 255.0 / 100);
        }

        private void CalculateColorSlider()
        {
            colorSliderRed.Min = (int)(dbRed.GetValue() * 255.0 / 100);
            colorSliderGreen.Min = (int)(dbGreen.GetValue() * 255.0 / 100);
            colorSliderBlue.Min = (int)(dbBlue.GetValue() * 255.0 / 100);
        }
    }
}
