using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Quadarax.Foundation.Core.Exceptions;

namespace Quadarax.Foundation.Core.Data.Interface.Entity.Dto
{
    public class ResultValueDto<TDto> : ResultDto where TDto : IDto, new()
    {
        [JsonPropertyName("value")]
        public TDto Value { get; set; }


        public ResultValueDto(TDto value) : this()
        {
            Value = value;
        }

        public ResultValueDto() : base(new List<IError>())
        {
            Value = new TDto();
        }
        public ResultValueDto(CodeException[] exceptions) : base(exceptions.Select(x => new ErrorMessageDto(x)))
        {
        }

        public ResultValueDto(Exception exception) : base( new []{ exception is CodeException codeException ? new ErrorMessageDto(codeException) : new ErrorMessageDto(exception, -1)})
        {
        }

        public override object GetValue()
        {
            return Value;
        }
        public TDto GetValueAs()
        {
            return Value;
        }
    }
}
