namespace PerpetuumSoft.Widgets
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    public abstract class Clock : InstrumentationControl
    {
        private DateTime clockTime = DateTime.Now;
        private bool showSecond = true;
        private Timer timer = new Timer();

        public Clock()
        {
            this.timer.Tick += new EventHandler(this.timer_Tick);
            this.timer.Interval = 0x3e8;
            this.timer.Enabled = true;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.timer != null))
            {
                this.timer.Dispose();
                this.timer = null;
            }
            base.Dispose(disposing);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            if (((now.Hour != this.clockTime.Hour) || (now.Minute != this.clockTime.Minute)) || (this.showSecond && (now.Second != this.clockTime.Second)))
            {
                DateTime clockTime = this.clockTime;
                this.clockTime = now;
                this.UpdateTime(clockTime, this.clockTime);
            }
        }

        protected virtual void UpdateTime(DateTime oldTime, DateTime newTime)
        {
        }

        protected string ClockText
        {
            get
            {
                if (this.showSecond)
                {
                    return this.ClockTime.ToString("hh:mm:ss");
                }
                return this.ClockTime.ToString("hh:mm");
            }
        }

        protected DateTime ClockTime
        {
            get
            {
                return this.clockTime;
            }
        }

        [Category("Appearance"), DefaultValue(true), Description("Show second section in clock")]
        public bool ShowSecond
        {
            get
            {
                return this.showSecond;
            }
            set
            {
                if (this.showSecond != value)
                {
                    this.showSecond = value;
                    base.Invalidate();
                }
            }
        }
    }
}
