package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.FinanceDepositParameterEntity;
import com.crm.pay.entity.FinanceDepositUcardEntity;
import com.crm.pay.entity.callback.UcardPayCallbackEntity;
import com.crm.pay.entity.ucard.CardSendEmailCodeEntity;
import com.crm.pay.service.FinanceDepositService;
import com.crm.pay.service.UcardPayService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.pay.dto.PayDto;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.pay.entity.finance.withdraw.automatic.FinanceWithdrawInfoEntity;
import com.crm.pay.entity.finance.withdraw.automatic.FinanceWithdrawUcardpayEntity;
import com.crm.rely.backend.core.exception.PayValidatedException;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositAddEntity;
import com.crm.rely.backend.util.DateUtil;
import com.crm.rely.backend.util.GetIpAndMac;
import org.springframework.beans.BeanUtils;
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;
import java.util.Date;

@RestController
@RequestMapping("/ucard")
public class UcardPayController extends BasePayCloseFunctionController {

    @Autowired
    private UcardPayService service;
    @Autowired
    private FinanceDepositService financeDepositService;

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

        return payCall(login, amount, activity, entity, infoEntity, request);
    }

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

        return payCall(login, amount, 0, entity, infoEntity, request);
    }

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

        FinanceDepositAddEntity financeDepositSaveEntity = getFinanceDepositSaveEntity(login, amount,
                PayConstants.UCARD_PAY_KEY, activity, entity, request);

        String serial = service.save(financeDepositSaveEntity);
        PayDto payDto = new PayDto(9, serial);
        return BaseResultDto.success(payDto);
    }

    @PostMapping("/pay")
    public BaseResultDto pay(@RequestBody FinanceDepositUcardEntity entity) throws Exception {
        service.validated(entity);
        return BaseResultDto.success(service.pay(entity));
    }

    /*@PostMapping("/send/email/code")
    public BaseResultDto sendEmailCode(@RequestBody CardSendEmailCodeEntity entity, HttpServletRequest req) throws Exception {
        entity.setIp(GetIpAndMac.getIp(req));
        entity.setTime(new Date());
        service.sendEmailCode(entity);
        return BaseResultDto.success(Constants.SEND_SUCCESS);
    }*/

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

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

    /**
     * 异步回调
     *
     * @param callbackEntity 用于获取回调数据
     * @return 返回成功给第三方系统
     * @throws Exception
     */
    @RequestMapping(value = "/callback")
    public BaseResultDto callback(@RequestBody UcardPayCallbackEntity callbackEntity, HttpServletResponse response) throws Exception {
        try {
            return service.callback(callbackEntity);
        } catch (Exception e) {
            return BaseResultDto.error("system error");
        }
    }

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

        return BaseResultDto.success();
    }

    @RequestMapping("/withdraw/callback")
    public BaseResultDto withdrawCallback(@RequestBody UcardPayCallbackEntity entity) throws Exception {
        try {
            return service.withdrawCallback(1, entity);
        } catch (Exception e) {
            return BaseResultDto.error("system error");
        }

    }

    @RequestMapping("/agent/withdraw/callback")
    public BaseResultDto agentWithdrawCallback(@RequestBody UcardPayCallbackEntity entity) throws Exception {
        try {
            return service.withdrawCallback(2, entity);
        } catch (Exception e) {
            return BaseResultDto.error("system error");
        }

    }

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

        sendRedirect(response);
    }

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

        sendRedirect(response);
    }
}
