using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PXP.Crawler.API.Designers.Controls;
using PXP.Crawler.API.Model;

namespace PXP.Crawler.API.Plugins.Values.Controls
{
    public partial class NumberEditUserControl : UserControl
    {
        internal Node node;

        public NumberEditUserControl()
        {
            InitializeComponent();
        }

        public NumberEditUserControl(Node node)
        {
            this.node = node;
            InitializeComponent();
        }


        private Point mdPos = new Point();
        private bool mouseScrolling = false;

        private void valueSpinEdit_MouseDown(object sender, MouseEventArgs e)
        {
            NodeDesignerControl ndc = (NodeDesignerControl)node.Designer.Control;
            if (e.Button == MouseButtons.Left)
            {
                if (!ndc.IsSelected)
                {
                    ndc.DoSelect();
                }
            }

            if (e.Button == MouseButtons.Middle)
            {
                if (ndc.IsSelected)
                {
                    mdPos = Cursor.Position;
                    mouseScrolling = true;                    
                }
            }
        }

        private void valueSpinEdit_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Middle)
            {
                if (mouseScrolling)
                {
                    Point md = new Point(mdPos.X - Cursor.Position.X, mdPos.Y - Cursor.Position.Y);
                    if (md.X != 0 || md.Y != 0)
                    {
                        if (valueSpinEdit.Properties.IsFloatValue)
                        {
                            valueSpinEdit.Value =Convert.ToDecimal( Convert.ToSingle(valueSpinEdit.Value) + md.Y / 100.0f);
                        }
                        else
                        {
                            valueSpinEdit.Value += md.Y / 3;
                        }
                        Cursor.Position = mdPos;
                    }
            }
            }
        }

        private void valueSpinEdit_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Middle)
            {
                if (mouseScrolling)
                {
                    Cursor.Show();
                    mouseScrolling = false;                    
                }
            }
        }

        private void valueSpinEdit_MouseLeave(object sender, EventArgs e)
        {
            
        }
    }
}
