using Hotline.Application.Exam.Interface.Strategy;

namespace Hotline.Application.Exam.Strategy
{
    public class ExamStrategyProxy
    {
        /// <summary>
        /// 当前策略
        /// </summary>
        private IExamStrategy _current;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; private set; }

        public ExamStrategyProxy()
        {
        }


        public bool Validate()
        {
            if(_current!=null)
            {

                if (_current.Validate())
                {
                    return true;
                }
                else
                {
                    ErrorMessage = _current.ErroMessage;                  

                    return false;
                }                
            }
            return true;
        }

        public object GetResult()
        {
            return _current.GetResult();
        }

        public void Initial(List<IExamStrategy> examStrategys)
        {
            if (examStrategys == null || examStrategys.Count <= 0) return;

            var current = examStrategys.FirstOrDefault();
            if (current != null)
            {
                for (var i = 1; i <= examStrategys.Count - 1; i++)
                {
                    current.SetNext(examStrategys[i]);
                    current = examStrategys[i];
                }
            }
            _current = examStrategys.FirstOrDefault();
        }
    }
}
