using Microsoft.Extensions.Configuration;
using Phares.Shared;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;

namespace View_by_Distance.Date.Group.Models.Binder;

public class Configuration
{

#nullable disable

    [Display(Name = "By Create Date Shortcut"), Required] public bool? ByCreateDateShortcut { get; set; }
    [Display(Name = "By Date"), Required] public bool? ByDay { get; set; }
    [Display(Name = "By Hash"), Required] public bool? ByHash { get; set; }
    [Display(Name = "By Season"), Required] public bool? BySeason { get; set; }
    [Display(Name = "By Week"), Required] public bool? ByWeek { get; set; }
    [Display(Name = "Ignore Subdirectories for Rename"), Required] public bool? KeepFullPath { get; set; }
    [Display(Name = "Property Configuration"), Required] public Property.Models.Configuration PropertyConfiguration { get; set; }

#nullable restore

    public override string ToString()
    {
        string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
        return result;
    }

    private static Models.Configuration Get(Configuration? configuration)
    {
        Models.Configuration result;
        if (configuration is null)
            throw new NullReferenceException(nameof(configuration));
        if (configuration.ByCreateDateShortcut is null)
            throw new NullReferenceException(nameof(configuration.ByCreateDateShortcut));
        if (configuration.ByDay is null)
            throw new NullReferenceException(nameof(configuration.ByDay));
        if (configuration.ByHash is null)
            throw new NullReferenceException(nameof(configuration.ByHash));
        if (configuration.BySeason is null)
            throw new NullReferenceException(nameof(configuration.BySeason));
        if (configuration.ByWeek is null)
            throw new NullReferenceException(nameof(configuration.ByWeek));
        if (configuration.KeepFullPath is null)
            throw new NullReferenceException(nameof(configuration.KeepFullPath));
        result = new(
            configuration.PropertyConfiguration,
            configuration.ByCreateDateShortcut.Value,
            configuration.ByDay.Value,
            configuration.ByHash.Value,
            configuration.BySeason.Value,
            configuration.ByWeek.Value,
            configuration.KeepFullPath.Value);
        return result;
    }

    public static Models.Configuration Get(IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, Property.Models.Configuration propertyConfiguration)
    {
        Models.Configuration result;
        Configuration? configuration;
        if (isEnvironment is null)
            configuration = configurationRoot.Get<Configuration>();
        else
        {
            string environmentName = IsEnvironment.GetEnvironmentName(isEnvironment);
            string section = string.Concat(environmentName, ":", nameof(Configuration));
            IConfigurationSection configurationSection = configurationRoot.GetSection(section);
            configuration = configurationSection.Get<Configuration>();
        }
        result = Get(configuration);
        if (configuration is null)
            throw new NullReferenceException(nameof(configuration));
        result.SetAndUpdate(propertyConfiguration);
        return result;
    }

}