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 PostItemL10nModel : RequestModelBase<PostItemL10n> {

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

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

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

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

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

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

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

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

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

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

      return new PostItemL10n() {
        Locale = Enumeration.FromDisplayName<Locales>(Locale),
        Slug = Slug,
        Description = Description,
        Title = Title,
        Text = Text,
        ShortText = ShortText,

        // TODO: create plain text creation logic
        PlainText = "TODO",

        TextFormat = Enumeration.FromDisplayName<TextFormat>(TextFormat),
        Badges = Badges
      };
    }

    /// <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(Slug))
        yield return new ValidationResult($"{nameof(Slug)} {Errors.NullOrWhiteSpace.Name}");

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

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

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

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

      if (string.IsNullOrWhiteSpace(TextFormat))
        yield return new ValidationResult($"{nameof(TextFormat)} {Errors.NullOrWhiteSpace.Name}");
      else if (Enumeration.FromDisplayName<TextFormat>(TextFormat) == null)
        yield return new ValidationResult($"{nameof(TextFormat)} {Errors.WrongOrNotManaged}");
    }

    [MemberNotNullWhen(false, new[] { nameof(Locale), nameof(Slug), nameof(Description), nameof(Title), nameof(Text), nameof(ShortText), nameof(TextFormat) })]
    private bool HasValidationErrors() => Validate(new ValidationContext(this)).Any();
  }
}
