using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using PokemonSleep.Web.Models;
using PokemonSleep.Web.Service.IService;
using System.Diagnostics;

namespace PokemonSleep.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IPokemonService _pokemonService;
        private readonly IPokemonNatureService _natureService;
        private readonly IPokemonSubskillService _subskillService;

        public HomeController(ILogger<HomeController> logger, IPokemonService pokemonService, IPokemonNatureService natureService, IPokemonSubskillService subskillService)
        {
            _pokemonService = pokemonService;
            _natureService = natureService;
            _subskillService = subskillService;
            _logger = logger;
        }


        public async Task<IActionResult> Index()
        {
            PokemonSleepDto? list = new();

            ResponseDto? presponse = await _pokemonService.GetAllPokemonAsync();
            ResponseDto? nresponse = await _natureService.GetAllPokemonNaturesAsync();
            ResponseDto? sresponse = await _subskillService.GetAllPokemonSubskillsAsync();

            if (presponse != null && presponse.IsSuccess)
            {
                list.pokemonList = JsonConvert.DeserializeObject<List<PokemonDto>>(Convert.ToString(presponse.Result));
            }
            if (nresponse != null && nresponse.IsSuccess)
            {
                list.natureList = JsonConvert.DeserializeObject<List<PokemonNatureDto>>(Convert.ToString(nresponse.Result));
            }
            if (sresponse != null && sresponse.IsSuccess)
            {
                list.subskillList = JsonConvert.DeserializeObject<List<PokemonSubskillDto>>(Convert.ToString(sresponse.Result));
            }


            return View(list);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
