﻿namespace P08Timer
{
    using System;
    using System.Threading;

    public class Timer
    {
        private int secondsBetweenTicks;

        public Timer(int seconds)
        {
            this.SecondsBetweenTicks = seconds;
        }

        public delegate void TimerDelegate();

        public int SecondsBetweenTicks 
        {
            get
            {
                return this.secondsBetweenTicks;
            }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentOutOfRangeException("time must be possitive");
                }

                this.secondsBetweenTicks = value;
            }
        }

        public TimerDelegate TimerFunctions { get; set; }

        public DateTime StartTime { get; private set;  }

        public void Start()
        {
            this.StartTime = DateTime.Now;

            while (true)
            {
                this.TimerFunctions();
                Thread.Sleep(this.SecondsBetweenTicks * 1000);
            }
        }
    }
}