using Hotline.Application.Exam.Interface.Strategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotline.Application.Exam.Strategy
{
    public class CheckValidateTimeStrategy : IExamStrategy
    {
        public string ErroMessage { get; private set; }

        public Func<object> CallBack { get; set; }

        private IExamStrategy _next;

        private DateTime? _startTime;

        private int _timeSpan;

        private IExamStrategy _current;


        public CheckValidateTimeStrategy(int timeSpan, DateTime? startTime)
        {
            _startTime = startTime;
            _timeSpan = timeSpan;
        }


        public void SetNext(IExamStrategy examStrategy)
        {
            _next = examStrategy;
        }

        public bool Validate()
        {
            if (_startTime.Value.AddMinutes(_timeSpan) < DateTime.Now)
            {
                ErroMessage = "考试已超时，考试结束";
                _current = this;
                return false;
            }
            _current = _next;

            if (_next == null) return true;

            return _next.Validate();
        }



        public object GetResult()
        {
            if (_current != null && _current.CallBack != null)
                return _current.CallBack();

            return null;
        }
    }
}
