﻿using LuzzedroCMS.Domain.Abstract;
using LuzzedroCMS.Domain.Entities;
using LuzzedroCMS.Infrastructure.Attributes;
using LuzzedroCMS.Models;
using LuzzedroCMS.WebUI.Infrastructure.Helpers;
using LuzzedroCMS.WebUI.Infrastructure.Static;
using LuzzedroCMS.WebUI.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace LuzzedroCMS.Controllers
{
    public class SearchController : Controller
    {
        private IArticleRepository repoArticle;
        private ICategoryRepository repoCategory;
        private IConfigurationKeyRepository repoConfig;
        public int PageSize = 10;

        public SearchController(
            IArticleRepository articleRepo,
            ICategoryRepository categoryRepo,
            IConfigurationKeyRepository configRepo)
        {
            repoArticle = articleRepo;
            repoCategory = categoryRepo;
            repoConfig = configRepo;
        }

        [HttpGet]
        [ChildActionOnly]
        public ViewResult Index()
        {
            return View();
        }

        [HttpGet]
        [RestoreModelStateFromTempData]
        public ActionResult Result(SearchViewModel searchViewModel, int page = 1)
        {
            if (ModelState.IsValid)
            {
                IList<Article> articles = repoArticle.Articles(key: searchViewModel.Key, page: page, take: PageSize).OrderBy(x => x.DateAdd).ToList();
                ArticlesSearchedViewModel articleSearchViewModel = new ArticlesSearchedViewModel
                {
                    SearchKey = searchViewModel.Key,
                    Articles = articles,
                    ContentExternalUrl = repoConfig.Get(ConfigurationKeyStatic.CONTENT_EXTERNAL_URL),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage = page,
                        ItemsPerPage = PageSize,
                        TotalItems = repoArticle.Articles(key: searchViewModel.Key).Count()
                    }
                };
                ViewBag.Title = String.Format("{0} - {1}", searchViewModel.Key, Resources.SearchResult);
                if (articles.Count() != 0)
                {
                    ViewBag.Description = String.Format("{0} - {1}", articles.First().Lead, Resources.SearchResult);
                    ViewBag.Keywords = String.Format("{0} - {1}", articles.First().ImageDesc, Resources.ArticleByCategory);
                }
                return View(articleSearchViewModel);
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        [SetTempDataModelState]
        public ActionResult Result(SearchViewModel searchViewModel, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Title = String.Format("{0} - {1}", searchViewModel.Key, Resources.SearchResult);
                ViewBag.Description = String.Format("{0} - {1}", searchViewModel.Key, Resources.SearchResult);
                return RedirectToRoute("Search", new { Key = searchViewModel.Key });
            }
            else
            {
                return Redirect(returnUrl ?? Url.Action("Index", "Home"));
            }
        }
    }
}