using System;
using Application;
using Microsoft.AspNetCore.Mvc;
using Website.Models;

namespace Website
{
  public class LinksController : BaseController
  {
    private readonly LinkService _linkService;
    private readonly CommentService _commentService;

    public LinksController(LinkService linkService, CommentService commentService)
    {
      _linkService = linkService;
      _commentService = commentService;
    }

    public IActionResult Index()
    {
      // return Forbid();
      return View(_linkService.GetAllLinks());
    }

    [HttpGet("{controller}/detail/{linkId:guid}")]
    // [TypeFilter(typeof(CustomExceptionFilter))] // Permet d'appliquer un filtre ASP.Net
    // sur une action uniquement (valable pour toutes les actions d'un controlleur si appliqué sur ce dernier)
    public IActionResult Show(Guid linkId)
    {
      // ViewData["Comments"] = _commentService.GetAllLinkComments(linkId);

      return View(new ShowLinkViewModel(
        _linkService.GetLinkById(linkId),
        _commentService.GetAllLinkComments(linkId)));
    }

    // [HttpGet] // Implicite car HttpGet par défaut
    public IActionResult Create()
    {
      // ViewData["Message"] = "Une valeur";
      return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create(PublishLinkCommand cmd)
    {
      if (!ModelState.IsValid)
      {
        return View(cmd);
      }

      _linkService.PublishLink(cmd);

      Success("Your link was posted!");

      return RedirectToAction(nameof(Index));
    }
  }
}