using AS1024.CommunityDocumentationPage.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using AS1024.CommunityDocumentationPage.Interfaces;

namespace AS1024.CommunityDocumentationPage.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IBgpCommunity _bgpCommunity;
        private readonly IConfiguration _configuration;

        public HomeController(ILogger<HomeController> logger,
            IConfiguration configuration,
            IBgpCommunity bgpCommunity)
        {
            _logger = logger;
            _configuration = configuration;
            _bgpCommunity = bgpCommunity;
        }

        [ResponseCache(Duration = 3600)]
        public async Task<IActionResult> Index()
        {
            try
            {
                var result =
                    await _bgpCommunity.GetCommunities();

                return View(result);
            } catch (Exception ex)
            {
                _logger.LogError($"Failed to obtain data\n{ex}");
            }

            return NotFound();
        }

        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 });
        }
    }
}