using System;
using System.Windows.Forms;
using ImageBlockz.Core;
using ImageBlockz.Api;

namespace ImageBlockz.Operations
{
    public partial class GradientControl : OperationControl
    {
        private Gradient gradient;

        public GradientControl()
            : base(typeof(Gradient).FullName)
        {
            IsPreview = true;
            InitializeComponent();

            dbFactor.SetDigitBox(DigitBox.ValueType.Dec, 0, 2, 1);
        }

        public override IOperation GetOperation()
        {
            gradient.Factor = dbFactor.GetValue();

            gradient.IsRedChecked = cbRed.Checked;
            gradient.IsGreenChecked = cbGreen.Checked;
            gradient.IsBlueChecked = cbBlue.Checked;

            return gradient;
        }

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

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

            gradient = (Gradient)operation;
        }

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

            gradient.Factor = dbFactor.GetValue();

            gradient.IsRedChecked = cbRed.Checked;
            gradient.IsGreenChecked = cbGreen.Checked;
            gradient.IsBlueChecked = cbBlue.Checked;

            return true;
        }

        public override void ShowData()
        {
            if (IsNew)
            {
               dbFactor.Value = 1;
               cbRed.Checked = true;
               cbGreen.Checked = true;
               cbBlue.Checked = true;

            }
            else
            {
                dbFactor.Value = gradient.Factor;
                cbRed.Checked = gradient.IsRedChecked;
                cbGreen.Checked = gradient.IsGreenChecked;
                cbBlue.Checked = gradient.IsBlueChecked;
            }
        }
    }
}
