package com.gree.mall.miniapp.controller.coupon;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.coupon.*;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.coupon.CouponLogic;
import com.gree.mall.miniapp.plus.entity.Coupon;
import com.gree.mall.miniapp.bean.coupon.CouponBean;
import com.gree.mall.miniapp.bean.coupon.CouponDataBean;
import com.gree.mall.miniapp.logic.coupon.CouponLogic;
import com.gree.mall.miniapp.plus.entity.Coupon;
import com.gree.mall.miniapp.plus.entity.UserCoupon;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "优惠券管理", tags = {"优惠券管理"})
@RequestMapping(value = "/coupon", produces = "application/json; charset=utf-8")
public class CouponController {

    @Autowired
    CouponLogic couponLogic;
    @Autowired
    RedisLockRegistry redisLockRegistry;


    @GetMapping("/timeout")
    @ApiOperation(value = "优惠券过期提醒")
    public ResponseHelper<Boolean> timeout(
            @ApiParam(value = "用户id") @RequestParam(required = false) String userId
    ) {
        Boolean timeout = couponLogic.timeout(userId);
        return ResponseHelper.success(timeout);
    }


    @GetMapping("/count")
    @ApiOperation(value = "优惠券统计")
    public ResponseHelper<UserCouponCountBean> count(
            @ApiParam(value = "用户id") @RequestParam(required = false) String userId
    ) {
        UserCouponCountBean count = couponLogic.count(userId);
        return ResponseHelper.success(count);
    }

    @GetMapping("/list/page")
    @ApiOperation(value = "优惠券列表")
    public ResponseHelper<Page<UserCoupon>> page(
            @ApiParam(value = "用户id") @RequestParam(required = false) String userId,
            @ApiParam(value = "0:未使用  1:已使用 2:已过期", required = false) @RequestParam(required = false) Integer type,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize

    ) throws RemoteServiceException {
        IPage<UserCoupon> page = couponLogic.page(userId, type, pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @GetMapping("/list/expiring")
    @ApiOperation(value = "快要到期优惠券列表")
    public ResponseHelper<List<UserCoupon>> expiring(
            @ApiParam(value = "用户id") @RequestParam(required = false) String userId
    ) throws RemoteServiceException {
        List<UserCoupon> coupons = couponLogic.expiring(userId);
        return ResponseHelper.success(coupons);
    }

    @GetMapping("/list/all")
    @ApiOperation(value = "可领/已领/待领优惠券列表")
    public ResponseHelper<List<CouponObtainBean>> listAll(
            @ApiParam(value = "用户id") @RequestParam(required = false) String userId,
            HttpServletRequest request
    ) throws RemoteServiceException {
        List<CouponObtainBean> coupons = couponLogic.allObtainCoupou(userId,request);
        return ResponseHelper.success(coupons);
    }


    @GetMapping("/obtain")
    @ApiOperation(value = "领券")
    public ResponseHelper obtain(HttpServletRequest request,
            @ApiParam(value = "用户id") @RequestParam(required = false) String userId,
            @ApiParam(value = "优惠券id") @RequestParam(required = false) List<String> couponIds

    ) throws RemoteServiceException, InterruptedException {
        for(String couponId : couponIds) {
            Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_COUPON + couponId);
            if(!obtain.tryLock(10,TimeUnit.SECONDS)){
                log.error("系统繁忙，领券失败,userId:{},couponId:{}",userId,couponId);
                continue;
            }
            try {
                couponLogic.obtainCoupou(request, userId, couponId);
            }finally {
                obtain.unlock();
            }
        }
        return ResponseHelper.success();
    }


    @GetMapping("/list/useable")
    @ApiOperation(value = "可用优惠券列表")
    public ResponseHelper<List<CustomCoupouBean>> useableCoupou(
            @ApiParam(value = "用户id", required = true) @RequestParam(required = true) String userId,
            @ApiParam(value = "订单金额", required = true) @RequestParam(required = true) BigDecimal orderAmount,
            @ApiParam(value = "商品ids", required = true) @RequestParam(required = true) List<String> goodsSpecIds
    ) throws RemoteServiceException {
        return ResponseHelper.success(couponLogic.useableCoupou(userId, orderAmount, goodsSpecIds));
    }


    @GetMapping("/detail")
    @ApiOperation(value = "券详情")
    public ResponseHelper<CouponBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String couponId
    ) throws RemoteServiceException {
        UserCoupon detail = couponLogic.detail(couponId);
        return ResponseHelper.success(detail);
    }

    @GetMapping("/transfer/qrcode")
    @ApiOperation(value = "获取转赠二维码")
    public ResponseHelper<CouponQrcodeBean> qrcode(@ApiParam(value = "id", required = true) @RequestParam String userCouponId)
            throws RemoteServiceException {
        return ResponseHelper.success(couponLogic.qrcode(userCouponId));
    }


    @GetMapping("/transfer/coupon")
    @ApiOperation(value = "转赠优惠券")
    public ResponseHelper<String> transferCoupon(HttpServletRequest request,@ApiParam(value = "id", required = true) @RequestParam String userCouponId)
            throws RemoteServiceException {

        return ResponseHelper.success(couponLogic.transferCoupon(request,userCouponId));
    }


}
