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 System.Xml;
using PXP.Crawler.API.Designers;
using PXP.Crawler.API.Designers.Controls;
using PXP.Crawler.API.Designers.Forms;
using PXP.Crawler.API.Model;
using PXP.Crawler.API.Plugins.Net.Chrome.Controls;
using PXP.Crawler.API.Plugins.Properties;
using PXP.Crawler.API.Utils;
using Skybound.Gecko;

namespace PXP.Crawler.API.Plugins.Net.Chrome
{
    [NodeInfo("Chrome", 
        Category = "Chrome",
        Description = "Chrome Browser",       
        DefaultWidth = 430, DefaultHeight = 200)]
    [NodeInfo.NodeInfoIcon(typeof(Resources), "Chrome")]
    public class ChromeNode : Node
    {
        #region Variables

        private NodeContainerDesigner nodeDesigner;
        internal ChromeControl chrome;

        #endregion

        #region Constructor

        public ChromeNode(Program program) : base(program)
        {
            nodeForm = new NodeForm(this);
            chrome = new ChromeControl(this);
            chrome.webBrowser.Visible = true;
            chrome.webBrowser.HandleCreated += chrome_HandleCreated;
            nodeForm.Controls.Add(chrome);
        }

        public override void Dispose()
        {
            chrome.Dispose();
            base.Dispose();
        }

        #endregion

        #region Internal Events
        private void chrome_HandleCreated(object sender, EventArgs e)
        {
            if (chrome.webBrowser.Url == null)
            {
                chrome.webBrowser.Visible = true;
                chrome.webBrowser.Navigate(url);
            }
            else if (chrome.webBrowser.Url.ToString() != url)
            {
                chrome.webBrowser.Visible = true;
                chrome.webBrowser.Navigate(url);
            }
        }

        #endregion

        #region Overrides

        protected override void OnDesignerBeginSizing()
        {
            base.OnDesignerBeginSizing();
            nodeDesigner.ContainerControl.DesignerContainer.Visible = false;
        }

        protected override void OnDesignerEndSizing()
        {
            base.OnDesignerEndSizing();
            nodeDesigner.ContainerControl.DesignerContainer.Visible = true;
        }

        public override void CreateDesigner()
        {
            nodeDesigner = new NodeContainerDesigner(this);
            designer = nodeDesigner;
            nodeDesigner.Control.BackColor = Color.Peru;
            nodeDesigner.ContainerControl.ContainerPadding = new Padding(10, 30, 10, 10);
            
            nodeForm.Controls.Remove(chrome);
            nodeDesigner.ContainerControl.DesignerContainer.Controls.Add(chrome);

            if (chrome.webBrowser.IsHandleCreated)
            {
                chrome.webBrowser.Visible = true;
                chrome.webBrowser.Navigate(url);
            }            
        }

        public override void DestroyDesigner()
        {
            base.DestroyDesigner();
            nodeForm.Controls.Add(chrome);
        }
        
        #endregion

        #region Properties

        internal bool docLoaded = false;

        [NodePropertyPin(NodePinDirection.Output)]
        [Browsable(false)]
        public ChromeControl Chrome
        {
            get
            {
                return chrome;
            }
        }

        [NodePropertyPin(NodePinDirection.Output)]
        [Browsable(false)]
        public bool DocumentLoaded
        {
            get { return docLoaded; }
        }


        private string url = "";

        [NodePropertyPin(NodePinDirection.Input)]
        public string URL
        {
            get
            {
                return url;
            }
            set
            {
                if (url != value)
                {
                    url = value;
                    if (chrome.webBrowser.IsHandleCreated)
                    {
                        chrome.webBrowser.Visible = true;
                        chrome.webBrowser.Navigate(url);
                        NotifyPropertyChange("URL");
                    }
                }
            }
        }

        #endregion        
    }
}
