namespace PerpetuumSoft.Widgets
{
    using PerpetuumSoft.Widgets.Licensing;
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    [LicenseProvider(typeof(WidgetsLicenseProvider)), LicenseOption("Widgets")]
    public abstract class InstrumentationControl : Control
    {
        private PerpetuumSoft.Widgets.Bevel bevel = new PerpetuumSoft.Widgets.Bevel(BevelStyle.None, false, false);
        private PerpetuumSoftLicense license = null;
        private Timer timer = null;

        public InstrumentationControl()
        {
            base.SetStyle(0x2000, true);
            base.SetStyle(0x10000, true);
            base.SetStyle(2, true);
            base.SetStyle(0x10, true);
            base.SetStyle(0x800, true);
            base.SetStyle(0x200, false);
            this.ValidateLicense();
        }

        protected virtual void Draw(Graphics g, Rectangle r)
        {
            this.DrawBevel(g, r);
        }

        protected virtual void DrawBevel(Graphics g, Rectangle r)
        {
            Rectangle rectangle = this.get_DisplayRectangle();
            rectangle.Size -= new Size(1, 1);
            this.bevel.Draw(g, rectangle);
        }

        protected virtual void DrawFocus(Graphics g, Rectangle r)
        {
            int margin = this.bevel.GetMargin();
            Rectangle workRectangle = this.WorkRectangle;
            if (margin == 0)
            {
                workRectangle.Width--;
                workRectangle.Height--;
                Pen pen = new Pen(Color.Black);
                pen.DashStyle = (DashStyle) 2;
                g.DrawRectangle(pen, workRectangle);
            }
            else
            {
                workRectangle.X--;
                workRectangle.Y--;
                workRectangle.Width++;
                workRectangle.Height++;
                Pen pen2 = new Pen(Color.LightGreen);
                g.DrawRectangle(pen2, workRectangle);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            this.Draw(pe.Graphics, pe.ClipRectangle);
            if (this.Focused)
            {
                this.DrawFocus(pe.Graphics, pe.ClipRectangle);
            }
        }

        private void SetRegKey(string newRegKey)
        {
            if (base.get_DesignMode() && !LicensingUtilities.ValidRegKey(this.license, newRegKey))
            {
                this.license = LicensingUtilities.CheckRegKey(this.license, newRegKey);
                this.timer = new Timer();
                this.timer.Interval = 0x3e8;
                this.timer.Tick += new EventHandler(this.timer_Tick);
                this.timer.Enabled = true;
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.timer.Enabled = false;
            if ((this.Site != null) && this.Site.DesignMode)
            {
                IComponentChangeService service = (IComponentChangeService) this.Site.GetService(typeof(IComponentChangeService));
                if (service != null)
                {
                    service.OnComponentChanged(this, TypeDescriptor.GetProperties(this)["StringRegKey"], this.StringRegKey, this.StringRegKey);
                }
            }
        }

        private void ValidateLicense()
        {
            try
            {
                this.license = LicenseManager.Validate(typeof(InstrumentationControl), this);
            }
            catch (LicenseException)
            {
            }
        }

        [Editor(typeof(AboutEditor), typeof(UITypeEditor)), EditorBrowsable(EditorBrowsableState.Never)]
        public string About
        {
            get
            {
                return "(c) 2006 Perepetuum Software";
            }
        }

        [Description("Control's Bevel"), DesignerSerializationVisibility(2), Category("Appearance")]
        public PerpetuumSoft.Widgets.Bevel Bevel
        {
            get
            {
                return this.bevel;
            }
            set
            {
                this.bevel = value;
                base.Invalidate();
            }
        }

        [Browsable(false)]
        internal bool IsTrialLicence
        {
            get
            {
                if ((this.license != null) && !this.license.IsTrial)
                {
                    return this.license.IsExpire(typeof(InstrumentationControl));
                }
                return true;
            }
        }

        [Browsable(false), DefaultValue("")]
        public string StringRegKey
        {
            get
            {
                if (this.IsTrialLicence)
                {
                    return string.Empty;
                }
                return this.license.RegKey;
            }
            set
            {
                string newRegKey = string.Empty;
                if ((value != null) && (value != string.Empty))
                {
                    newRegKey = LicensingUtilities.DecodeLicenseKey(value);
                }
                this.SetRegKey(newRegKey);
                string stringRegKey = this.StringRegKey;
            }
        }

        protected virtual Rectangle WorkRectangle
        {
            get
            {
                Rectangle rectangle = this.get_DisplayRectangle();
                int margin = this.bevel.GetMargin();
                rectangle.Inflate(-margin, -margin);
                return rectangle;
            }
        }
    }
}
