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 VisiMath;

namespace VisimBuilder
{
    public partial class ColorEditing : UserControl
    {
        
        public bool AlphaStateEnabled
        {
            get
            {
                return alphaStateEnabled;
            }
            set
            {
                alphaStateEnabled = value;
                textBoxAlpha.Enabled = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ColorByte EditedColor
        {
            get
            {
                byte alpha;
                if (alphaStateEnabled)
                    alpha = myAlpha;
                else alpha = 255;
                
                return new ColorByte(MyColor.R, MyColor.G, MyColor.B, alpha);
            }

            set
            {
                MyColor = Color.FromArgb(value.R, value.G, value.B);
                myAlpha = value.A;
                UpdateFromMyColor();
            }
        }
        
        Color MyColor = Color.White;
        byte myAlpha = 255;

        bool alphaStateEnabled = true;


        public ColorEditing()
        {
            InitializeComponent();
            UpdateFromMyColor();
        }

        private void PickColor(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() != DialogResult.OK)
                return;
            
            Color newc = colorDialog1.Color;
            if (newc != MyColor)
            {
                MyColor = newc;
            }

            if (ColorChanged != null)
            {
                ColorChanged(this, new ColorByte(MyColor.R, MyColor.G, MyColor.B, myAlpha));
            }


            UpdateFromMyColor();
        }

        private void UpdateFromMyColor()
        {
            picturePreview.BackColor = MyColor;
            textBoxBlue.Text = MyColor.B.ToString();
            textBoxRed.Text = MyColor.R.ToString();
            textBoxGreen.Text = MyColor.G.ToString();

            textBoxAlpha.Text = myAlpha.ToString();
            textBoxAlpha.ReadOnly = !alphaStateEnabled;
        }

        private byte CheckTextBox(TextBox tb, byte original)
        {
            byte val;

            try
            {
                val = Convert.ToByte(tb.Text);
            }
            catch
            {
                tb.Text = original.ToString();
                return original;
            }
            return val;
        }

        private void CheckUserChanges()
        {
            bool changed = false;
            byte al = CheckTextBox(textBoxAlpha, myAlpha);
            if (al != myAlpha)
            {
                myAlpha = al;
                changed = true;
            }
            byte red = CheckTextBox(textBoxRed, MyColor.R);
            if (red != MyColor.R)
                changed = true;
            byte green = CheckTextBox(textBoxGreen, MyColor.G);
            if (green != MyColor.G)
                changed = true;
            byte blue = CheckTextBox(textBoxBlue, MyColor.B);
            if (blue != MyColor.B)
                changed = true;

            if (!changed)
                return;
            MyColor = Color.FromArgb(red, green, blue);

            if (ColorChanged != null)
            {
                ColorChanged(this, new ColorByte(red, green, blue, al));
            }

            UpdateFromMyColor();
        }

        private void FocusLeftKey(object sender, EventArgs e)
        {
            CheckUserChanges();
        }

        private void ValKeyPres(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                CheckUserChanges();
            }
        }

        public delegate void ColorChangedCallback(ColorEditing sender, ColorByte newcolor);

        public event ColorChangedCallback ColorChanged;

        private void EnableChange(object sender, EventArgs e)
        {
            if (Enabled)
                picturePreview.BackColor = MyColor;
            else
                picturePreview.BackColor = Color.Black;
        }
    }
}
