using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIControl
{
    [TypeConverterAttribute(typeof(ExpandableObjectConverter))]
    public class PinInfo
    {
        public int PinIndex { get; set; }
        public Boolean PinLevel { get; set; }
        public Boolean Output { get; set; }
        public eValueDirection ValueDirection { get; set; }
        public Boolean Value
        {
            get
            {
                if (PinLevel == false) return !Raw;
                else return Raw;
            }
        }

        private Boolean _raw = false;
        public Boolean Raw
        {
            get { return _raw; }
            set
            {
                Boolean changed = _raw != value;
                _raw = value;
                if (changed && ValueChanged != null)
                {
                    ValueChanged(this, new EventArgs());
                }
            }
        }
        public PinInfo(Boolean isOutput = false)
        {
            _raw = false;
            PinIndex = -1;
            PinLevel = true;
            Output = isOutput;
            ValueDirection = eValueDirection.input;
        }

        public event EventHandler ValueChanged;
    }


    public enum eValueDirection
    {
        input = 0,
        output,
    }
    public abstract partial class CtlBase : Control
    {
        Boolean bRemakeRect ;


        public List<PinInfo> PinList;
        public CtlBase()
        {
            InitializeComponent();

            // Set Optimized Double Buffer to reduce flickering
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.ContainerControl, false);
            this.SetStyle(ControlStyles.Selectable, true);
            PinList = new List<PinInfo>();
            bRemakeRect = true;
            this.Resize += Loader_Resize;
        }

        public abstract void MakeRect();
        public abstract void UpdateValue();
        protected void SetPinCount(int iCnt)
        {
            this.PinList = new List<PinInfo>(iCnt);

            for (int i = 0; i < iCnt; i++)
                PinList.Add(new PinInfo());

        }

        void Loader_Resize(object sender, EventArgs e)
        {
            bRemakeRect = true;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            if (bRemakeRect)
            {
                MakeRect();
                bRemakeRect = false;
            }

            base.OnPaint(pe);
        }
    }
}
