using BinaryDad.AacpsBusAlert.Services;
using BinaryDad.AacpsBusAlert.Web.Models;
using BinaryDad.Extensions;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace BinaryDad.AacpsBusAlert.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> logger;
        private readonly BusRouteService busRouteService;

        public HomeController(ILogger<HomeController> logger, BusRouteService busRouteService)
        {
            this.logger = logger;
            this.busRouteService = busRouteService;
        }

        public async Task<IActionResult> Index()
        {
            var userRoutes = new[] { 203, 365, 277, 269, 274 };

            //var busRoutes = await CacheHelper.GetAsync("BusRoutes", () => busRouteService.GetBusRoutesAsync());
            var busRoutes = await busRouteService.GetBusRoutesAsync();

            var matchedBusRoutes = busRoutes.Join(userRoutes, r => r.BusNumber, u => u, (r, u) => r).ToList();

            return View(matchedBusRoutes);
        }

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