using HRServer.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;

namespace HRServer.Controllers
{
    [ApiController]
    public class HorseController : ControllerBase
    {
        private readonly ILogger<HorseController> _logger;

        public HorseController(ILogger<HorseController> logger)
        {
            _logger = logger;
        }
        [HttpGet("/api/ping")]
        public IActionResult Ping()
        {
            return Ok("pong");
        }
        [HttpGet("/api/getHorse/{id}")]
        public IActionResult GetHorse (int id)
        {
            var horse = HorseFactory.GetHorse((ulong)id);
            if (horse == null)
            {
                horse = new Horse { Id = (ulong)id };
                HorseFactory.AddOrUpdateHorse(horse);
            }
            HorseFactory.SaveHorsesToFile();
            return Ok(horse);
        }
        [HttpGet("/api/getHorse/{id}/LoadState")]
        public IActionResult GetHorseLoadState (ulong id)
        {
            // Hole das Pferd aus der Factory
            var horse = HorseFactory.GetHorse((ulong)id);

            // Überprüfe, ob das Pferd existiert
            if (horse == null)
            {
                horse = new Horse { Id = (ulong)id };
                HorseFactory.AddOrUpdateHorse(horse);
            }
            if (horse.LastDrawnDate.AddDays(1) < DateTime.Now)
            {
                horse.LoadState.AchievementsNeedsRefresh = true;
            }
            // Gib den Ladezustand des Pferdes zurück
            HorseFactory.SaveHorsesToFile();
            return Ok(horse.LoadState);
        }
        [HttpPost("/api/updateHorse/{id}/BasicData")]
        public IActionResult UpdateHorseBasicData(ulong id, [FromBody] Horse websiteHorse)
        {
            var localHorse = HorseFactory.GetHorse((ulong)id);
            if (localHorse == null)
            {
                localHorse = new Horse { Id = (ulong)id };
                HorseFactory.AddOrUpdateHorse(localHorse);
            }
            localHorse.Age = localHorse.Age;
            localHorse.Breed = localHorse.Breed;
            localHorse.HorseName = localHorse.HorseName;
            localHorse.Gender = localHorse.Gender;
            localHorse.Link = localHorse.Link;
            HorseFactory.SaveHorsesToFile();
            return Ok(localHorse);
        }
        [HttpPost("/api/updateHorse/{id}/Pedigree")]
        public IActionResult UpdateHorsePedigree(ulong id, [FromBody] HorseSummary websiteHorseSummary)
        {
            var localHorse = HorseFactory.GetHorse((ulong)id);
            if (localHorse == null)
            {
                localHorse = new Horse { Id = (ulong)id };
                HorseFactory.AddOrUpdateHorse(localHorse);
            }
            localHorse.Summary = websiteHorseSummary;
            HorseFactory.SaveHorsesToFile();
            return Ok(localHorse);
        }
        [HttpPost("/api/updateHorse/{id}/Training")]
        public IActionResult UpdateHorseTraining(ulong id, [FromBody] HorseTraining websiteHorseTraining)
        {
            var localHorse = HorseFactory.GetHorse((ulong)id);
            if (localHorse == null)
            {
                localHorse = new Horse { Id = (ulong)id };
                HorseFactory.AddOrUpdateHorse(localHorse);
            }
            localHorse.Training = websiteHorseTraining;
            HorseFactory.SaveHorsesToFile();
            return Ok(localHorse);
        }
        [HttpPost("/api/updateHorse/{id}/Health")]
        public IActionResult UpdateHorseHealth(ulong id, [FromBody] HorseHealth websiteHorseHealth)
        {
            var localHorse = HorseFactory.GetHorse((ulong)id);
            if (localHorse == null)
            {
                localHorse = new Horse { Id = (ulong)id };
                HorseFactory.AddOrUpdateHorse(localHorse);
            }
            localHorse.Health = websiteHorseHealth;
            HorseFactory.SaveHorsesToFile();
            return Ok(localHorse);
        }
        [HttpPost("/api/updateHorse/{id}/Genetics")]
        public IActionResult UpdateHorseGenetics(ulong id, [FromBody] HorseGenetics websiteHorseGenetics)
        {
            var localHorse = HorseFactory.GetHorse((ulong)id);
            if (localHorse == null)
            {
                localHorse = new Horse { Id = (ulong)id };
                HorseFactory.AddOrUpdateHorse(localHorse);
            }
            localHorse.Genetics = websiteHorseGenetics;
            HorseFactory.SaveHorsesToFile();
            return Ok(localHorse);
        }
        [HttpPost("/api/updateHorse/{id}/Achievements")]
        public IActionResult UpdateHorseAchievements(ulong id, [FromBody] HorseAchievements websiteHorseAchievements)
        {
            var localHorse = HorseFactory.GetHorse((ulong)id);
            if (localHorse == null)
            {
                localHorse = new Horse { Id = (ulong)id };
                HorseFactory.AddOrUpdateHorse(localHorse);
            }
            if (websiteHorseAchievements.MaxShowResult < localHorse.Achievements.MaxShowResult)
            {
                websiteHorseAchievements.MaxShowResult = localHorse.Achievements.MaxShowResult;
            }
            if (websiteHorseAchievements.MinShowResult > localHorse.Achievements.MinShowResult)
            {
                websiteHorseAchievements.MinShowResult = localHorse.Achievements.MinShowResult;
            }
            if (websiteHorseAchievements.MaxCompetitionResult < localHorse.Achievements.MaxCompetitionResult)
            {
                websiteHorseAchievements.MaxCompetitionResult = localHorse.Achievements.MaxCompetitionResult;
            }
            if (websiteHorseAchievements.MinCompetitionResult > localHorse.Achievements.MinCompetitionResult)
            {
                websiteHorseAchievements.MinCompetitionResult = localHorse.Achievements.MinCompetitionResult;
            }
            localHorse.Achievements = websiteHorseAchievements;
            HorseFactory.SaveHorsesToFile();
            return Ok(localHorse);
        }
    }
}
