package com.crm.pay.controller.base;

import com.crm.pay.entity.FinanceDepositParameterEntity;
import com.crm.pay.service.SysConfigService;
import com.crm.rely.backend.configuration.LocaleMessage;
import com.crm.rely.backend.core.exception.PayValidatedException;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.model.constant.ConfigConstants;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositAddEntity;
import com.crm.rely.backend.util.GetIpAndMac;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;

public abstract class BasePayCloseFunctionController {

    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private SysConfigService sysConfigService;


    /**
     * 构建balanceSaveEntity实体
     *
     * @param login   入金账号
     * @param amount  入金金额($)
     * @param payType 那个系统支付
     * @return
     */
    protected FinanceDepositAddEntity getFinanceDepositSaveEntity(Long login, BigDecimal amount, String payType,
                                                                  HttpServletRequest request) {

        return getFinanceDepositSaveEntity(login, amount, payType, payType, request);
    }

    protected FinanceDepositAddEntity getFinanceDepositSaveEntity(Long login, BigDecimal amount, String payType,
                                                                  int activity, HttpServletRequest request) {

        return getFinanceDepositSaveEntity(login, amount, payType, payType, activity, request);
    }

    protected FinanceDepositAddEntity getFinanceDepositSaveEntity(Long login, BigDecimal amount, String payType,
                                                                  int activity, FinanceDepositParameterEntity entity,
                                                                  HttpServletRequest request) {

        return getFinanceDepositSaveEntity(login, amount, payType, payType, activity, entity, request);
    }

    protected FinanceDepositAddEntity getFinanceDepositSaveEntity(Long login, BigDecimal amount, String bankCode,
                                                                  String payType, HttpServletRequest request) {

        return getFinanceDepositSaveEntity(login, amount, bankCode, payType, 0, request);
    }

    protected FinanceDepositAddEntity getFinanceDepositSaveEntity(Long login, BigDecimal amount, String bankCode,
                                                                  String payType, int activity,
                                                                  HttpServletRequest request) {


        return getFinanceDepositSaveEntity(login, amount, bankCode, payType, activity, null, request);
    }

    protected FinanceDepositAddEntity getFinanceDepositSaveEntity(Long login, BigDecimal amount, String bankCode,
                                                                  String payType, int activity,
                                                                  FinanceDepositParameterEntity entity,
                                                                  HttpServletRequest request) {
        FinanceDepositAddEntity financeDepositAddEntity = new FinanceDepositAddEntity();
        financeDepositAddEntity.setAmount(amount);
        financeDepositAddEntity.setLogin(login);
        financeDepositAddEntity.setAddTime(new Date());
        financeDepositAddEntity.setBankCode(bankCode);
        financeDepositAddEntity.setPayType(payType);
        financeDepositAddEntity.setActivityDeposit(activity);
        financeDepositAddEntity.setGoodIds(entity.getGoodIds());
        financeDepositAddEntity.setPayName(entity.getPayName());
        financeDepositAddEntity.setPayPhone(entity.getPayPhone());

        if (entity != null) {
            if (!Strings.isNullOrEmpty(entity.getPromoCode())) {
                financeDepositAddEntity.setPromoCode(entity.getPromoCode());
            }
            if (!Strings.isNullOrEmpty(entity.getAreaCode())) {
                financeDepositAddEntity.setAreaCode(entity.getAreaCode());
            }
            if (!Strings.isNullOrEmpty(entity.getPhone())) {
                financeDepositAddEntity.setPhone(entity.getPhone());
            }
            if (!Strings.isNullOrEmpty(entity.getName())) {
                financeDepositAddEntity.setName(entity.getName());
            }
            if (!Strings.isNullOrEmpty(entity.getBankCardNum())) {
                financeDepositAddEntity.setBankCardNum(entity.getBankCardNum());
            }
            if (!Strings.isNullOrEmpty(entity.getBankUname())) {
                financeDepositAddEntity.setBankUname(entity.getBankUname());
            }
            if (!Strings.isNullOrEmpty(entity.getCvv())) {
                financeDepositAddEntity.setCvv(entity.getCvv());
            }
            if (!Strings.isNullOrEmpty(entity.getExpiryMonth())) {
                financeDepositAddEntity.setExpiryMonth(entity.getExpiryMonth());
            }
            if (!Strings.isNullOrEmpty(entity.getExpiryYear())) {
                financeDepositAddEntity.setExpiryYear(entity.getExpiryYear());
            }
            if (!Strings.isNullOrEmpty(entity.getPin())) {
                financeDepositAddEntity.setPin(entity.getPin());
            }

        }
        if (!Strings.isNullOrEmpty(financeDepositAddEntity.getIp())) {
            return financeDepositAddEntity;
        }

        if (request == null) {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

            if (requestAttributes != null) {
                request = ((ServletRequestAttributes) requestAttributes).getRequest();
            }
        }
        if (request != null) {
            String ip = GetIpAndMac.getClientIp(request);
            if (Strings.isNullOrEmpty(ip)) {
                ip = GetIpAndMac.getIp(request);
            }

            financeDepositAddEntity.setIp(ip);
        }


        return financeDepositAddEntity;
    }

    /**
     * 验证login和balanceNumber是否为null和0
     *
     * @param login  入金的账号
     * @param amount 入金金额($)
     * @return 返回错误信息
     */
    protected void validated(Long login, BigDecimal amount) throws PayValidatedException {
        String error = null;
        /*if (login == null || login <= 0) {
            error = localeMessage.getMessage(PayConstants.PAY_LOGIN_NOT_EMPTY, PayConstants.PAY_LOGIN_NOT_EMPTY);
        } else */if (amount == null) {
            error = localeMessage.getMessage(PayConstants.PAY_REMITTANCE_AMOUNT_NOT_EMPTY,
                    PayConstants.PAY_REMITTANCE_AMOUNT_NOT_EMPTY);
        }
        if (!Strings.isNullOrEmpty(error)) {
            //逻辑最好是重定向到特定页面 然后显示对应错误 现在直接返回错误信息 在页面上打印出来
            throw new PayValidatedException(error);
        }
    }

    protected void sendRedirect(HttpServletResponse response) throws Exception {
        String url = "";
        SysConfigTable sysConfigTable = sysConfigService.getByCode(ConfigConstants.WEB_MY_URE_CONFIG);
        if (sysConfigTable != null) {
            url = sysConfigTable.getValue();
        }
        response.sendRedirect(url + "/payment.html");
    }

}
