using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PXP.Crawler.API.Designers.Property;
using PXP.Crawler.API.Model;
using Skybound.Gecko;

namespace PXP.Crawler.API.Plugins.Net.Chrome
{
    [NodeInfo("Element", typeof(ChromeElement), Category = "Chrome", Description = "Element utils control...")]
    public class ChromeElement : Node
    {
        private GeckoElement element;
        private string tagName = "";
        private string _innerHtml = "";
        private string _outterHtml = "";

        public ChromeElement(Program program) : base(program)
        {
            }

        public override void CreateDesigner()
        {
            base.CreateDesigner();
            base.designer.Control.BackColor = Color.Peru;
        }

        private void OnElementChange()
        {
            if (element == null)
            {
                TagName = "";
                OutterHtml = "";
            }
            else
            {
                TagName = element.TagName;

                InnerHtml = element.InnerHtml;

                if (element.Parent != null)
                {
                    OutterHtml = element.Parent.InnerHtml;
                }
                else
                {
                    OutterHtml = InnerHtml;
                }
            }
            
            NotifyPropertyChange("ChildNodes");
        }
        
        [NodePin(NodePin.NodePinDirection.Input)]
        [Browsable(false)]
        public GeckoNode Element
        {
            get
            {
                return element;
            }
            set
            {
                if (value is GeckoElement)
                {
                    element = (GeckoElement) value;
                }
                else
                {
                    element = null;
                }
                OnElementChange();
            }
        }
        
        [NodePin(NodePin.NodePinDirection.Output)]
        public string TagName
        {
            get { return tagName; }
            set
            {
                tagName = value;
                NotifyPropertyChange("TagName");
            }
        }

            
        [NodePin(NodePin.NodePinDirection.Output)]
        public string OutterHtml
        {
            get { return _outterHtml; }
            set
            {
                _outterHtml = value;
                NotifyPropertyChange("OutterHtml");
            }
        }


        [NodePin(NodePin.NodePinDirection.Output)]
        public string InnerHtml
        {
            get { return _innerHtml; }
            set
            {
                _innerHtml = value;
                NotifyPropertyChange("InnerHtml");
            }
        }
    }
}
