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 IBusRouteService busRouteService;
        private readonly IUserService userService;

        public HomeController(IBusRouteService busRouteService, IUserService userService)
        {
            this.busRouteService = busRouteService;
            this.userService = userService;
        }

        public async Task<IActionResult> Index()
        {
            var user = userService.GetUser(Guid.Parse("b43f4f45-0f7b-4976-849d-8ad4b271199c"));

            //var busRoutes = await CacheHelper.GetAsync("BusRoutes", () => busRouteService.GetBusRoutesAsync());
            var busRoutes = await busRouteService.GetBusRoutesAsync();
            var userRouteIds = user.Routes.Select(r => r.BusNumber).ToList();

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

            var summary = new SummaryViewModel
            {
                User = user,
                Routes = matchedBusRoutes
            };

            return View(summary);
        }

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