using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using New_College.IServices;
using New_College.Model;
using New_College.Model.Request;
using New_College.Model.ViewModels;

namespace New_College.Api.Controllers.Front
{
    [Route("api/front/[controller]/[action]")]
    [ApiController]
    public class NewsInfoController : ControllerBase
    {

        private readonly ID_NewsCategoryServices iD_NewsCategory;
        private readonly ID_NewsInfoServices d_NewsInfo;
        public NewsInfoController(ID_NewsCategoryServices d_NewsCategoryServices, ID_NewsInfoServices d_NewsInfoServices)
        {
            this.iD_NewsCategory = d_NewsCategoryServices;
            this.d_NewsInfo = d_NewsInfoServices;
        }



        /// <summary>
        /// 获取top多少的新闻
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<NewsInfoResponse>>> GetTopNews([FromQuery] NewsRequest request)
        {
            try
            {
                var list = new List<NewsInfoResponse>();
                var query = (await d_NewsInfo.Query(e => e.IsDelete == false && e.CategoryId == request.CategoryId)).Take(request.Top).OrderByDescending(s => s.CreateTime);
                list = query.Select(s => new NewsInfoResponse
                {
                    CoverImg = s.CoverImg,
                    CreateTime = s.CreateTime.Value,
                    Id = s.Id,
                    Title = s.Title
                }).ToList();
                return new MessageModel<List<NewsInfoResponse>>()
                {
                    msg = "success",
                    success = true,
                    response = list
                };
            }
            catch (Exception ex)
            {
                return new MessageModel<List<NewsInfoResponse>>()
                {
                    msg = ex.Message

                };
            }
        }



        /// <summary>
        /// 所有新闻列表带分页
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<NewsInfoResponse>>> GetPageNews([FromQuery] NewsPageRequest request)
        {
            var pageresponse = new PageModel<NewsInfoResponse>();
            try
            {
                var pageinfo = await d_NewsInfo.QueryPage(w => w.CategoryId == request.CategoryId, request.PageIndex, request.PageSize);
                if (pageinfo.data.Any())
                {
                    pageresponse.data = pageinfo.data.Select(s => new NewsInfoResponse()
                    {
                        CoverImg = s.CoverImg,
                        CreateTime = s.CreateTime.Value,
                        Id = s.Id,
                        Title = s.Title
                    }).ToList();
                }
                return new MessageModel<PageModel<NewsInfoResponse>>()
                {
                    msg = "success",
                    response = pageresponse,
                    success = true
                };

            }
            catch (Exception ex)
            {
                return new MessageModel<PageModel<NewsInfoResponse>>()
                {
                    msg = "fail",
                    success = false
                };
            }
        }


        /// <summary>
        /// 根据Id 获取新闻详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<NewsDetailInfoResponse>> GetDetail([FromQuery] NewsDetailRequest request)
        {
            try
            {
                var result = new NewsDetailInfoResponse();
                var single = await d_NewsInfo.QueryById(request.Id);
                result.Author = single.Author;
                result.Click = single.Click;
                result.CoverImg = single.CoverImg;
                result.CreateTime = single.CreateTime.Value;
                result.Id = single.Id;
                result.Summary = single.Summary;
                result.Title = single.Title;
                result.Detail = single.Detail;
                return new MessageModel<NewsDetailInfoResponse>()
                {
                    msg = "success",
                    response = result,
                    success = true
                };
            }
            catch (Exception ex)
            {
                return new MessageModel<NewsDetailInfoResponse>()
                {
                    msg = ex.Message
                };
            }
        }

    }
}
