using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.Admin;
using GxPress.Request.AppVersion;
using GxPress.Request.Thesaurus;
using GxPress.Result.Admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// 词库 热词 冷词
    /// </summary>
    [Route("api/admin/thesaurus")]
    [ApiController]
    [Authorize]
    public class AdminThesaurusController : ControllerBase
    {

        private readonly IThesaurusRepository _thesaurusRepository;

        public AdminThesaurusController(IThesaurusRepository thesaurusRepository)
        {
            _thesaurusRepository = thesaurusRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [HttpPut("add")]
        [AllowAnonymous]
        public async Task<bool> InThesaurus([FromBody] ThesaurusInRequest request)
        {
            if (!string.IsNullOrEmpty(request.ThesaurusName) && request.ThesaurusName.Length > 500)
            {
                throw new Exception("保存失败，字符数不能超出16字");
            }
            var appVersion = new Thesaurus { ThesaurusType = request.ThesaurusType, ThesaurusName = request.ThesaurusName };
            return await _thesaurusRepository.InsertAsync(appVersion);
        }

        /// <summary>
        /// 分页查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("page")]
        [AllowAnonymous]
        public async Task<PagedList<Thesaurus>> GetPagedList([FromBody]ThesaurusPageSearchRequest request)
        {
            return await _thesaurusRepository.GetPageList(request);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> Delete(int id)
        {
            return await _thesaurusRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<Thesaurus> Get(int id)
        {
            return await _thesaurusRepository.GetDetailAsync(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<bool> Update(int id, [FromBody] Thesaurus request)
        {
            if (!string.IsNullOrEmpty(request.ThesaurusName) && request.ThesaurusName.Length > 500)
            {
                throw new Exception("保存失败，字符数不能超出16字");
            }
            return await _thesaurusRepository.UpdateAsync(id, request);
        }
    }
}