using Core.DomainObjects.Documents;
using Core.Enumerations;
using Extensions;
using System.ComponentModel.DataAnnotations;
using WeatherForecast.Models.Abstractions;

namespace WeatherForecast.Models.Requests {

  /// <summary>
  /// 
  /// </summary>
  public class BlogItemRequestModel : PostItemRequestModelBase<BlogItem> {

    /// <summary>
    /// 
    /// </summary>
    public uint? ReadTime { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public uint? Likes { get; set; }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override BlogItem ToDomainObject() {
      if (HasValidationErrors(this) || HasValidationErrorsBase(HasValidationErrors(this)))
        throw new ValidationException();

      return new BlogItem() {
        L10n = L10n.Select(x => x.ToDomainObject()).ToList(),

        // Images
        // Author

        Tags = Tags,
        Categories = Categories,
        FamilyFriendly = FamilyFriendly,

        ReadTime = ReadTime,
        Likes = Likes
      };
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="validationContext"></param>
    /// <returns></returns>
    public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
      if (L10n.IsNullOrEmpty())
        yield return new ValidationResult($"{nameof(L10n)} {Errors.NullOrEmpty}");

      if (MediaAttachments.IsNullOrEmpty())
        yield return new ValidationResult($"{nameof(MediaAttachments)} {Errors.NullOrEmpty}");
    }

    private bool HasValidationErrors(BlogItemRequestModel model) => Validate(new ValidationContext(model)).Any();
  }
}
