﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WakeboardUK2018.Data;
using WakeboardUK2018.Models;

namespace WakeboardUK2018.Controllers
{
    public class HomeViewModel
    {
        public PaginatedList<Article> Articles { set; get; }
        public PaginatedList<Competition> Competitions { set; get; }
        public PaginatedList<Media> Media { set; get; }
    }

    public class HomeController : Controller
    {
        private readonly ApplicationDbContext _context;

        public HomeController(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<IActionResult> Index()
        {
            var articles = _context.Articles.OrderByDescending(s => s.ArticleId);
            var year = System.DateTime.Today.Year;
            var competitions = _context.Competitions
                .OrderBy(a => a.start_date)
                .Where(a => a.start_date.Value.Year == year)
                .Where(s => s.published);
            var media =  _context.Medias.OrderByDescending(x => x.MediaId);

            HomeViewModel hvm = new HomeViewModel();
            hvm.Articles = await PaginatedList<Article>.CreateAsync(articles, 1, 5);
            hvm.Competitions = await PaginatedList<Competition>.CreateAsync(competitions, 1, 8);
            hvm.Media = await PaginatedList<Media>.CreateAsync(media, 1, 4);

            return View(hvm);
        }

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

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

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

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

        public IActionResult Error()
        {
            return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
        }
    }
}