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

namespace PXP.Crawler.API.Plugins.Net.Chrome
{
    [NodeInfo("Set Attribute", typeof(ChromeElementSetAttribute), Category = "Chrome", Description = "Set element attribute value...")]
    public class ChromeElementSetAttribute : Node
    {

        private GeckoElement element;
        private string attName = "";
        private string val = "";

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

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

        private void OnElementChange()
        {
            if (element != null)
            {
                if (attName.Length > 0)
                {
                    element.SetAttribute(attName, val);

                    OnSet = true;
                }
            }
        }

        private bool setEnabled = false;

        [NodePin(NodePin.NodePinDirection.Input)]        
        public bool Enabled
        {
            get { return setEnabled; }

            set
            {
                if (setEnabled != value)
                {
                    setEnabled = value;
                    if (setEnabled)
                    {
                        OnElementChange();
                    }
                }
            }
        }

        [NodePin(NodePin.NodePinDirection.Input)]
        public GeckoNode Element
        {
            get
            {
                return element;
            }
            set
            {
                if (value is GeckoElement)
                {
                    element = (GeckoElement)value;                    
                } else
                {
                    element = null;
                }
                OnElementChange();
            }
        }

        [NodeSerializeProperty]
        [Browsable(true)]
        public string AttributeName
        {
            get
            {
                return attName;
            }
            set
            {
                attName = value;
                if (setEnabled)
                    OnElementChange();
            }
        }

        [NodePin(NodePin.NodePinDirection.Input)]
        public string Value{
            get
            {
                return val;
            }
            set
            {
                val = value;
                if (setEnabled)
                    OnElementChange();
            }
        }

        private bool onSet = false;

        [NodePin(NodePin.NodePinDirection.Output)]
        [Browsable(false)]
        public bool OnSet
        {
            get { return onSet; }
            internal set
            {
                if (value)
                {
                    onSet = true;
                    NotifyPropertyChange("OnSet");
                    onSet = false;
                }
            }
        }
    }
}
