using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Sells.Domain;
using Hncore.Pass.Sells.Model;
using Hncore.Pass.Sells.Request.RedeemCode;
using Hncore.Pass.Sells.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Hncore.Pass.Live.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/sells/v{version:apiVersion}/coupon/[action]")]
    public class CouponController : HncoreControllerBase
    {
        CouponService m_CouponService;
        CouponResourceService m_CouponResource;

        public CouponController(CouponService _CouponService
            , CouponResourceService _CouponResource)
        {
            m_CouponService = _CouponService;
            m_CouponResource = _CouponResource;
        }

        [HttpPost]
        public async Task<ApiResult> Post([FromBody]PostCouponRequest request)
        {           
            var entity = request.MapTo<CouponEntity>();          
            if (entity.Name.NotHas())
            {
                return Error("���Ʋ���Ϊ��");
            }
            if (m_CouponService.Exist(m => m.Name == entity.Name))
            {
                return Error("�����Ѿ�������");
            }          
            var ret = await m_CouponService.Add(entity);          
            return Success();
        }

        [HttpPost]
        public async Task<ApiResult> Put([FromBody]PostCouponRequest request)
        {
            var entity = await m_CouponService.GetById(request.Id);
            if (entity == null)
            {
                return Error("�Ż�ȯ������");
            }
            if (request.Name.NotHas())
            {
                return Error("���Ʋ���Ϊ��");
            }
            if (m_CouponService.Exist(m => m.Id != request.Id && m.Name == request.Name))
            {
                return Error("�����Ѿ�������");
            }

            entity.Name = request.Name;
            entity.AllowMinAmount = request.AllowMinAmount;
            entity.CouponType = request.CouponType;
            entity.CouponValue = request.CouponValue;
            entity.DateRule = request.DateRule;
            if (entity.DateRule == ECouponDateRule.BenginEnd)
            {
                entity.EndDate = request.EndDate;
                entity.StartDate = request.StartDate;
            }
            else
            {
                entity.ValidDay = request.ValidDay;
            }
            await m_CouponService.Update(entity);
            return Success();
        }

        [HttpPost]
        public async Task<ApiResult> Disabled([FromQuery]int Id)
        {
            var redeemCode = await m_CouponService.GetById(Id);
            if (redeemCode == null)
            {
                return Error("���Ż�ȯ������");
            }
            redeemCode.Disabled = 1;
            await m_CouponService.Update(redeemCode);
            return Success();
        }

        [HttpPost]
        public async Task<ApiResult> Delete([FromQuery]int id)
        {
            await m_CouponService.DeleteById(id);
            return Success();
        }

        [HttpPost]
        public ApiResult Deletes([FromBody] List<int> ids)
        {
            ids.ForEach(async m => await m_CouponService.DeleteById(m));
            return Success();
        }

        [HttpGet]
        public async Task<ApiResult> Get([FromQuery] int id)
        {
            var ret = await m_CouponService.GetById(id);
            return Success(ret);
        }

        /// <summary>
        /// ��ѯ
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<ApiResult> Page([FromQuery]PageRequestBase request)
        {
            Expression<Func<CouponEntity, bool>> expr = m =>
                string.IsNullOrEmpty(request.KeyWord)
                || (m.Name.Contains(request.KeyWord));

            var ret = await m_CouponService.Page(request.PageIndex, request.PageSize, expr);
            var data = ret.ToApiResult();
            return data;
        }

        [HttpPost]
        public async Task<ApiResult> Give([FromBody]GiveCouponModelRequest request)
        {           
            if (request.CouponId==0)
            {
                return Error("��ѡ���Ż�ȯ");
            }
            if (request.UserId == 0)
            {
                return Error("��ѡ���û�");
            }    
            await this.m_CouponService.Give(request.CouponId, this.Request.GetManageUserInfo().OperaterId.ToString(), request.UserId, 1);
            return Success();
        }
        [HttpPost]
        public async Task<ApiResult> Freeze([FromQuery]int originId)
        {
            await m_CouponService.Freeze(originId);
            return Success();
        }
        [HttpGet,UserAuth]
        public async Task<ApiResult> GetAvailableCoupon()
        {
            var userId = this.Request.GetUserInfo().UserId;
            var ret = await m_CouponService.GetUserAvailableCoupon(userId);
            return Success(ret);
        }
    }
}