// Created with MonoDevelop
//
//    Copyright (C) 2021 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
namespace Hiperscan.Common
{
    public abstract class Polling
    {
        public void StartPolling()
        {
            lock (this.Sync)
            {
                if (this.IsRunning || this.CanPolling == false)
                    return;

                this.IsRunning = true;
                this.RunningStopped = false;
            }

            System.Threading.Tasks.Task.Run(() => this.Run());
        }

        public void StopPolling()
        {
            lock (this.Sync)
            {
                if (this.IsRunning == false || this.CanPolling == false)
                    return;

                this.IsRunning = false;
            }


            for (; ; )
            {
                lock (this.Sync)
                {
                    if (this.RunningStopped)
                        break;
                }

                System.Threading.Thread.Sleep(20);
            }
        }

        private void Run()
        {
            this.BeforeStartRunning();


            bool polling_finsihed = false;

            for (; ; )
            {
                DateTime next_poll = DateTime.Now.AddMilliseconds(this.PollingTimeIntervall);

                for (; ; )
                {
                    if (DateTime.Now < next_poll)
                    {
                        lock (this.Sync)
                        {
                            if (this.IsRunning == false)
                            {
                                polling_finsihed = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(20);
                }

                if (polling_finsihed)
                    break;

                if (this.CycleIsRunning)
                    continue;

                if (this.ExecutePoolingEventAsyncronously == false)
                    this.ExecutePollingEvent();
                else
                    System.Threading.Tasks.Task.Run(() => this.ExecutePollingEvent());
            }

            lock (this.Sync)
            {
                this.RunningStopped = true;
            }

            this.AfterFinishRunning();
        }

        protected abstract void BeforeStartRunning();
        protected abstract void ExecutePollingEvent();
        protected abstract void AfterFinishRunning();

        private bool IsRunning { get; set; } = false;
        private bool RunningStopped { get; set; } = true;
        protected bool CanPolling { get; set; }
        protected int PollingTimeIntervall { get; set; } = 100;
        public bool ExecutePoolingEventAsyncronously { get; set; } = false;
        public bool CycleIsRunning { protected get; set; } = false;

        private object Sync { get; set; } = new object();

    }
}
