package com.ycwl.basic.controller.mobile;

import com.ycwl.basic.model.mobile.claim.ClaimReq;
import com.ycwl.basic.model.mobile.claim.ClaimResp;
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
import com.ycwl.basic.pricing.dto.CouponClaimRequest;
import com.ycwl.basic.pricing.dto.CouponClaimResult;
import com.ycwl.basic.pricing.dto.req.VoucherPrintReq;
import com.ycwl.basic.pricing.dto.resp.VoucherPrintResp;
import com.ycwl.basic.pricing.enums.CouponType;
import com.ycwl.basic.pricing.service.ICouponService;
import com.ycwl.basic.pricing.service.VoucherPrintService;
import com.ycwl.basic.repository.FaceRepository;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
import com.ycwl.basic.utils.ApiResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;

@RestController
@RequestMapping("/api/mobile/claim/v1")
@AllArgsConstructor
public class AppClaimController {
    private final FaceRepository faceRepository;
    private final ScenicRepository scenicRepository;
    private final VoucherPrintService voucherPrintService;
    private final ICouponService couponService;


    @PostMapping("tryClaim")
    public ApiResponse<ClaimResp> tryClaim(@RequestBody ClaimReq req) {
        FaceEntity face = faceRepository.getFace(req.getFaceId());
        if (face == null) {
            return ApiResponse.fail("请选择人脸");
        }
        ClaimResp claimResp = new ClaimResp();
        ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(face.getScenicId());
        if (Boolean.TRUE.equals(scenicConfig.getBoolean("voucher_enable"))) {
            // 可以领券
            VoucherPrintResp voucherPrintResp = voucherPrintService.queryPrintedVoucher(face.getId());
            if (voucherPrintResp == null) {
                // 打印
                if (req.getMorphId() != null) {
                    voucherPrintResp = voucherPrintService.printVoucherTicket(new VoucherPrintReq(face.getId(), req.getMorphId(), face.getScenicId()));
                }
            }
            if (voucherPrintResp != null) {
                claimResp.setHasCoupon(false);
                claimResp.setHasPrint(true);
                claimResp.setPrintCode(voucherPrintResp.getCode());
                claimResp.setPrintType(voucherPrintResp.getType());
                return ApiResponse.success(claimResp);
            }
        }
        if (Boolean.TRUE.equals(scenicConfig.getBoolean("booking_enable"))) {
            VoucherPrintResp voucherPrintResp = voucherPrintService.queryPrintedVoucher(face.getId());
            if (voucherPrintResp == null) {
                // 打印
                if (req.getMorphId() != null) {
                    voucherPrintResp = voucherPrintService.printBookingTicket(new VoucherPrintReq(face.getId(), req.getMorphId(), face.getScenicId()));
                }
            }
            if (voucherPrintResp != null) {
                claimResp.setHasCoupon(false);
                claimResp.setHasPrint(true);
                claimResp.setPrintCode(voucherPrintResp.getCode());
                claimResp.setPrintType(voucherPrintResp.getType());
                return ApiResponse.success(claimResp);
            }
        }
        if (req.getType() != null) {
            // 第几次进入
            Integer couponId = scenicConfig.getInteger("coupon_id_for_type_" + req.getType());
            if (couponId != null) {
                // 可以领券
                CouponClaimRequest request = new CouponClaimRequest(face.getMemberId(), Long.valueOf(couponId));
                CouponClaimResult claimResult = couponService.claimCoupon(request);
                if (claimResult.isSuccess()) {
                    // 领到了
                    claimResp.setHasCoupon(true);
                    switch (claimResult.getCoupon().getCouponType()) {
                        case CouponType.PERCENTAGE:
                            claimResp.setCouponType("折扣优惠券");
                            claimResp.setCouponDesc("打" + (BigDecimal.valueOf(1).setScale(2, RoundingMode.HALF_UP).subtract(claimResult.getCoupon().getDiscountValue())).multiply(BigDecimal.valueOf(10)) + "折");
                            break;
                        case CouponType.FIXED_AMOUNT:
                            if (claimResult.getCoupon().getMinAmount().compareTo(BigDecimal.ZERO) > 0) {
                                claimResp.setCouponType("满减优惠券");
                                claimResp.setCouponDesc("满" + claimResult.getCoupon().getMinAmount() + "减" + claimResult.getCoupon().getDiscountValue());
                            } else {
                                claimResp.setCouponType("直减优惠券");
                                claimResp.setCouponDesc("直减" + claimResult.getCoupon().getDiscountValue());
                            }
                            break;
                        default:
                            claimResp.setCouponType("普通优惠券");
                            break;
                    }
                    claimResp.setCouponDesc(scenicConfig.getString("coupon_desc_for_type_" + req.getType(), "专属折扣券"));
                    claimResp.setCouponCountdown(scenicConfig.getString("coupon_countdown_for_type_" + req.getType(), "送你优惠，保存美好！"));
                    return ApiResponse.success(claimResp);
                }
            }
        }
        return ApiResponse.fail("异常");
    }
}
