package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.dto.callback.ecom.EcomResultDto;
import com.crm.pay.entity.callback.EcomCallbackEntity;
import com.crm.pay.service.EcomService;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.pay.dto.PayDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositAddEntity;
import com.crm.pay.entity.FinanceDepositParameterEntity;
import com.crm.pay.entity.finance.withdraw.automatic.FinanceWithdrawEcomEntity;
import com.crm.pay.entity.finance.withdraw.automatic.FinanceWithdrawInfoEntity;
import com.crm.rely.backend.core.exception.PayValidatedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @description:
 * @author: houn
 * @create: 2024-01-20 14:58
 */
@RestController
@RequestMapping("/ecom")
public class EcomController extends BasePayCloseFunctionController {


    @Autowired
    private EcomService service;

    /**
     * 支付
     *
     * @param login  入金账号
     * @param amount 入金金额
     * @return 错误信息
     * @throws Exception
     */
    @RequestMapping("/pay/{login}/{amount}/{bankCode}")
    public BaseResultDto payIdrIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.ECOM_PAY_TYPE_KEY, 0, entity, infoEntity, request);
    }

    @RequestMapping("/pay/{login}/{amount}/{bankCode}/{activity}")
    public BaseResultDto payIdrIbP2p(@PathVariable("login") Long login,
                                     @PathVariable("amount") BigDecimal amount,
                                     @PathVariable("activity") int activity,
                                     @PathVariable("bankCode") String bankCode,
                                     @RequestBody FinanceDepositParameterEntity entity,
                                     CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, bankCode, PayConstants.ECOM_PAY_TYPE_KEY, activity, entity, infoEntity,
                request);
    }

    private BaseResultDto payCall(Long login,
                                  BigDecimal amount,
                                  String bankCode,
                                  String payType,
                                  int activity,
                                  FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {
        validated(login, amount);

        FinanceDepositAddEntity financeDepositSaveEntity = getFinanceDepositSaveEntity(login, amount, bankCode,
                payType, activity, entity, request);

        String serial = service.pay(financeDepositSaveEntity);
        PayDto url = new PayDto(1, serial);
        return BaseResultDto.success(url);
    }

    @PostMapping("/withdraw/info")
    @FeignClientAnnotation
    public BaseResultDto withdrawInfo(@RequestBody FinanceWithdrawInfoEntity entity) throws Exception {

        return BaseResultDto.success(service.withdrawInfo(entity));
    }

    @PostMapping("/withdraw")
    @FeignClientAnnotation
    public BaseResultDto withdraw(@RequestBody FinanceWithdrawEcomEntity entity) throws Exception {
        service.withdraw(entity);

        return BaseResultDto.success();
    }

    @RequestMapping(value = "/callback")
    @ResponseBody
    public EcomResultDto callback(@RequestBody EcomCallbackEntity callbackEntity) throws Exception {

        EcomResultDto resultDto = service.callback(callbackEntity);
        return resultDto;
    }


    /**
     * 同步页面 成功
     *
     * @param response 用于重定向页面
     * @throws IOException
     */
    @RequestMapping(value = "/pickup")
    public void pickup(HttpServletResponse response) throws Exception, PayValidatedException {

        sendRedirect(response);
    }

    @RequestMapping(value = "/error")
    public void error(HttpServletResponse response) throws Exception {

        sendRedirect(response);
    }
}
