using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PXP.Crawler.API.Model;
using PXP.Crawler.API.Plugins.Properties;
using Skybound.Gecko;

namespace PXP.Crawler.API.Plugins.Net.Chrome
{
    [NodeInfo("Get Children", typeof(ChromeElementGetChildrenNode), Category = "Chrome", Description = "Access element child nodes...")]
    [NodeInfo.NodeInfoIcon(typeof(Resources), "Children")]
    public class ChromeElementGetChildrenNode : ChromeElementNodeBase
    {
        public ChromeElementGetChildrenNode(Program program) : base(program)
        {
        }

        protected override void OnElementChanged()
        {
            base.OnElementChanged();
            ChildNodes = ChildNodes;
        }

        private GeckoNode firstElement;

        [NodePropertyPin(NodePinDirection.Output)]
        [Browsable(false)]
        public GeckoNode FirstElement
        {
            get{
                return firstElement;
            }
            set
            {
                firstElement = value;
                NotifyPropertyChange("FirstElement");
            }
        }

        [NodePropertyPin(NodePinDirection.Output)]
        [Browsable(false)]
        public List<GeckoNode> ChildNodes
        {
            get
            {
                List<GeckoNode> ret;
                    if (element != null)
                    {
                        ret = element.ChildNodes.ToList();
                    }
                    else
                    {
                        ret = new List<GeckoNode>();
                    }
                if (ret.Count > 0)
                {
                    FirstElement = ret[0];
                }
                else
                {
                    FirstElement = null;
                }
                return ret;
            }
            set
            {                
                NotifyPropertyChange("ChildNodes");
            }
        }
    }
}
