package com.neusoft.smart.pos.payment.controller;

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.fumao.bean.MemberDiscount;
import com.neusoft.smart.pos.dto.payment.request.FumaoPayRequest;
import com.neusoft.smart.pos.dto.payment.response.*;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.payment.dto.request.FumaoLoginRequest;
import com.neusoft.smart.pos.payment.dto.request.FumaoQueryCalcRequest;
import com.neusoft.smart.pos.payment.dto.response.FumaoCheckUserResponse;
import com.neusoft.smart.pos.payment.repository.FumaoPayRepository;
import com.neusoft.smart.pos.payment.service.FumaoPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.util.ArrayList;
import java.util.List;

@Api(value = "富茂支付管理使用rest接口", description = "富茂支付管理使用rest接口")
@RestController
@RequestMapping(path = "/fumaoPay")
@Validated
public class FumaoPayController {

	@Autowired
	FumaoPayRepository fumaoPayRepository;

	@Autowired
    FumaoPayService fumaoPayService;

	@ApiOperation(value = "富茂查询试算接口H5", notes = "富茂查询试算接口H5")
	@PostMapping("/queryAndCalc")
	public ResponseData<FumaoMemberInfoRes> queryAndCalc(@RequestBody FumaoPayRequest fumaoPayRequest) {
		return new ResponseData<>(fumaoPayRepository.fumaoMemberInfoToThirdPlatform(fumaoPayRequest));
	}

    @ApiOperation(value = "富茂门店用户登录检查接口", notes = "富茂门店用户登录检查接口")
    @PostMapping("/checkUserPassword")
    public ResponseData<?> checkUser(@RequestBody FumaoLoginRequest fumaoLoginRequest) {
        fumaoPayService.fumaoCheckUser(fumaoLoginRequest);
        return new ResponseData<>();
    }

    @ApiOperation(value = "富茂券查询接口", notes = "富茂券查询接口")
    @PostMapping("/couponQueryCalc")
    public ResponseData<FumaoMemberInfoRes> couponQueryCalc(@RequestBody FumaoQueryCalcRequest fumaoCouponQueryCalcRequest) {
        return new ResponseData<>(fumaoPayService.queryAndCalc(fumaoCouponQueryCalcRequest));
    }


