using System;
using System.Threading.Tasks;
using GxPress.Common.Page;
using GxPress.Repository.Interface;
using GxPress.Request.Admin.Article;
using GxPress.Request.Article;
using GxPress.Request.ArticleAnalyze;
using GxPress.Result.Article;
using GxPress.Service.Interface.Article;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// 审批文章
    /// </summary>
    [Route("api/admin/article")]
    [ApiController]
    [Authorize]
    public class AdminArticleController : ControllerBase
    {
        private readonly IArticleRepository _articleRepository;
        private readonly IArticleService _articleService;

        public AdminArticleController(IArticleRepository articleRepository, IArticleService articleService)
        {
            _articleRepository = articleRepository;
            _articleService = articleService;
        }

        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ArticleResult> Add([FromBody] ArticleAddRequest request)
        {
            if (!string.IsNullOrEmpty(request.Summary) && request.Summary.Length > 500)
            {
                throw new Exception("保存失败,字符数不能超出500");
            }
            return await _articleService.AddAsync(request);
        }

        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ArticleResult> Edit([FromBody] ArticleEditRequest request)
        {
            if (!string.IsNullOrEmpty(request.Summary) && request.Summary.Length > 500)
            {
                throw new Exception("保存失败,字符数不能超出500");
            }
            return await _articleService.EditAsync(request);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("")]
        [AllowAnonymous]
        public async Task<PagedList<ArticleResult>> GetList([FromQuery] ArticleSearchRequest request)
        {
            var pagedList = await _articleRepository.GetListAsync(request.Keyword, request);
            return pagedList;
        }

        /// <summary>
        /// 文章
        /// </summary>
        /// <returns></returns>
        [HttpGet("{id}")]
        [AllowAnonymous]
        public async Task<ArticleResult> Get(int id)
        {
            var article= await _articleService.GetAsync(id,0);
            return  article;
        }

        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> Delete(int id)
        {
            return await _articleRepository.DeleteAsync(id);
        }
        ///// <summary>
        /////  文章 点赞 收藏 转发
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost("analyze")]
        //[AllowAnonymous]
        //public async Task<bool> AddArticleAnalyze([FromBody] ArticleAnalyzeRequest request)
        //{
        //    return await _articleAnalyzeRepository.SetArticleAnalyzeAsync(request);
        //}

        /// <summary>
        /// 克隆文章
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("clone")]
        public async Task<bool> CloneArticle(CloneArticleRequest request)
        { 
            return await _articleRepository.CloneArticleAsync(request);
        }
    }
}