using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PXP.Crawler.API.Model;
using PXP.Crawler.API.Utils;

namespace PXP.Crawler.API.Plugins.Utils
{
    [NodeInfo("Log", typeof(LogNode), Category = "Utils",
        DefaultWidth = 80, DefaultHeight = 40, LockSize = true)]
    public class LogNode : Node
    {
        public LogNode(Program program) : base(program)
        {
            LogOnMessageChange = true;
        }

        public override void CreateDesigner()
        {
            base.CreateDesigner();
            designer.Control.BackColor = Color.Indigo;
        }
        private string logType = "info";

        private bool logOnMsgChange = true;

        [NodeSerializeProperty]
        public bool LogOnMessageChange
        {
            get
            {
                return logOnMsgChange;
            }
            set
            {
                logOnMsgChange = value;
            }
        }

        [NodeSerializeProperty]
        public string LogType
        {
            get { return logType; }
            set { logType = value; }        
        }

        [NodePin(NodePin.NodePinDirection.Input)]
        public bool DoLog
        {
            internal get
            {
                return false;
            }
            set
            {
                if (value)
                {
                    program.Log(logMsg, logType);
                }
            }
        }

        private string logMsg = "";

        [NodePin(NodePin.NodePinDirection.Input)]
        public string LogMessage
        {
            get
            {
                return logMsg;
            }
            internal set
            {
                string nv = Null.StringFromDBNull(value);
                if (nv.Length > 0)
                {
                    if (this.logMsg != nv)
                    {
                        logMsg = nv;

                        if (LogOnMessageChange)
                            program.Log(logMsg, logType);
                    }                    
                }
            }
        }
    }
}
