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


namespace WeatherForecast.Models.Requests.L10n {

  /// <summary>
  /// 
  /// </summary>
  public class CategoryL10nModel : RequestModelBase<CategoryL10n> {

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

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

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

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

      return new CategoryL10n() {
        Locale = Enumeration.FromDisplayName<Locales>(Locale),
        Slug = Slug,
        Text = Text
      };
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="validationContext"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    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(Slug))
        yield return new ValidationResult($"{nameof(Slug)} {Errors.NullOrWhiteSpace.Name}");

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

    // https://www.meziantou.net/csharp-8-nullable-reference-types.htm
    [MemberNotNullWhen(false, new[] { nameof(Locale), nameof(Slug), nameof(Text) })]
    private bool HasValidationErrors() => Validate(new ValidationContext(this)).Any();
  }
}
