package com.yc.ship.module.marketing.controller.app.coupon;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.security.core.annotations.PreAuthenticated;
import com.yc.ship.module.marketing.controller.app.coupon.vo.AppCouponCanUseReqVo;
import com.yc.ship.module.marketing.controller.app.coupon.vo.AppCouponRespVO;
import com.yc.ship.module.marketing.dal.dataobject.coupon.CouponDO;
import com.yc.ship.module.marketing.dal.dataobject.couponCode.CouponCodeDO;
import com.yc.ship.module.marketing.enums.coupon.CouponReceiveStateEnum;
import com.yc.ship.module.marketing.service.coupon.CouponService;
import com.yc.ship.module.marketing.service.couponCode.CouponCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.web.core.util.WebFrameworkUtils.getLoginUserId;


@Tag(name = "用户 APP - 优惠券")
@RestController
@RequestMapping("/marketing/coupon")
@Validated
public class AppCouponController {

    @Resource
    private CouponService couponService;

    @Resource
    private CouponCodeService couponCodeService;

    @GetMapping("/getCanReceiveCouponList")
    @Operation(summary = "获取可以领取的优惠券列表")
    @PreAuthenticated
    public CommonResult<List<AppCouponRespVO>> getCanReceiveCouponList(){
        List<CouponDO> couponList = couponService.getCanReceiveCouponList(getLoginUserId());
        return CommonResult.success(BeanUtils.toBean(couponList, AppCouponRespVO.class));
    }
    @GetMapping("/getCanReceiveCouponListBySpuId")
    @Operation(summary = "获取当前产品可以领取的优惠券列表")
    @PreAuthenticated
    public CommonResult<List<AppCouponRespVO>> getCanReceiveCouponListBySpuId(Long spuId){
        List<CouponDO> couponList = couponService.getCanReceiveCouponListBySpuId(getLoginUserId(), spuId);
        List<AppCouponRespVO> list = BeanUtils.toBean(couponList, AppCouponRespVO.class);
        List<Long> couponIds = CollectionUtils.convertList(list, AppCouponRespVO::getId);
        List<CouponCodeDO> couponCodeList = couponCodeService.getNotUsedCouponCodeList(getLoginUserId(),couponIds);
        Map<Long, List<CouponCodeDO>> longListMap = CollectionUtils.convertMultiMap(couponCodeList, CouponCodeDO::getCouponId);
        list.stream().forEach(item -> {
            List<CouponCodeDO> couponCodeDOList = longListMap.get(item.getId());
            if(couponCodeDOList != null && couponCodeDOList.size() > 0) {
                item.setReceiveState(CouponReceiveStateEnum.RECEIVE_YES.getType());
            }else {
                item.setReceiveState(CouponReceiveStateEnum.RECEIVE_NO.getType());
            }
        });
        return CommonResult.success(list);
    }

    @GetMapping("/getReceivedCouponList")
    @Operation(summary = "获取已领取的优惠券列表")
    @PreAuthenticated
    public CommonResult<List<AppCouponRespVO>> getReceivedCouponList(){
        List<CouponDO> couponList = couponService.getReceivedCouponList(getLoginUserId());
        return CommonResult.success(BeanUtils.toBean(couponList, AppCouponRespVO.class));
    }
    //获取我的未使用优惠券列表
    @GetMapping("/getMyUnUseCouponList")
    @Operation(summary = "获取我的未使用优惠券列表")
    @PreAuthenticated
    public CommonResult<List<AppCouponRespVO>> getMyUnUseCouponList(){
        List<CouponDO> couponList = couponService.getMyUnUseCouponList(getLoginUserId());
        return CommonResult.success(BeanUtils.toBean(couponList, AppCouponRespVO.class));
    }
    //获取我的已使用优惠券列表
    @GetMapping("/getMyUsedCouponList")
    @Operation(summary = "获取我的已使用优惠券列表")
    @PreAuthenticated
    public CommonResult<List<AppCouponRespVO>> getMyUsedCouponList(){
        List<CouponDO> couponList = couponService.getMyUsedCouponList(getLoginUserId());
        return CommonResult.success(BeanUtils.toBean(couponList, AppCouponRespVO.class));
    }
    //获取我的已过期优惠券列表
    @GetMapping("/getMyExpiredCouponList")
    @Operation(summary = "获取我的已过期优惠券列表")
    @PreAuthenticated
    public CommonResult<List<AppCouponRespVO>> getMyExpiredCouponList(){
        List<CouponDO> couponList = couponService.getMyExpiredCouponList(getLoginUserId());
        return CommonResult.success(BeanUtils.toBean(couponList, AppCouponRespVO.class));
    }

    //领取优惠券
    @GetMapping("/receiveCoupon")
    @Operation(summary = "领取优惠券")
    @PreAuthenticated
    public CommonResult<Long> receiveCoupon(Long couponId){
        return CommonResult.success(couponService.receiveCoupon(getLoginUserId(),couponId));
    }

    @GetMapping("/getCoupon")
    @Operation(summary = "获取优惠券")
    @PreAuthenticated
    public CommonResult<AppCouponRespVO> getCoupon(Long id){
        CouponDO couponDO = couponService.getCoupon(id);
        return CommonResult.success(BeanUtils.toBean(couponDO, AppCouponRespVO.class));
    }

    @GetMapping("/getCouponCode")
    @Operation(summary = "获取优惠券码详情")
    @PreAuthenticated
    public CommonResult<AppCouponRespVO> getCouponCode(Long codeId){
        CouponCodeDO couponCode = couponCodeService.getCouponCode(codeId);
        CouponDO couponDO = couponService.getByCouponCodeId(codeId);
        AppCouponRespVO appCouponRespVO = BeanUtils.toBean(couponDO, AppCouponRespVO.class);
        String qrcode = QrCodeUtil.generateAsBase64("couponCodeId:"+couponCode.getCode(), new QrConfig(150, 150), ImgUtil.IMAGE_TYPE_PNG);
        appCouponRespVO.setQrcode(qrcode);
        return CommonResult.success(appCouponRespVO);
    }

    //获取可以使用的优惠券
    @PostMapping("/findCanUseCouponList")
    @Operation(summary = "获取可以使用的优惠券")
    @PreAuthenticated
    public CommonResult<List<AppCouponRespVO>> findCanUseCouponList(@Valid @RequestBody AppCouponCanUseReqVo reqVo){
        if(reqVo.getTravelDate() == null) {
            reqVo.setTravelDate(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        }
        List<CouponDO> couponList = couponService.getCanUseCouponList(getLoginUserId(), reqVo);
        return CommonResult.success(BeanUtils.toBean(couponList, AppCouponRespVO.class));
    }
}
