using System;
using System.Collections.Generic;
using System.Linq;
using Quadarax.Foundation.Core.Exceptions;

namespace Quadarax.Foundation.Core.Data.Interface.Entity.Dto
{
    public abstract class ResultDto : IDto, IResult
    {
        /// <summary>
        /// <inheritdoc cref="IResult.IsSuccess"/>
        /// </summary>
        public bool IsSuccess => !Errors.Any();
        /// <summary>
        /// <inheritdoc cref="IResult.Errors"/>
        /// </summary>
        public IEnumerable<ErrorMessageDto> Errors { get; set; }
        //public IEnumerable<IError> Errors { get; set; }

        IEnumerable<IError> IResult.Errors => Errors;

        /// <summary>
        /// <inheritdoc cref="IResult.GetValue()"/>
        /// </summary>
        /// <returns></returns>
        public abstract object GetValue();

        protected ResultDto(IEnumerable<IError> errors)
        {
             Errors = new List<ErrorMessageDto>(errors.Select(x=>(ErrorMessageDto) x));
            //Errors = new List<IError>(errors);
        }
        protected ResultDto()
        {
            Errors = new List<ErrorMessageDto>();
        }
        public AggregateException ToAggregateException()
        {
            if (IsSuccess)
                return null;
            var errors = new List<Exception>();
            foreach (var e in Errors)
            {
                var addExc = new Exception(e.Message);
                addExc.SetCode(e.Code);
                errors.Add(addExc);
            }
            return new AggregateException(errors);
        }
        
    }
}
