using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Utils.Paint;
using PXP.Crawler.API.Model;
using PXP.Crawler.API.Plugins.Net.Chrome.Controls;
using Skybound.Gecko;

namespace PXP.Crawler.API.Plugins.Net.Chrome
{
    [NodeInfo("Selector", typeof(ChromeElementSelectorNode), 
        Category = "Chrome", Description = "Select document elements...")]
    public class ChromeElementSelectorNode : Node
    {
        public enum ElementSelectionMode : int
        {
            ByID = 0,
            ByClassName,
            ByTag
        }
        
        internal ChromeControl chrome;
        internal ElementSelectionMode selMode = ElementSelectionMode.ByID;
        internal string selValue = "";
        internal List<GeckoNode> selElements = new List<GeckoNode>();
        private GeckoElement parentElement = null;

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

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

        internal void OnDoSelect()
        {
            if (chrome != null)
            {
                selElements.Clear();
                switch (selMode)
                {
                    case ElementSelectionMode.ByID:
                        GeckoElement idEl = chrome.webBrowser.Document.GetElementById(selValue);
                        if (idEl != null)
                        {
                            selElements.Add(idEl);
                        }
                        break;
                    case ElementSelectionMode.ByClassName:
                        if (parentElement != null)
                        {
                            foreach (GeckoElement ge in parentElement.GetElementsByClassName(selValue))
                            {
                                selElements.Add(ge);
                            }
                        }
                        else
                        {
                            foreach (GeckoElement ge in chrome.webBrowser.Document.GetElementsByClassName(selValue))
                            {
                                selElements.Add(ge);
                            }                            
                        }
                        break;
                }
                Elements = selElements;
            }
        }

        [NodeSerializeProperty]
        public ElementSelectionMode SelectionMode
        {
            get
            {
                return selMode;
            }
            set
            {
                selMode = value;
                OnDoSelect();
            }
        }

        [NodeSerializeProperty]
        public string Value
        {
            get { return selValue; }
            set
            {
                selValue = value;
                OnDoSelect();
            }
        }               

        [NodePin(NodePin.NodePinDirection.Output)]
        [Browsable(false)]
        public GeckoNode FirstElement
        {
            get
            {
                if (selElements.Count == 0)
                    return null;

                return selElements[0];
            }
            set
            {                
                NotifyPropertyChange("FirstElement");
            }
        }

        [NodePin(NodePin.NodePinDirection.Output)]
        [Browsable(false)]
        public List<GeckoNode> Elements
        {
            get
            {
                return selElements;
            }
            set
            {
                selElements = value;
                NotifyPropertyChange("Elements");
                
                FirstElement = null;
            }
        }

        [NodePin(NodePin.NodePinDirection.Input)]
        [Browsable(false)]
        public ChromeControl Chrome
        {
            get
            {
                return chrome;
            }
            set
            {
                chrome = value;                
                OnDoSelect();
            }
        }

        [NodePin(NodePin.NodePinDirection.Input)]
        [Browsable(false)]
        public GeckoElement ParentElement
        {
            get { return parentElement; }
            set
            {
                parentElement = value;
                OnDoSelect();
            }
        }
    }
}
