using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.EnumConst;
using GxPress.Repository.Interface.Media;
using GxPress.Repository.Interface.TrackLog;
using GxPress.Request.Media;
using GxPress.Result.Media;
using GxPress.Service.Interface.Media;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using GxPress.Common.Tools;

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

namespace GxPress.Api.AdminControllers
{
    [Route("api/admin/media")]
    [ApiController]
    [Authorize]
    public class AdminMediaController : Controller
    {
        private readonly IMediaRepository mediaRepository;
        private readonly ILoginContext _loginContext;
        private readonly IMediaService mediaService;
        private readonly ITrackLogRepository trackLogRepository;
        private readonly IHttpContextAccessor httpContextAccessor;
        public AdminMediaController(IMediaRepository mediaRepository, ILoginContext _loginContext, IMediaService mediaService, ITrackLogRepository trackLogRepository, IHttpContextAccessor httpContextAccessor)
        {
            this.mediaRepository = mediaRepository;
            this._loginContext = _loginContext;
            this.mediaService = mediaService;
            this.trackLogRepository = trackLogRepository;
            this.httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MediaResult> GetAsync(int id)
        {
            return await mediaService.GetAsync(id);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> DeleteAsync(int id)
        {
            var result = await mediaRepository.GetAsync(id);
            var success = await mediaRepository.DeleteAsync(id);
            if (success)
            {
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
                var remark = $"删除{mediaTypeValue}{result.Title}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
        /// <summary>
        /// 添加媒体
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> InsertAsync(MediaResult result)
        {
            result.AdminId = _loginContext.AccountId;
            result.Creator = _loginContext.Name;
            var success = await mediaService.InsertAsync(result);
            if (success)
            {
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
                var remark = $"添加{mediaTypeValue}{result.Title}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> UpdateAsync(MediaResult result)
        {
            var success = await mediaRepository.UpdateAsync(result);
            if (success)
            {
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
                var remark = $"修改{mediaTypeValue}{result.Title}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
        {
            return await mediaRepository.GetAllAsync(request);
        }
    }
}
