using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PXP.Crawler.API.Designers.Controls;
using PXP.Crawler.API.Model;
using PXP.Crawler.API.Plugins.Values.Controls;
using PXP.Crawler.API.Utils;

namespace PXP.Crawler.API.Plugins.Values
{
    public class StringNodeBase : NodeContainer
    {
        protected internal StringEditUserControl valControl;
        protected internal bool valuePinConnected = false;

        public StringNodeBase(Program program) : base(program)
        {
            valControl = new StringEditUserControl();
            base.Control = valControl;
            InitValueControl();
        }

        protected virtual void InitValueControl()
        {
            valControl.valueTextEdit.Validated += valueTextEdit_Validated;
        }        

        void valueTextEdit_Validated(object sender, EventArgs e)
        {
            Value = valControl.valueTextEdit.Text;
        }

        protected virtual void UpdateDesignerControls()
        {
            if (valuePinConnected)
            {
                valControl.Enabled = false;
                valControl.valueTextEdit.BackColor = Misc.InterpolateColor(0.5f, designer.Control.BackColor, Color.Gray);
                valControl.valueMemoEdit.BackColor = valControl.valueTextEdit.BackColor;
                valControl.valueTextEdit.Properties.Appearance.ForeColor = Color.White;
                valControl.valueMemoEdit.Properties.Appearance.ForeColor = Color.White;
            }
            else
            {
                valControl.Enabled = true;
                valControl.valueTextEdit.BackColor = Color.White;
                valControl.valueMemoEdit.BackColor = Color.White;
            }
        }

        public override void OnInputPinConnected(string name)
        {
            base.OnInputPinConnected(name);
                if (name == "Value")
                {
                    valuePinConnected = true;
                    if (designer != null)
                    {
                        UpdateDesignerControls();
                    }
            }
        }

        public override void OnInputPinDisconnected(string name)
        {
            base.OnInputPinDisconnected(name);
            if (name == "Value")
            {
                valuePinConnected = false;
                if (designer != null)
                {
                    UpdateDesignerControls();
                }
            }
        }

        public override void CreateDesigner()
        {
            padd = new Padding(8, 22, 8, 8);
            
            base.CreateDesigner();
            designer.Control.BackColor = Color.DarkGreen;
            NodeDesignerControl ndCont = (NodeDesignerControl) designer.Control;
            UpdateDesignerControls();
            ndCont.TitlePositionAdjust = new Point(-6, -4);
            Value = Value;
        }

        [NodePropertyPin]
        public string Value
        {
            get
            {
                return valControl.valueTextEdit.Text;
            }
            set
            {
                if (valControl.valueTextEdit.Text != value)
                {
                    valControl.valueTextEdit.Text = value;
                }
                NotifyPropertyChange("Value");
            }
        }
    }
}
