using Core.Abstractions;
using Core.Abstractions.Models;
using Core.DomainObjects.L10n;
using Core.Enumerations;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;

namespace WeatherForecast.Models.Requests.L10n {

  /// <summary>
  /// 
  /// </summary>
  public class MediaAttachmentL10nModel : RequestModelBase<MediaAttachmentL10n> {

    /// <summary>
    /// 
    /// </summary>
    public string? Locale { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string? Alt { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string? Target { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string? Title { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override MediaAttachmentL10n ToDomainObject() {
      if (HasValidationErrors()) throw new ValidationException();

      return new MediaAttachmentL10n() {
        Locale = Enumeration.FromDisplayName<Locales>(Locale),
        Alt = Alt
      };
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="validationContext"></param>
    /// <returns></returns>
    public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
      if (string.IsNullOrWhiteSpace(Locale))
        yield return new ValidationResult($"{nameof(Locale)} {Errors.NullOrWhiteSpace.Name}");
      else if (Enumeration.FromDisplayName<Locales>(Locale) == null)
        yield return new ValidationResult($"{nameof(Locale)} {Errors.WrongOrNotManaged}");

      if (string.IsNullOrWhiteSpace(Alt))
        yield return new ValidationResult($"{nameof(Alt)} {Errors.NullOrWhiteSpace.Name}");
    }

    [MemberNotNullWhen(false, new[] { nameof(Locale), nameof(Alt) })]
    private bool HasValidationErrors() => Validate(new ValidationContext(this)).Any();
  }
}
