using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Repository.Interface.Category;
using GxPress.Repository.Interface.TrackLog;
using GxPress.Result.Category;
using GxPress.Service.Interface.Category;
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 ICategoryService categoryService;
        private readonly ILoginContext _loginContext;
        private readonly IHttpContextAccessor httpContextAccessor;
        private readonly ITrackLogRepository trackLogRepository;
        public AdminCategoryController(ICategoryRepository categoryRepository, ILoginContext _loginContext, IHttpContextAccessor httpContextAccessor, ITrackLogRepository trackLogRepository, ICategoryService categoryService)
        {
            this.categoryRepository = categoryRepository;
            this.httpContextAccessor = httpContextAccessor;
            this._loginContext = _loginContext;
            this.trackLogRepository = trackLogRepository;
            this.categoryService = categoryService;
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<CategoryResult> GetAsync(int id)
        {
            return await categoryService.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>
        /// 获取所有PC/APP端 0pc 1 app
        /// </summary>
        /// <returns></returns>
        [HttpGet("all/{typeId}")]
        public async Task<IEnumerable<Entity.tede2.Category.Category>> GetAllAsync(int typeId)
        {
            return await categoryRepository.GetAllAsync(typeId);
        }
        /// <summary>
        /// 查询子集
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="typeValue"> 类型0 pc 1 app</param>
        /// <returns></returns>
        [HttpGet("children/{parentId}/{typeValue}")]
        public async Task<IEnumerable<CategoryResult>> GetCategoryChildrenAsync(int parentId, int typeValue)
        {
            return await categoryService.GetCategoryChildrenAsync(parentId, typeValue);
        }
    }
}