// 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;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Hiperscan.Unix;

namespace Hiperscan.Common
{
    public abstract class Polling
    {
        private enum PollingState
        {
            Stopped,
            Started,
            IsStopping,
        }

        public void StartPolling()
        {
            System.Threading.Tasks.Task.Run(() => this.DoStartPolling());
        }

        private void DoStartPolling()
        {
            lock (this.StateSync)
            {
                if (this.State == PollingState.Started)
                    return;

                if (this.AllowRestart == false)
                    return;

                if (this.AllowRestart &&  this.State == PollingState.IsStopping)
                {
                    this.State = PollingState.Started;
                        return;
                }

                this.BeforeStartRunning();
                this.State = PollingState.Started;
            }

            for (; ; )
            {
                lock (this.StateSync)
                {
                    if (this.State == PollingState.IsStopping)
                    {
                        this.AfterFinishRunning();
                        this.State = PollingState.Stopped;
                        break;
                    }
                }

                this.ExecutePollingEvent();
                System.Threading.Thread.Sleep(this.PollingTimeIntervallMilliSeconds);
            }
        }

        public void StopPolling()
        {
            System.Threading.Tasks.Task.Run(() => this.DoStopPolling());
        }

        private void DoStopPolling()
        {
            lock (this.StateSync)
            {
                if (this.State != PollingState.Started)
                    return;
                    
                this.State = PollingState.IsStopping;
            }

            for (; ; )
            {
                lock (this.StateSync)
                {
                    if (this.State != PollingState.IsStopping)
                        break;
                }

                System.Threading.Thread.Sleep(this.WaitTimeMilliSeconds);
            }
        }

        public void WaitForPollingEnd()
        {
            for (; ; )
            {
                lock(this.StateSync)
                {
                    this.AllowRestart = false;
                    if (this.State == PollingState.Stopped)
                        break;
                }

                this.StopPolling();
                System.Threading.Thread.Sleep(this.WaitTimeMilliSeconds);
            }
        }

        public void AllowRestartPolling()
        {
            lock (this.StateSync)
            {
                this.AllowRestart = true;
            }
        }

        public bool AllowRestart { get; set; } = true;

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

        protected int PollingTimeIntervallMilliSeconds { get; set; } = 100;
        protected int WaitTimeMilliSeconds { get; set; } = 20;

        private object StateSync { get; set; } = new object();
        private PollingState State { get; set; } = PollingState.Stopped;
    }
}
