using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using PXP.Crawler.API.Designers.Controls;
using PXP.Crawler.API.Model;
using PXP.Crawler.API.Utils;

namespace PXP.Crawler.API.Plugins.Values
{
    [NodeInfo("Button", typeof(ButtonNode), Category = "Values")]
    public class ButtonNode : NodeContainer
    {
        internal class SimpleButtonEx : SimpleButton
        {
            internal bool mouseEnabled = true;

            public SimpleButtonEx()
            {
                SetStyle(ControlStyles.Selectable, false);
                SetStyle(ControlStyles.StandardClick, false);
                SetStyle(ControlStyles.Opaque, true);

                UpdateStyles();
            }

            public bool MouseEnabled
            {
                get { return mouseEnabled; }
                set { mouseEnabled = value; }
            }

            protected override void WndProc(ref Message m)
            {
                if (mouseEnabled)
                {
                    base.WndProc(ref m);
                }
                else
                {
                    if (m.Msg == (int)Win32.WindowsMessage.NCHITTEST)
                    {
                        m.Result = (IntPtr)Win32.HitTest.TRANSPARENT;
                    }
                    else
                    {
                        base.WndProc(ref m);
                    }
                }

            }
        }

        internal SimpleButtonEx button;

        public ButtonNode(Program program) : base(program)
        {
            button = new SimpleButtonEx();
            base.Control = button;
            base.Description = "Button";
            button.ButtonStyle = BorderStyles.Simple;
            button.Text = base.Description;
            button.Cursor = Cursors.Hand;
            button.TabStop = false;
            button.Click += button_Click;
            button.MouseDown += button_MouseDown;
            button.MouseUp += ButtonOnMouseUp;
        }

        private void ButtonOnMouseUp(object sender, MouseEventArgs mouseEventArgs)
        {
            Down = false;
        }

        void button_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Down = true;
            }
        }

        void button_Click(object sender, EventArgs e)
        {
            //OnClick = true;
        }

        public override string Description
        {
            get
            {
                return base.Description;
            }
            set
            {
                base.Description = value;
                if (designer != null)
                {
                    button.Text = base.Description;
                }
            }
        }

        public override void CreateDesigner()
        {
            padd = new Padding(8);
            base.CreateDesigner();
            designer.Control.BackColor = Color.DarkGreen;
            ((NodeDesignerControl)designer.Control).ShowTitle = false;
            ((NodeDesignerControl)designer.Control).SelectedChanged += ButtonNode_SelectedChanged;
            button.Font = designer.Control.Font;
            button.Text = base.Description;
            button.Appearance.BackColor = Misc.InterpolateColor(0.5f, designer.Control.BackColor, Color.White);
            button.Appearance.BorderColor = Misc.InterpolateColor(0.7f, designer.Control.BackColor, Color.Black);
            button.Appearance.Options.UseBorderColor = true;
        }

        void ButtonNode_SelectedChanged(object sender, EventArgs e)
        {
            NodeContainerDesignerControl ncdCont = (NodeContainerDesignerControl) designer.Control;
            bool isSEl = ncdCont.IsSelected;
            ncdCont.DesignerContainer.MouseEnabled = !isSEl;
            button.mouseEnabled = !isSEl;

        }

        private bool onDown = false;
        
        [NodePin(NodePin.NodePinDirection.Output)]
        public bool Down
        {
            get
            {
                return onDown;
            }
            internal set
            {
                if (value != onDown)
                {
                    onDown = value;
                    NotifyPropertyChange("Down");
                    /*onClick = true;
                    NotifyPropertyChange("OnClick");
                    onClick = false;*/
                }
            }
        }        
    }
}