    @ApiOperation(value = "富茂查询试算接口", notes = "富茂查询试算接口")
    @PostMapping("/queryCalc")
    public ResponseData<FumaoParameterforNextStep> queryCalc(@RequestBody FumaoQueryCalcRequest fumaoQueryCalcRequest) {
        FumaoMemberInfoRes memberInfo = fumaoPayService.queryAndCalc(fumaoQueryCalcRequest);
        FumaoParameterforNextStep result = new FumaoParameterforNextStep();
        result.setTotalAmount(fumaoQueryCalcRequest.getOrderAmount());
        // 构建predictionTotal
        FumaoPaymentPrediction predictionTotal = new FumaoPaymentPrediction();
        predictionTotal.setOrder_amount(Long.parseLong(memberInfo.getOrder_amount()));
        predictionTotal.setOrder_amount_left(Long.parseLong(memberInfo.getOrder_amount_left()));
        //解析第三方试算返回的结果
        FumaoPromotionData promotionData = memberInfo.getPromotionData();
        FumaoPromotionInfo promotionInfo = promotionData.getPromotion_info();
        FumaoPaymentPrediction paymentPrediction = promotionData.getPayment_prediction();
        MemberDiscount memberDiscount = promotionInfo.getMember_discount();
        FumaoCouponData coupon = promotionInfo.getCoupon() ;
        List<FumaoCouponInfo> couponList = coupon.getCoupon_list();

        //构建PaymentTypeList
        int sequence = 1;
        List<FumaoPaymentType> paymentTypeList = new ArrayList<FumaoPaymentType>();
        if(memberDiscount != null) { //富茂会员折扣
            FumaoPaymentType paymentType = new FumaoPaymentType();
            paymentType.setSequence(sequence++);
            paymentType.setPaymentTypeId(DictionaryConstant.PAY_TYPE_DISCOUNT.getCode());
            paymentType.setPaymentTypeName(DictionaryConstant.PAY_TYPE_DISCOUNT.getValue());
            paymentType.setSelectType("1");
            paymentType.setIsPaymentTypeSelected("1");

            List<FumaoPaymentData> paymentDataList = new ArrayList<FumaoPaymentData>();

            FumaoPaymentData paymentData = new FumaoPaymentData();
            paymentData.setMerchantID(memberInfo.getMerchant_id());
            paymentData.setMemberID(memberInfo.getMem_id());
            paymentData.setL1Title(memberDiscount.getDiscount_description());
            paymentData.setIsSelected("1");
            FumaoPaymentPrediction prediction = new FumaoPaymentPrediction();
            prediction.setOrder_amount(memberDiscount.getOrder_amount());
            prediction.setOrder_amount_left(memberDiscount.getOrder_amount_left());
            prediction.setPromotion_amount(memberDiscount.getDiscount_amount());
            prediction.setAmount_left_after_pay(Long.parseLong(memberDiscount.getRemain_amount()));
            paymentData.setPaymentPrediction(prediction);
            paymentDataList.add(paymentData);
            paymentType.setPaymentData(paymentDataList);
            paymentType.setPaymentPredictionbyType(prediction);
            paymentTypeList.add(paymentType);
        }
        if(couponList != null && couponList.size() > 0) {//富茂优惠券
            FumaoPaymentType paymentType = new FumaoPaymentType();
            paymentType.setSequence(sequence++);
            paymentType.setPaymentTypeId(DictionaryConstant.PAY_TYPE_FM_COUPON.getCode());
            paymentType.setPaymentTypeName(DictionaryConstant.PAY_TYPE_FM_COUPON.getValue());
            paymentType.setSelectType("1");
            paymentType.setIsPaymentTypeSelected("1");

            FumaoPaymentPrediction predictionType = new FumaoPaymentPrediction();
            predictionType.setOrder_amount(paymentPrediction.getOrder_amount());
            predictionType.setOrder_amount_left(paymentPrediction.getOrder_amount_left());
            predictionType.setPromotion_amount(paymentPrediction.getPromotion_amount());
            predictionType.setAmount_left_after_pay(paymentPrediction.getAmount_left_after_pay());

            List<FumaoPaymentData> paymentDataList = new ArrayList<FumaoPaymentData>();
            for (int i = 0; i < couponList.size(); i++) {
                FumaoCouponInfo couponInfo = couponList.get(i);
                FumaoPaymentData paymentData = new FumaoPaymentData();
                paymentData.setCuponID(couponInfo.getCoupon_id());
                paymentData.setCuponDescription(couponInfo.getCoupon_description());
                paymentData.setIsSelected(couponInfo.getIs_selected()+"");
                paymentDataList.add(paymentData);
            }

            FumaoPaymentPrediction prediction = new FumaoPaymentPrediction();
            prediction.setOrder_amount(predictionTotal.getOrder_amount());
            if(memberDiscount != null) {
                prediction.setOrder_amount_left(Long.parseLong(memberDiscount.getRemain_amount()));
            } else {
                prediction.setOrder_amount_left(predictionTotal.getOrder_amount_left());
            }
            prediction.setPromotion_amount(coupon.getCoupon_promotion_amount());
            prediction.setAmount_left_after_pay(Long.parseLong(coupon.getRemain_amount()));
            paymentType.setPaymentPredictionbyType(prediction);
            paymentType.setPaymentData(paymentDataList);
            paymentTypeList.add(paymentType);
        }
        predictionTotal.setPromotion_amount(paymentPrediction.getPromotion_amount());
        predictionTotal.setAmount_left_after_pay(Long.parseLong(coupon.getRemain_amount()));
        result.setPaymentPredictionofTotal(predictionTotal);
        result.setPaymentTypeList(paymentTypeList);
        result.setNextStep("Next");
        result.setMemberID(memberInfo.getMem_id());
        result.setMemberPhoneNumber(memberInfo.getMem_phone_number());
        return new ResponseData<>(result);
    }
}
	