using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Repository.Interface.Category;
using GxPress.Repository.Interface.TrackLog;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace GxPress.Api.AdminControllers
{
    [ApiController]
    [Authorize]
    [Route("api/admin/category")]
    public class AdminCategoryController : Controller
    {
        private readonly ICategoryRepository categoryRepository;
        private readonly ILoginContext _loginContext;
        private readonly IHttpContextAccessor httpContextAccessor;
        private readonly ITrackLogRepository trackLogRepository;
        public AdminCategoryController(ICategoryRepository categoryRepository, ILoginContext _loginContext, IHttpContextAccessor httpContextAccessor, ITrackLogRepository trackLogRepository)
        {
            this.categoryRepository = categoryRepository;
            this.httpContextAccessor = httpContextAccessor;
            this._loginContext = _loginContext;
            this.trackLogRepository = trackLogRepository;
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<Entity.tede2.Category.Category> GetAsync(int id)
        {
            return await categoryRepository.GetAsync(id);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> DeleteAsync(int id)
        {
            var success = await categoryRepository.DeleteAsync(id);
            if (success)
            {
                var category = await categoryRepository.GetAsync(id);
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var remark = $"删除类目：{category.Name}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task<bool> InsertAsync(Entity.tede2.Category.Category note)
        {
            var success = await categoryRepository.InsertAsync(note) > 0;
            if (success)
            {
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var remark = $"添加类目：{note.Name}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> UpdateAsync(Entity.tede2.Category.Category note)
        {
            var success = await categoryRepository.UpdateAsync(note);
            if (success)
            {
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var remark = $"修改类目：{note.Name}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IEnumerable<Entity.tede2.Category.Category>> GetAllAsync()
        {
            return await categoryRepository.GetAllAsync();
        }
        /// <summary>
        /// 查询子集
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet("children/{parentId}")]
        public async Task<IEnumerable<Entity.tede2.Category.Category>> GetCategoryChildrenAsync(int parentId)
        {
            return await categoryRepository.GetCategoryChildrenAsync(parentId);
        }
    }
}
