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

namespace PokemonSleep.Web.Controllers
{
    public class PokemonController : Controller
    {

        private readonly IPokemonService _pokemonService;
        private readonly IPokemonNatureService _natureService;
        private readonly IPokemonSubskillService _subskillService;
        public PokemonController(IPokemonService pokemonService, IPokemonNatureService natureService, IPokemonSubskillService subskillService)
        {
            _pokemonService = pokemonService;
            _natureService = natureService;
            _subskillService = subskillService;
        }
        public async Task<IActionResult> PokemonIndex()
        {
            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 async Task<IActionResult> PokemonCreate()
        {
            return View();
        }

        // Create a new Pokemon on Submit
        [HttpPost]
        public async Task<IActionResult> PokemonCreate(PokemonDto model)
        {
            if (ModelState.IsValid)
            {
                ResponseDto? response = await _pokemonService.CreatePokemonAsync(model);

                if (response != null && response.IsSuccess)
                {
                    TempData["success"] = "Pokemon Created. " + response?.Message;
                    return RedirectToAction(nameof(Index));
                }
                else
                {
                    TempData["error"] = response?.Message;
                }
            }
            return RedirectToAction(nameof(Index));
            //return View(model);
        }

        // Access the "Delete a Pokemon" form
        public async Task<IActionResult> PokemonDelete(int Id)
        {
            ResponseDto? response = await _pokemonService.GetPokemonByIdAsync(Id);

            if (response != null && response.IsSuccess)
            {
                PokemonDto? model = JsonConvert.DeserializeObject<PokemonDto>(Convert.ToString(response.Result));
                return View(model);
            }

            return NotFound();
        }

        // Delete a Pokemon on Submit
        [HttpPost]
        public async Task<IActionResult> PokemonDelete(PokemonDto pokemonDto)
        {
            ResponseDto? response = await _pokemonService.DeletePokemonAsync(pokemonDto.Id);

            if (response != null && response.IsSuccess)
            {
                TempData["success"] = "Pokemon Deleted. " + response?.Message;
                return RedirectToAction(nameof(Index));
            }
            else
            {
                TempData["error"] = response?.Message;
            }

            return View(pokemonDto);
        }


		/* OH BOY HERE WE GO */

		[HttpPost]
		public IActionResult GetPokemonDetails(int pokemonId)
		{
			var selectedPokemon = _pokemonService.GetPokemonByIdAsync(pokemonId);
			return Json(selectedPokemon);
		}

	}
}

