using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using PXP.Crawler.API.Model;

namespace PXP.Crawler.API.Designers.Controls
{
    public enum NodeDesignerPinType
    {
        Property,
        Method,
        Event
    }
    
    public class NodeDesignerPin
    {
        internal int id = 0;
        internal NodeDesignerControl designerControl;
        internal Node node;
        internal Point pos;
        internal Rectangle pinRect = new Rectangle();
        internal string name = "";
        internal string info = "";
        internal bool isOutput = false;

        public static int pinSize = 4;
        private static GraphicsPath pinPath;        

        internal NodeDesignerPinType type = NodeDesignerPinType.Property;
        internal Type pinType;

        internal Dictionary<int, NodeDesignerPin> connPins = new Dictionary<int, NodeDesignerPin>();
        internal bool isConnected = false;

        public void AddConnPin(NodeDesignerPin pin)
        {
            if (!connPins.ContainsKey(pin.id))
                connPins.Add(pin.id, pin);
            isConnected = connPins.Count > 0;
        }

        public void RemoveConnPin(NodeDesignerPin pin)
        {
            if (connPins.ContainsKey(pin.id))
                connPins.Remove(pin.id);
            isConnected = connPins.Count > 0;
        }

        public NodeDesignerPin(NodeDesignerControl ndCont)
        {
            pos = new Point();
            designerControl = ndCont;
            node = designerControl.node;
            pinType = typeof(NodeDesignerPin);
            UpdatePinBounds();

            if (pinPath == null)
            {
                pinPath = new GraphicsPath(new Point[]
                {
                    new Point(0, pinSize),
                    new Point(-pinSize - 1, -pinSize), 
                    new Point(pinSize + 1, -pinSize), 
                    new Point(0, pinSize)
                }, new byte[]
                {
                    (byte)PathPointType.Line,
                    (byte)PathPointType.Line,
                    (byte)PathPointType.Line,
                    (byte)PathPointType.Line
                });                
            }
        }

        internal void UpdatePinBounds()
        {
            UpdatePinBounds(new Point());
        }
        
        internal void UpdatePinBounds(Point posAdjust)
        {
            int ps = 6;
            pinRect.X = pos.X - ps + posAdjust.X - 1;
            pinRect.Y = pos.Y - ps + posAdjust.Y;
            pinRect.Width = ps * 2 + 2;
            pinRect.Height = pinRect.Width;
        }

        public void Draw(Graphics g)
        {
            g.ResetTransform();
            //g.DrawRectangle(Pens.Coral, pinRect);
            
            if (isOutput)
            {
                g.TranslateTransform(0, -4);
                if (node.program.designer.availableConnPins.ContainsKey(id))
                {
                    g.FillEllipse(Brushes.DeepSkyBlue, pinRect);
                    g.DrawEllipse(Pens.DarkSlateBlue, pinRect);
                }
            }
            else
            {
                g.TranslateTransform(0, -4);
                if (node.program.designer.availableConnPins.ContainsKey(id))
                {
                    g.FillEllipse(Brushes.MediumSpringGreen, pinRect);
                    g.DrawEllipse(Pens.DarkSlateGray, pinRect);
                }                
            }
            

            g.ResetTransform();
            g.TranslateTransform(pos.X, pos.Y);
            

            if (isOutput)
            {
                g.FillPath(Brushes.LightBlue, pinPath);
            }
            else
            {
                g.FillPath(Brushes.SeaGreen, pinPath);
            }

            g.DrawPath(Pens.Black, pinPath);
        }

        public Type PinType
        {
            get
            {
                return pinType;
            }
            set
            {
                if (pinType != value)
                {                    
                    //node.DisconnectOutProperty(name);
                    pinType = value;
                    if (pinType != null)
                    {
                        info = "<b>Type: </b>" + pinType.Name.Replace(pinType.Namespace, "");
                    }
                    else
                    {
                        info = "<b>Type: </b> None";
                    }
                }
            }
        }
    }
}
