using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.EnumConst;
using GxPress.Repository.Interface.TrackLog;
using GxPress.Repository.Interface.VipEquity;
using GxPress.Request.VipEquity;
using GxPress.Result.VipEquity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using GxPress.Common.Tools;
namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// VIP权益
    /// </summary>
    [Route("api/admin/vip-equity")]
    [ApiController]
    [Authorize]
    public class AdminVipEquityController : Controller
    {
        private readonly IVipEquityRepository vipEquityRepository;
        private readonly ILoginContext _loginContext;
        private readonly IHttpContextAccessor httpContextAccessor;
        private readonly ITrackLogRepository trackLogRepository;
        public AdminVipEquityController(IVipEquityRepository vipEquityRepository, ILoginContext _loginContext, IHttpContextAccessor httpContextAccessor, ITrackLogRepository trackLogRepository)
        {
            this.vipEquityRepository = vipEquityRepository;
            this._loginContext = _loginContext;
            this.httpContextAccessor = httpContextAccessor;
            this.trackLogRepository = trackLogRepository;
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IEnumerable<VipEquityResult>> GetAllAsync()
        {
            return await vipEquityRepository.GetAllAsync();
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> UpdateAsync(VipEquityUpdateRequest request)
        {
            var success = await vipEquityRepository.UpdateAsync(request);
            if (success)
            {
                var vipEquity = await vipEquityRepository.GetAsync(request.Id);
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var remark = $"修改Vip权益：{((VipCardTypeConst)vipEquity.VipType).GetDescriptionOriginal()}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> InsertAsync(Entity.tede2.VipEquity.VipEquity model)
        {
            var success = await vipEquityRepository.InsertAsync(model);
            if (success)
            {
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var remark = $"添加Vip权益：{((VipCardTypeConst)model.VipType).GetDescriptionOriginal()}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> DeleteAsync(int id)
        {
            var vipEquity = await vipEquityRepository.GetAsync(id);
            var success = await vipEquityRepository.DeleteAsync(id);
            if (success)
            {
                var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
                var remark = $"删除Vip权益：{((VipCardTypeConst)vipEquity.VipType).GetDescriptionOriginal()}";
                await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
            }
            return success;
        }
    }
}