package com.iformall.controller;

import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxMerchantBUser;
import com.iformall.exception.MallinkException;
import com.iformall.pay.WxPayConstant;
import com.iformall.service.PosBrunService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/pos")
@Api(description = "POS服务相关接口")
public class PosController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PosBrunService posService;

    @ApiOperation(value = "检查POS Server是否正常", notes = "")
    @GetMapping("checkAvaiable")
    public ResultData checkAvaiable() {
        return posService.checkAvaiable();
    }

    @ApiOperation(value = "商户POS用户/B端用户登录检查", notes = "{\"phone\":\"string\", \"password\":\"string(单位:分)\"}")
    @PostMapping("checkUserPassword")
    public ResultData checkUserPassword(@RequestBody Map<String, String> params) {
        WxMerchantBUser user = getUser();
        String phone = params.get("phone");
        String password = params.get("password");
        //登录凭证不能为空
        if (StringUtils.isBlank(phone)) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "手机号不能为空");
        }
        if (StringUtils.isBlank(password)) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "密码不能为空");
        }
        return posService.checkUserPassword(user, phone, password);
    }

    @ApiOperation(value = "获取会员折扣/优惠券/消费卡是否启用")
    @PostMapping("getPosMemConfig")
    public ResultData getPosMemConfig() {
        return posService.getPosMemConfig(getTenantId());
    }

    @ApiOperation(value = "获取注册二维码及小票二维码规则")
    @PostMapping("getQrCode")
    public ResultData getQrCode() {
        return posService.getQrCode(getTenantId());
    }

    @ApiOperation(value = "券独立核销-1-检查", notes = "{\"couponOrderId\":\"string\"}")
    @PostMapping("checkCouponOrderForIndepentVerify")
    public ResultData checkCouponOrderForIndepentVerify(@RequestBody Map<String, String> params) {
        WxMerchantBUser user = getUser();
        String couponOrderIdStr = params.get(WxPayConstant.COUPON_ORDER_ID);  // 券包ID

        if (StringUtils.isBlank(couponOrderIdStr)) {
            return errParam(WxPayConstant.COUPON_ORDER_ID);
        }
        String verifyType = WxPayConstant.SCENE_VERIFY;
        return posService.checkCouponOrderForIndepentVerify(user, verifyType, couponOrderIdStr);
    }

    @ApiOperation(value = "券独立核销-2-核销", notes = "{\"couponOrderId\":\"string\", \"payment\":\"string(单位:分)\"}")
    @PostMapping("posVerifyIndepent")
    public ResultData posVerifyIndepent(@RequestBody Map<String, String> params) {
        WxMerchantBUser user = getUser();
        String couponOrderId = params.get("coupon_order_id");
        String payment = params.get("payment");
        if (StringUtils.isBlank(couponOrderId)) {
            String errMessage = "券ID为空";
            logger.error(errMessage);
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), errMessage);
        }
        return posService.posVerifyIndepent(user, couponOrderId, payment);
    }

    @ApiOperation(value = "会员支付-1-会员识别", notes = "{\"mem_id\":\"string(可选)\",\"mem_phone\":\"string(可选)\",\"pos_order_id\":\"string(必填)\",\"pos_amount\":\"string(单位:分)(必填)\"}")
    @PostMapping("checkMem")
    public ResultData checkMem(@RequestBody Map<String, String> params) {
        WxMerchantBUser user = getUser();
        String memIdStr = params.get(WxPayConstant.MEM_ID);                 // 会员ID
        String memPhoneStr = params.get(WxPayConstant.MEM_PHONE);           // 会员手机
        String posOrderIdStr = params.get(WxPayConstant.POS_ORDER_ID);      // POS订单ID
        String posAmountStr = params.get(WxPayConstant.ORDER_AMOUNT);         // POS订单金额(单位:分)

        if (StringUtils.isBlank(posOrderIdStr)) {
            return errParam(WxPayConstant.POS_ORDER_ID);
        }
        if (StringUtils.isBlank(posAmountStr)) {
            return errParam(WxPayConstant.ORDER_AMOUNT);
        }

        if (StringUtils.isBlank(memIdStr) && StringUtils.isBlank(memPhoneStr)) {
            String errMessage = "please give one value for mem_id or mem_phone";
            logger.error(errMessage);
            throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), errMessage);
        }
        return posService.checkMem(user, memIdStr, memPhoneStr, posOrderIdStr, posAmountStr);
    }

    @ApiOperation(value = "会员支付-2-交易核销检查", notes = "{\"coupon_order_id\":\"string(必填)\",\"pos_order_id\":\"string(必填)\",\"pos_amount\":\"string(必填)\"}")
    @PostMapping("checkCouponOrderForPayVerify")
    public ResultData checkCouponOrderForPayVerify(@RequestBody Map<String, String> params) {
        WxMerchantBUser user = getUser();
        String couponOrderIdStr = params.get(WxPayConstant.COUPON_ORDER_ID);  // 券包ID
        String posOrderIdStr = params.get(WxPayConstant.POS_ORDER_ID);        // POS订单ID
        String posAmountStr  = params.get(WxPayConstant.ORDER_AMOUNT);          // POS订单金额

        if (StringUtils.isBlank(couponOrderIdStr)) {
            return errParam(WxPayConstant.COUPON_ORDER_ID);
        }
        if (StringUtils.isBlank(posOrderIdStr)) {
            return errParam(WxPayConstant.POS_ORDER_ID);
        }
        if (StringUtils.isBlank(posAmountStr)) {
            return errParam(WxPayConstant.ORDER_AMOUNT);
        }
        String verifyType = WxPayConstant.SCENE_PAY;
        return posService.checkCouponOrderForPayVerify(user, verifyType, couponOrderIdStr, posOrderIdStr, posAmountStr);
    }

    @ApiOperation(value = "会员支付-3-券预核销", notes = "{\"coupon_order_id\":\"string(必填)\",\"deduct_amount\":\"string(必填)\",\"pos_order_id\":\"string(必填)\",\"pos_amount\":\"string(必填)\"}")
    @PostMapping("couponOrderPreVerify")
    public ResultData couponOrderPreVerify(@RequestBody Map<String, String> params) {
        WxMerchantBUser user = getUser();
        String couponOrderIdStr = params.get(WxPayConstant.COUPON_ORDER_ID);  // 券包ID
        String deductAmountStr = params.get(WxPayConstant.DEDUCT_AMOUNT);     // 扣减金额
        String posOrderIdStr = params.get(WxPayConstant.POS_ORDER_ID);        // POS订单ID
        String posAmountStr = params.get(WxPayConstant.ORDER_AMOUNT);           // POS订单金额(单位:分)

        if (StringUtils.isBlank(couponOrderIdStr)) {
            return errParam(WxPayConstant.COUPON_ORDER_ID);
        }
        if (StringUtils.isBlank(deductAmountStr)) {
            return errParam(WxPayConstant.DEDUCT_AMOUNT);
        }
        if (StringUtils.isBlank(posOrderIdStr)) {
            return errParam(WxPayConstant.POS_ORDER_ID);
        }
        if (StringUtils.isBlank(posAmountStr)) {
            return errParam(WxPayConstant.ORDER_AMOUNT);
        }
        return posService.couponOrderPreVerify(user, couponOrderIdStr, deductAmountStr, posOrderIdStr, posAmountStr);
    }


    @ApiOperation(value = "会员支付-4-支付完成", notes = "{\"pos_order_id\":\"string\"}")
    @PostMapping("posOrderFinished")
    public ResultData posOrderFinished(@RequestBody Map<String, String> params) {
        WxMerchantBUser user = getUser();
        String posOrderIdStr = params.get(WxPayConstant.POS_ORDER_ID);        // POS订单ID
        if (StringUtils.isBlank(posOrderIdStr)) {
            return errParam(WxPayConstant.POS_ORDER_ID);
        }
        // 1. 获取订单信息
        // 2. 获取所有的支付订单信息
        // 3. 同步给FMPOS

        return new ResultData();
    }

    @ApiOperation(value = "会员支付-10-券预核销取消", notes = "{\"coupon_order_id\":\"string(必填)\",\"pos_order_id\":\"string(必填)\",\"pos_amount\":\"string(必填)\"}")
    @PostMapping("couponOrderPreVerifyCancel")
    public ResultData couponOrderPreVerifyCancel(@RequestBody Map<String, String> params) {
        WxMerchantBUser user = getUser();
        String couponOrderIdStr = params.get(WxPayConstant.COUPON_ORDER_ID);  // 券包ID
        String posOrderIdStr = params.get(WxPayConstant.POS_ORDER_ID);        // POS订单ID
        String posAmountStr = params.get(WxPayConstant.ORDER_AMOUNT);           // POS订单金额(单位:分)

        if (StringUtils.isBlank(couponOrderIdStr)) {
            return errParam(WxPayConstant.COUPON_ORDER_ID);
        }
        if (StringUtils.isBlank(posOrderIdStr)) {
            return errParam(WxPayConstant.POS_ORDER_ID);
        }
        if (StringUtils.isBlank(posAmountStr)) {
            return errParam(WxPayConstant.ORDER_AMOUNT);
        }
        return posService.couponOrderPreVerifyCancel(user, couponOrderIdStr, posOrderIdStr, posAmountStr);
    }

    private ResultData errParam(String paramStr) {
        String errMessage = String.format("request params[%s] error.", paramStr);
        logger.error(errMessage);
        return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), errMessage);
    }

}
