using System.Text.Json.Serialization;

namespace HRServer.Models
{
    public static class HorseFactory
    {
        // Thread-safe Dictionary
        private static readonly Dictionary<ulong, Horse> Horses = new();
        public static Horse? GetHorse(ulong id)
        {
            return Horses.TryGetValue(id, out var horse) ? horse : null;
        }
        public static void AddOrUpdateHorse(Horse horse)
        {
            if (horse == null || horse.Id == null)
                throw new ArgumentNullException(nameof(horse), "Horse or its ID cannot be null.");

            Horses[horse.Id.Value] = horse;
        }

        public static IReadOnlyDictionary<ulong, Horse> GetAllHorses()
        {
            return Horses;
        }
    }

    public class Horse
    {
        // Basic Information
        [JsonPropertyName("id")]
        private ulong? _id;
        [JsonPropertyName("age")]
        private int? _age;
        [JsonPropertyName("name")]
        private string _horseName = string.Empty;
        [JsonPropertyName("gender")]
        private string _gender = string.Empty;
        [JsonPropertyName("breed")]
        private string _breed = string.Empty;
        [JsonPropertyName("link")]
        private string _link = string.Empty;

        public ulong? Id
        {
            get => _id;
            set
            {
                _id = value;
                LoadState.BasicInfoLoaded = true;
            }
        }

        public int? Age
        {
            get => _age;
            set
            {
                _age = value;
                LoadState.BasicInfoLoaded = true;
            }
        }

        public string HorseName
        {
            get => _horseName;
            set
            {
                _horseName = value;
                LoadState.BasicInfoLoaded = true;
            }
        }

        public string Gender
        {
            get => _gender;
            set
            {
                _gender = value;
                LoadState.BasicInfoLoaded = true;
            }
        }

        public string Breed
        {
            get => _breed;
            set
            {
                _breed = value;
                LoadState.BasicInfoLoaded = true;
            }
        }

        public string Link
        {
            get => _link;
            set
            {
                _link = value;
                LoadState.BasicInfoLoaded = true;
            }
        }

        private HorseSummary _summary = new();
        public HorseSummary Summary
        {
            get => _summary;
            set
            {
                _summary = value;
                LoadState.SummaryLoaded = true;
            }
        }

        private HorseTraining _training = new();
        public HorseTraining Training
        {
            get => _training;
            set
            {
                _training = value;
                LoadState.TrainingLoaded = true;
            }
        }

        private HorseGenetics _genetics = new();
        public HorseGenetics Genetics
        {
            get => _genetics;
            set
            {
                _genetics = value;
                LoadState.GeneticsLoaded = true;
            }
        }

        private HorseAchievements _achievements = new();
        public HorseAchievements Achievements
        {
            get => _achievements;
            set
            {
                _achievements = value;
                LoadState.AchievementsLoaded = true;
            }
        }

        private HorseHealth _health = new();
        public HorseHealth Health
        {
            get => _health;
            set
            {
                _health = value;
                LoadState.HealthLoaded = true;
            }
        }

        public DataLoadState LoadState { get; set; } = new();

        public bool IsAllDataLoaded()
        {
            return LoadState.IsAllDataLoaded();
        }

        public void PrintLoadState()
        {
            Console.WriteLine("Load State of Horse Data:");
            Console.WriteLine($"- Basic Info Loaded: {LoadState.BasicInfoLoaded}");
            Console.WriteLine($"- Summary Loaded: {LoadState.SummaryLoaded}");
            Console.WriteLine($"- Training Loaded: {LoadState.TrainingLoaded}");
            Console.WriteLine($"- Genetics Loaded: {LoadState.GeneticsLoaded}");
            Console.WriteLine($"- Achievements Loaded: {LoadState.AchievementsLoaded}");
            Console.WriteLine($"- Health Loaded: {LoadState.HealthLoaded}");
        }
    }

    public class DataLoadState
    {
        public bool BasicInfoLoaded { get; set; } = false;
        public bool SummaryLoaded { get; set; } = false;
        public bool TrainingLoaded { get; set; } = false;
        public bool GeneticsLoaded { get; set; } = false;
        public bool AchievementsLoaded { get; set; } = false;
        public bool HealthLoaded { get; set; } = false;

        public bool IsAllDataLoaded()
        {
            return BasicInfoLoaded && SummaryLoaded && TrainingLoaded && GeneticsLoaded && AchievementsLoaded && HealthLoaded;
        }
    }

    public class HorseSummary
    {
        public List<ulong> RelatedIds { get; set; } = new();
    }

    public class HorseTraining
    {
        public string Training { get; set; } = string.Empty;
    }

    public class HorseGenetics
    {
        [JsonPropertyName("GP")]
        public int GP { get; set; }

        [JsonPropertyName("GeneticPotential")]
        public Dictionary<string, float> GeneticPotential { get; set; } = new();

        [JsonPropertyName("Disciplines")]
        public Dictionary<string, float> Disciplines { get; set; } = new() {
            { "Dressage", 0 },
            { "Driving", 0 },
            { "Endurance", 0 },
            { "Eventing", 0 },
            { "Flat Racing", 0 },
            { "Show Jumping", 0 },
            {"Western Reining", 0 }
        };

        [JsonPropertyName("Colors")]
        public Dictionary<string, string> Colors { get; set; } = new() {
            { "extension", string.Empty },
            { "agouti", string.Empty },
            { "grey", string.Empty},
            { "cream", string.Empty },
            { "dun", string.Empty },
            { "champagne", string.Empty },
            { "silver", string.Empty },
            { "mushroom", string.Empty },
            { "frame", string.Empty},
            { "appaloosa", string.Empty },
            { "patn1", string.Empty },
            { "mitf", string.Empty },
            { "sw2", string.Empty },
            { "kit", string.Empty },
            { "rab", string.Empty},
            { "seal", string.Empty },
            { "flaxen", string.Empty }
        };
    }

    public class HorseAchievements
    {
        // Anpassung: Nun wird ein Array an Achievements verarbeitet,
        // wie es vom Content Script gesendet wird.
        public List<string> Achievements { get; set; } = new();
    }

    public class HorseHealth
    {
        public string Fertility { get; set; } = string.Empty;
    }
}
